blob: 20ebcfa3c92ecd9760464d96d0cc7f133c10ccff [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);
Andy Adamson774d5f12013-05-20 14:13:50 -0400575 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400576 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500577 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500578 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400579 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400580 }
581
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500582 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500583 if (IS_ERR(slot)) {
584 /* If out of memory, try again in 1/4 second */
585 if (slot == ERR_PTR(-ENOMEM))
586 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400587 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400588 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589 }
590 spin_unlock(&tbl->slot_tbl_lock);
591
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500592 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400593
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500594 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
595 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596
Benny Halevydfb4f3092010-09-24 09:17:01 -0400597 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500598 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400599 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600 /*
601 * sr_status is only set in decode_sequence, and so will remain
602 * set to 1 if an rpc level failure occurs.
603 */
604 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400605out_success:
606 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400608out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400609 /* Privileged tasks are queued with top priority */
610 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400611 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
612 NULL, RPC_PRIORITY_PRIVILEGED);
613 else
614 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400615 spin_unlock(&tbl->slot_tbl_lock);
616 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000618EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619
Trond Myklebust035168a2010-06-16 09:52:26 -0400620int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 struct nfs4_sequence_args *args,
622 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 struct rpc_task *task)
624{
Trond Myklebust035168a2010-06-16 09:52:26 -0400625 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626 int ret = 0;
627
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400628 if (session == NULL) {
629 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400631 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400632
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500633 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400634 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500635 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400636
Trond Myklebust9d12b212012-01-17 22:04:25 -0500637 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638out:
639 dprintk("<-- %s status=%d\n", __func__, ret);
640 return ret;
641}
642
643struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400644 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 struct nfs4_sequence_args *seq_args;
646 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647};
648
649static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
650{
651 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400652 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653
Trond Myklebust035168a2010-06-16 09:52:26 -0400654 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
655
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400656 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657}
658
Andy Adamson69ab40c2009-04-01 09:22:19 -0400659static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
660{
661 struct nfs41_call_sync_data *data = calldata;
662
Trond Myklebust14516c32010-07-31 14:29:06 -0400663 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400664}
665
Trond Myklebust17280172012-03-11 13:11:00 -0400666static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400668 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669};
670
Bryan Schumaker7c513052011-03-24 17:12:24 +0000671static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
672 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673 struct rpc_message *msg,
674 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400675 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676{
677 int ret;
678 struct rpc_task *task;
679 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400680 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 .seq_args = args,
682 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 };
684 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000685 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 .rpc_message = msg,
687 .callback_ops = &nfs41_call_sync_ops,
688 .callback_data = &data
689 };
690
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691 task = rpc_run_task(&task_setup);
692 if (IS_ERR(task))
693 ret = PTR_ERR(task);
694 else {
695 ret = task->tk_status;
696 rpc_put_task(task);
697 }
698 return ret;
699}
700
Trond Myklebustdf896452010-06-16 09:52:26 -0400701#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400702static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500703void nfs41_init_sequence(struct nfs4_sequence_args *args,
704 struct nfs4_sequence_res *res, int cache_reply)
705{
706}
707
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400708static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
709{
710}
711
712
Trond Myklebust14516c32010-07-31 14:29:06 -0400713static int nfs4_sequence_done(struct rpc_task *task,
714 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400715{
Trond Myklebust14516c32010-07-31 14:29:06 -0400716 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400717}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400718#endif /* CONFIG_NFS_V4_1 */
719
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400720static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000721int _nfs4_call_sync(struct rpc_clnt *clnt,
722 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400723 struct rpc_message *msg,
724 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400725 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400726{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400728}
729
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400730static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731int nfs4_call_sync(struct rpc_clnt *clnt,
732 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
735 struct nfs4_sequence_res *res,
736 int cache_reply)
737{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400738 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000739 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400740 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000741}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400742
Trond Myklebust38478b22006-05-25 01:40:57 -0400743static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Trond Myklebust38478b22006-05-25 01:40:57 -0400745 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Trond Myklebust38478b22006-05-25 01:40:57 -0400747 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400748 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700749 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400750 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700751 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000752 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400753 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400757 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758 struct nfs_openargs o_arg;
759 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100760 struct nfs_open_confirmargs c_arg;
761 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500762 struct nfs4_string owner_name;
763 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400766 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400768 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100770 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400771 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400772 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100773 int rpc_status;
774 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775};
776
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400777static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
778 int err, struct nfs4_exception *exception)
779{
780 if (err != -EINVAL)
781 return false;
782 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
783 return false;
784 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
785 exception->retry = 1;
786 return true;
787}
788
789static enum open_claim_type4
790nfs4_map_atomic_open_claim(struct nfs_server *server,
791 enum open_claim_type4 claim)
792{
793 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
794 return claim;
795 switch (claim) {
796 default:
797 return claim;
798 case NFS4_OPEN_CLAIM_FH:
799 return NFS4_OPEN_CLAIM_NULL;
800 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
801 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
802 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
803 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
804 }
805}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400806
807static void nfs4_init_opendata_res(struct nfs4_opendata *p)
808{
809 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400810 p->o_res.seqid = p->o_arg.seqid;
811 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400812 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400813 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400814 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500815 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400816}
817
Al Viro82a2c1b2011-06-22 18:30:55 -0400818static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400820 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400821 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400822 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823{
Al Viro82a2c1b2011-06-22 18:30:55 -0400824 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 struct inode *dir = parent->d_inode;
826 struct nfs_server *server = NFS_SERVER(dir);
827 struct nfs4_opendata *p;
828
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400829 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 if (p == NULL)
831 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400832 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100833 if (p->o_arg.seqid == NULL)
834 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400835 nfs_sb_active(dentry->d_sb);
836 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 p->dir = parent;
838 p->owner = sp;
839 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500840 p->o_arg.open_flags = flags;
841 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700842 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
843 * will return permission denied for all bits until close */
844 if (!(flags & O_EXCL)) {
845 /* ask server to check for all possible rights as results
846 * are cached */
847 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
848 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700849 }
David Howells7539bba2006-08-22 20:06:09 -0400850 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400851 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
852 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400853 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 p->o_arg.server = server;
855 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400856 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400857 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
858 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400859 case NFS4_OPEN_CLAIM_NULL:
860 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
861 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
862 p->o_arg.fh = NFS_FH(dir);
863 break;
864 case NFS4_OPEN_CLAIM_PREVIOUS:
865 case NFS4_OPEN_CLAIM_FH:
866 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
867 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
868 p->o_arg.fh = NFS_FH(dentry->d_inode);
869 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500870 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500871 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400872
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100873 p->o_arg.u.attrs = &p->attrs;
874 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500875
876 verf[0] = jiffies;
877 verf[1] = current->pid;
878 memcpy(p->o_arg.u.verifier.data, verf,
879 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100880 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100881 p->c_arg.fh = &p->o_res.fh;
882 p->c_arg.stateid = &p->o_res.stateid;
883 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400884 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400885 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886 return p;
887err_free:
888 kfree(p);
889err:
890 dput(parent);
891 return NULL;
892}
893
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400894static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100895{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400896 struct nfs4_opendata *p = container_of(kref,
897 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400898 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400899
900 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400901 if (p->state != NULL)
902 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400903 nfs4_put_state_owner(p->owner);
904 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400905 dput(p->dentry);
906 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500907 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400908 kfree(p);
909}
910
911static void nfs4_opendata_put(struct nfs4_opendata *p)
912{
913 if (p != NULL)
914 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100915}
916
Trond Myklebust06f814a2006-01-03 09:55:07 +0100917static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
918{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100919 int ret;
920
Trond Myklebust06f814a2006-01-03 09:55:07 +0100921 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100922 return ret;
923}
924
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400926{
927 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928
Trond Myklebust536e43d2012-01-17 22:04:26 -0500929 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 goto out;
931 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400932 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500933 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
934 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400935 break;
936 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500937 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
938 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400939 break;
940 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500941 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
942 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400943 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400945 return ret;
946}
947
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400949{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500950 if (delegation == NULL)
951 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400953 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500954 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400955 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500956 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
957 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500958 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400959 return 1;
960}
961
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100963{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100965 case FMODE_WRITE:
966 state->n_wronly++;
967 break;
968 case FMODE_READ:
969 state->n_rdonly++;
970 break;
971 case FMODE_READ|FMODE_WRITE:
972 state->n_rdwr++;
973 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100975}
976
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400978{
979 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500980 nfs4_stateid_copy(&state->stateid, stateid);
981 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -0400982 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500983 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400984 case FMODE_READ:
985 set_bit(NFS_O_RDONLY_STATE, &state->flags);
986 break;
987 case FMODE_WRITE:
988 set_bit(NFS_O_WRONLY_STATE, &state->flags);
989 break;
990 case FMODE_READ|FMODE_WRITE:
991 set_bit(NFS_O_RDWR_STATE, &state->flags);
992 }
993}
994
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400996{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400997 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400999 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001000}
1001
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001004 /*
1005 * Protect the call to nfs4_state_set_mode_locked and
1006 * serialise the stateid update
1007 */
1008 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001009 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001010 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001011 set_bit(NFS_DELEGATED_STATE, &state->flags);
1012 }
1013 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001015 write_sequnlock(&state->seqlock);
1016 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001018 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001022{
1023 struct nfs_inode *nfsi = NFS_I(state->inode);
1024 struct nfs_delegation *deleg_cur;
1025 int ret = 0;
1026
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001028
1029 rcu_read_lock();
1030 deleg_cur = rcu_dereference(nfsi->delegation);
1031 if (deleg_cur == NULL)
1032 goto no_delegation;
1033
1034 spin_lock(&deleg_cur->lock);
1035 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001036 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001038 goto no_delegation_unlock;
1039
1040 if (delegation == NULL)
1041 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001042 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001043 goto no_delegation_unlock;
1044
Trond Myklebustb7391f42008-12-23 15:21:52 -05001045 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001047 ret = 1;
1048no_delegation_unlock:
1049 spin_unlock(&deleg_cur->lock);
1050no_delegation:
1051 rcu_read_unlock();
1052
1053 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001055 ret = 1;
1056 }
1057
1058 return ret;
1059}
1060
1061
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001063{
1064 struct nfs_delegation *delegation;
1065
1066 rcu_read_lock();
1067 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001068 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069 rcu_read_unlock();
1070 return;
1071 }
1072 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001073 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074}
1075
Trond Myklebust6ee41262007-07-08 14:11:36 -04001076static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077{
1078 struct nfs4_state *state = opendata->state;
1079 struct nfs_inode *nfsi = NFS_I(state->inode);
1080 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001081 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001082 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001083 nfs4_stateid stateid;
1084 int ret = -EAGAIN;
1085
Trond Myklebustaac00a82007-07-05 19:02:21 -04001086 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001088 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089 if (can_open_cached(state, fmode, open_mode)) {
1090 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001092 goto out_return_state;
1093 }
1094 spin_unlock(&state->owner->so_lock);
1095 }
Trond Myklebust34310432008-12-23 15:21:38 -05001096 rcu_read_lock();
1097 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001098 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001099 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001100 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001101 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001103 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001105 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001106 if (!opendata->is_recover) {
1107 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1108 if (ret != 0)
1109 goto out;
1110 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001112
1113 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001114 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001115 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117out:
1118 return ERR_PTR(ret);
1119out_return_state:
1120 atomic_inc(&state->count);
1121 return state;
1122}
1123
Andy Adamsone23008e2012-10-02 21:07:32 -04001124static void
1125nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1126{
1127 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1128 struct nfs_delegation *delegation;
1129 int delegation_flags = 0;
1130
1131 rcu_read_lock();
1132 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1133 if (delegation)
1134 delegation_flags = delegation->flags;
1135 rcu_read_unlock();
1136 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1137 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1138 "returning a delegation for "
1139 "OPEN(CLAIM_DELEGATE_CUR)\n",
1140 clp->cl_hostname);
1141 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1142 nfs_inode_set_delegation(state->inode,
1143 data->owner->so_cred,
1144 &data->o_res);
1145 else
1146 nfs_inode_reclaim_delegation(state->inode,
1147 data->owner->so_cred,
1148 &data->o_res);
1149}
1150
1151/*
1152 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1153 * and update the nfs4_state.
1154 */
1155static struct nfs4_state *
1156_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1157{
1158 struct inode *inode = data->state->inode;
1159 struct nfs4_state *state = data->state;
1160 int ret;
1161
Weston Andros Adamson231817e2013-10-21 13:10:13 -04001162 if (!data->rpc_done) {
1163 if (data->rpc_status) {
1164 ret = data->rpc_status;
1165 goto err;
1166 }
1167 /* cached opens have already been processed */
1168 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001169 }
1170
Andy Adamsone23008e2012-10-02 21:07:32 -04001171 ret = nfs_refresh_inode(inode, &data->f_attr);
1172 if (ret)
1173 goto err;
1174
1175 if (data->o_res.delegation_type != 0)
1176 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamson231817e2013-10-21 13:10:13 -04001177update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001178 update_open_stateid(state, &data->o_res.stateid, NULL,
1179 data->o_arg.fmode);
Trond Myklebust13111572013-10-28 14:57:12 -04001180 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001181
1182 return state;
1183err:
1184 return ERR_PTR(ret);
1185
1186}
1187
1188static struct nfs4_state *
1189_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001190{
1191 struct inode *inode;
1192 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001193 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001194
Trond Myklebustaac00a82007-07-05 19:02:21 -04001195 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001196 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001197 goto out;
1198 }
1199
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001200 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001201 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001202 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001203 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001204 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001205 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001206 goto err;
1207 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001208 state = nfs4_get_open_state(inode, data->owner);
1209 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001210 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001211 if (data->o_res.delegation_type != 0)
1212 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001213 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001214 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001215 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001216out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001217 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001218 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001219err_put_inode:
1220 iput(inode);
1221err:
1222 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001223}
1224
Andy Adamsone23008e2012-10-02 21:07:32 -04001225static struct nfs4_state *
1226nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1227{
1228 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1229 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1230 return _nfs4_opendata_to_nfs4_state(data);
1231}
1232
Trond Myklebust864472e2006-01-03 09:55:15 +01001233static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1234{
1235 struct nfs_inode *nfsi = NFS_I(state->inode);
1236 struct nfs_open_context *ctx;
1237
1238 spin_lock(&state->inode->i_lock);
1239 list_for_each_entry(ctx, &nfsi->open_files, list) {
1240 if (ctx->state != state)
1241 continue;
1242 get_nfs_open_context(ctx);
1243 spin_unlock(&state->inode->i_lock);
1244 return ctx;
1245 }
1246 spin_unlock(&state->inode->i_lock);
1247 return ERR_PTR(-ENOENT);
1248}
1249
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001250static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1251 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001252{
1253 struct nfs4_opendata *opendata;
1254
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001255 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1256 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001257 if (opendata == NULL)
1258 return ERR_PTR(-ENOMEM);
1259 opendata->state = state;
1260 atomic_inc(&state->count);
1261 return opendata;
1262}
1263
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001264static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001265{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001266 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001267 int ret;
1268
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001269 opendata->o_arg.open_flags = 0;
1270 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001271 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1272 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1273 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001274 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001275 if (ret != 0)
1276 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001277 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001278 if (IS_ERR(newstate))
1279 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001280 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001281 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 return 0;
1283}
1284
1285static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1286{
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 int ret;
1289
1290 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001291 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001292 clear_bit(NFS_OPEN_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 Myklebust270e2342014-11-10 18:43:56 -05001419 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001420 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 &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001545 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001546 can_open_delegated(delegation, data->o_arg.fmode))
1547 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548 rcu_read_unlock();
1549 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001550 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001551 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001552 switch (data->o_arg.claim) {
1553 case NFS4_OPEN_CLAIM_PREVIOUS:
1554 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1555 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001556 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001557 case NFS4_OPEN_CLAIM_FH:
1558 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001559 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1560 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001561 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001562 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001563 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001564 &data->o_res.seq_res,
1565 task) != 0)
1566 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001567
1568 /* Set the create mode (note dependency on the session type) */
1569 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1570 if (data->o_arg.open_flags & O_EXCL) {
1571 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1572 if (nfs4_has_persistent_session(clp))
1573 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1574 else if (clp->cl_mvops->minor_version > 0)
1575 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1576 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001577 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001578unlock_no_action:
1579 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001580out_no_action:
1581 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001582out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001583 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001584}
1585
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001586static void nfs4_open_done(struct rpc_task *task, void *calldata)
1587{
1588 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001590 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001591
Trond Myklebust14516c32010-07-31 14:29:06 -04001592 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1593 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001594
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001595 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001596 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1597 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001598 case S_IFREG:
1599 break;
1600 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001601 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001602 break;
1603 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001604 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001605 break;
1606 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001607 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001608 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001609 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001610 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001611 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1612 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001613 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001614 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001615}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001616
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001617static void nfs4_open_release(void *calldata)
1618{
1619 struct nfs4_opendata *data = calldata;
1620 struct nfs4_state *state = NULL;
1621
1622 /* If this request hasn't been cancelled, do nothing */
1623 if (data->cancelled == 0)
1624 goto out_free;
1625 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001626 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001627 goto out_free;
1628 /* In case we need an open_confirm, no cleanup! */
1629 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1630 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001631 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001632 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001633 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001634out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001635 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001636}
1637
1638static const struct rpc_call_ops nfs4_open_ops = {
1639 .rpc_call_prepare = nfs4_open_prepare,
1640 .rpc_call_done = nfs4_open_done,
1641 .rpc_release = nfs4_open_release,
1642};
1643
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001644static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001645{
1646 struct inode *dir = data->dir->d_inode;
1647 struct nfs_server *server = NFS_SERVER(dir);
1648 struct nfs_openargs *o_arg = &data->o_arg;
1649 struct nfs_openres *o_res = &data->o_res;
1650 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001651 struct rpc_message msg = {
1652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1653 .rpc_argp = o_arg,
1654 .rpc_resp = o_res,
1655 .rpc_cred = data->owner->so_cred,
1656 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001657 struct rpc_task_setup task_setup_data = {
1658 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001659 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001660 .callback_ops = &nfs4_open_ops,
1661 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001662 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001663 .flags = RPC_TASK_ASYNC,
1664 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001665 int status;
1666
Trond Myklebust9d12b212012-01-17 22:04:25 -05001667 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001668 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001669 data->rpc_done = 0;
1670 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001671 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001672 data->is_recover = 0;
1673 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001674 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001675 data->is_recover = 1;
1676 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001677 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001678 if (IS_ERR(task))
1679 return PTR_ERR(task);
1680 status = nfs4_wait_for_completion_rpc_task(task);
1681 if (status != 0) {
1682 data->cancelled = 1;
1683 smp_wmb();
1684 } else
1685 status = data->rpc_status;
1686 rpc_put_task(task);
1687
1688 return status;
1689}
1690
1691static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1692{
1693 struct inode *dir = data->dir->d_inode;
1694 struct nfs_openres *o_res = &data->o_res;
1695 int status;
1696
1697 status = nfs4_run_open_task(data, 1);
1698 if (status != 0 || !data->rpc_done)
1699 return status;
1700
Trond Myklebust6926afd2012-01-07 13:22:46 -05001701 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1702
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001703 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1704 status = _nfs4_proc_open_confirm(data);
1705 if (status != 0)
1706 return status;
1707 }
1708
1709 return status;
1710}
1711
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001712static int nfs4_opendata_access(struct rpc_cred *cred,
1713 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001714 struct nfs4_state *state, fmode_t fmode,
1715 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001716{
1717 struct nfs_access_entry cache;
1718 u32 mask;
1719
1720 /* access call failed or for some reason the server doesn't
1721 * support any access modes -- defer access call until later */
1722 if (opendata->o_res.access_supported == 0)
1723 return 0;
1724
1725 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001726 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001727 * write mode bits, but POSIX allows the creating process to write.
1728 * use openflags to check for exec, because fmode won't
1729 * always have FMODE_EXEC set when file open for exec. */
1730 if (openflags & __FMODE_EXEC) {
1731 /* ONLY check for exec rights */
1732 mask = MAY_EXEC;
1733 } else if (fmode & FMODE_READ)
1734 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001735
1736 cache.cred = cred;
1737 cache.jiffies = jiffies;
1738 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1739 nfs_access_add_cache(state->inode, &cache);
1740
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001741 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001742 return 0;
1743
1744 /* even though OPEN succeeded, access is denied. Close the file */
1745 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001746 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001747}
1748
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001749/*
1750 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1751 */
1752static int _nfs4_proc_open(struct nfs4_opendata *data)
1753{
1754 struct inode *dir = data->dir->d_inode;
1755 struct nfs_server *server = NFS_SERVER(dir);
1756 struct nfs_openargs *o_arg = &data->o_arg;
1757 struct nfs_openres *o_res = &data->o_res;
1758 int status;
1759
1760 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001761 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001762 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001763 if (status != 0) {
1764 if (status == -NFS4ERR_BADNAME &&
1765 !(o_arg->open_flags & O_CREAT))
1766 return -ENOENT;
1767 return status;
1768 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001769
Trond Myklebust6926afd2012-01-07 13:22:46 -05001770 nfs_fattr_map_and_free_names(server, &data->f_attr);
1771
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001772 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001773 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001774 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1775 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001777 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001782 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Andy Adamsond83217c2011-03-01 01:34:17 +00001786static int nfs4_recover_expired_lease(struct nfs_server *server)
1787{
1788 return nfs4_client_recover_expired_lease(server->nfs_client);
1789}
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791/*
1792 * OPEN_EXPIRED:
1793 * reclaim state on the server after a network partition.
1794 * Assumes caller holds the appropriate lock
1795 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001796static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001798 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001799 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001801 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001802 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001803 if (IS_ERR(opendata))
1804 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001805 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001806 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001807 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001808 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001809 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001812static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001813{
Trond Myklebust539cd032007-06-05 11:46:42 -04001814 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001815 struct nfs4_exception exception = { };
1816 int err;
1817
1818 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001819 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001820 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1821 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001822 switch (err) {
1823 default:
1824 goto out;
1825 case -NFS4ERR_GRACE:
1826 case -NFS4ERR_DELAY:
1827 nfs4_handle_exception(server, err, &exception);
1828 err = 0;
1829 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001830 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001831out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001832 return err;
1833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1836{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001838 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Trond Myklebust864472e2006-01-03 09:55:15 +01001840 ctx = nfs4_state_find_open_context(state);
1841 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001842 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001843 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001844 put_nfs_open_context(ctx);
1845 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Trond Myklebust361eeee2014-10-17 15:10:25 +03001848static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
1849{
1850 nfs_remove_bad_delegation(state->inode);
1851 write_seqlock(&state->seqlock);
1852 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1853 write_sequnlock(&state->seqlock);
1854 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1855}
1856
1857static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
1858{
1859 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
1860 nfs_finish_clear_delegation_stateid(state);
1861}
1862
1863static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1864{
1865 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
1866 nfs40_clear_delegation_stateid(state);
1867 return nfs4_open_expired(sp, state);
1868}
1869
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001870#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001871static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001872{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001873 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001874 nfs4_stateid *stateid = &state->stateid;
1875 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001876
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001877 /* If a state reset has been done, test_stateid is unneeded */
1878 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1879 return;
1880
1881 status = nfs41_test_stateid(server, stateid);
1882 if (status != NFS_OK) {
1883 /* Free the stateid unless the server explicitly
1884 * informs us the stateid is unrecognized. */
1885 if (status != -NFS4ERR_BAD_STATEID)
1886 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001887 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001888
Bryan Schumaker69388672012-09-26 15:25:52 -04001889 write_seqlock(&state->seqlock);
1890 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1891 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001892 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1893 }
1894}
1895
1896/**
1897 * nfs41_check_open_stateid - possibly free an open stateid
1898 *
1899 * @state: NFSv4 state for an inode
1900 *
1901 * Returns NFS_OK if recovery for this stateid is now finished.
1902 * Otherwise a negative NFS4ERR value is returned.
1903 */
1904static int nfs41_check_open_stateid(struct nfs4_state *state)
1905{
1906 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001907 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001908 int status;
1909
1910 /* If a state reset has been done, test_stateid is unneeded */
1911 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1912 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1913 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1914 return -NFS4ERR_BAD_STATEID;
1915
1916 status = nfs41_test_stateid(server, stateid);
1917 if (status != NFS_OK) {
1918 /* Free the stateid unless the server explicitly
1919 * informs us the stateid is unrecognized. */
1920 if (status != -NFS4ERR_BAD_STATEID)
1921 nfs41_free_stateid(server, stateid);
1922
1923 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1924 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1925 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001926 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001927 }
1928 return status;
1929}
1930
1931static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1932{
Chuck Levereb64cf92012-07-11 16:30:05 -04001933 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001934
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001935 nfs41_clear_delegation_stateid(state);
1936 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001937 if (status != NFS_OK)
1938 status = nfs4_open_expired(sp, state);
1939 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001940}
1941#endif
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001944 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1945 * fields corresponding to attributes that were used to store the verifier.
1946 * Make sure we clobber those fields in the later setattr call
1947 */
1948static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1949{
1950 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1951 !(sattr->ia_valid & ATTR_ATIME_SET))
1952 sattr->ia_valid |= ATTR_ATIME;
1953
1954 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1955 !(sattr->ia_valid & ATTR_MTIME_SET))
1956 sattr->ia_valid |= ATTR_MTIME;
1957}
1958
Trond Myklebustc21443c2013-02-07 14:26:21 -05001959static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1960 fmode_t fmode,
1961 int flags,
1962 struct nfs4_state **res)
1963{
1964 struct nfs4_state_owner *sp = opendata->owner;
1965 struct nfs_server *server = sp->so_server;
1966 struct nfs4_state *state;
1967 unsigned int seq;
1968 int ret;
1969
1970 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1971
1972 ret = _nfs4_proc_open(opendata);
1973 if (ret != 0)
1974 goto out;
1975
1976 state = nfs4_opendata_to_nfs4_state(opendata);
1977 ret = PTR_ERR(state);
1978 if (IS_ERR(state))
1979 goto out;
1980 if (server->caps & NFS_CAP_POSIX_LOCK)
1981 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1982
1983 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1984 if (ret != 0)
1985 goto out;
1986
Trond Myklebust91876b12013-03-28 14:01:33 -04001987 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001988 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001989 *res = state;
1990out:
1991 return ret;
1992}
1993
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001994/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001995 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 */
Andy Adamson82be4172012-05-23 05:02:35 -04001997static int _nfs4_do_open(struct inode *dir,
1998 struct dentry *dentry,
1999 fmode_t fmode,
2000 int flags,
2001 struct iattr *sattr,
2002 struct rpc_cred *cred,
2003 struct nfs4_state **res,
2004 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 struct nfs4_state_owner *sp;
2007 struct nfs4_state *state = NULL;
2008 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002009 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002010 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002011 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002015 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2016 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2018 goto out_err;
2019 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002020 status = nfs4_recover_expired_lease(server);
2021 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002022 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002023 if (dentry->d_inode != NULL)
2024 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002025 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002026 if (dentry->d_inode)
2027 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002028 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002029 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002030 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002031 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Andy Adamson82be4172012-05-23 05:02:35 -04002033 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2034 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2035 if (!opendata->f_attr.mdsthreshold)
2036 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002037 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002038 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002039 if (dentry->d_inode != NULL)
2040 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002041
Trond Myklebustc21443c2013-02-07 14:26:21 -05002042 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002043 if (status != 0)
2044 goto err_opendata_put;
2045
Trond Myklebust549b19c2013-04-16 18:42:34 -04002046 if ((opendata->o_arg.open_flags & O_EXCL) &&
2047 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002048 nfs4_exclusive_attrset(opendata, sattr);
2049
2050 nfs_fattr_init(opendata->o_res.f_attr);
2051 status = nfs4_do_setattr(state->inode, cred,
2052 opendata->o_res.f_attr, sattr,
2053 state);
2054 if (status == 0)
2055 nfs_setattr_update_inode(state->inode, sattr);
2056 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2057 }
Andy Adamson82be4172012-05-23 05:02:35 -04002058
2059 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2060 *ctx_th = opendata->f_attr.mdsthreshold;
2061 else
2062 kfree(opendata->f_attr.mdsthreshold);
2063 opendata->f_attr.mdsthreshold = NULL;
2064
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002065 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 *res = state;
2068 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002069err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002070 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002071 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002072err_put_state_owner:
2073 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 *res = NULL;
2076 return status;
2077}
2078
2079
Andy Adamson82be4172012-05-23 05:02:35 -04002080static struct nfs4_state *nfs4_do_open(struct inode *dir,
2081 struct dentry *dentry,
2082 fmode_t fmode,
2083 int flags,
2084 struct iattr *sattr,
2085 struct rpc_cred *cred,
2086 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002088 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 struct nfs4_exception exception = { };
2090 struct nfs4_state *res;
2091 int status;
2092
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002093 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002095 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2096 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (status == 0)
2098 break;
2099 /* NOTE: BAD_SEQID means the server and client disagree about the
2100 * book-keeping w.r.t. state-changing operations
2101 * (OPEN/CLOSE/LOCK/LOCKU...)
2102 * It is actually a sign of a bug on the client or on the server.
2103 *
2104 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002105 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * have unhashed the old state_owner for us, and that we can
2107 * therefore safely retry using a new one. We should still warn
2108 * the user though...
2109 */
2110 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002111 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002112 " returned a bad sequence-id error!\n",
2113 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 exception.retry = 1;
2115 continue;
2116 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002117 /*
2118 * BAD_STATEID on OPEN means that the server cancelled our
2119 * state before it received the OPEN_CONFIRM.
2120 * Recover by retrying the request as per the discussion
2121 * on Page 181 of RFC3530.
2122 */
2123 if (status == -NFS4ERR_BAD_STATEID) {
2124 exception.retry = 1;
2125 continue;
2126 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002127 if (status == -EAGAIN) {
2128 /* We must have found a delegation */
2129 exception.retry = 1;
2130 continue;
2131 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002132 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2133 continue;
2134 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 status, &exception));
2136 } while (exception.retry);
2137 return res;
2138}
2139
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002140static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2141 struct nfs_fattr *fattr, struct iattr *sattr,
2142 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002144 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002146 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 .iap = sattr,
2148 .server = server,
2149 .bitmask = server->attr_bitmask,
2150 };
2151 struct nfs_setattrres res = {
2152 .fattr = fattr,
2153 .server = server,
2154 };
2155 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2157 .rpc_argp = &arg,
2158 .rpc_resp = &res,
2159 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002161 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002162 fmode_t fmode;
2163 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002164 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Trond Myklebust0e574af2005-10-27 22:12:38 -04002166 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Trond Myklebustee3ae842013-04-29 10:35:36 -04002168 /* Servers should only apply open mode checks for file size changes */
2169 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2170 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2171
2172 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2173 /* Use that stateid */
2174 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002175 struct nfs_lockowner lockowner = {
2176 .l_owner = current->files,
2177 .l_pid = current->tgid,
2178 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002179 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002180 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002181 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002182 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Bryan Schumaker7c513052011-03-24 17:12:24 +00002184 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002185 if (status == 0 && state != NULL)
2186 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002187 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002190static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2191 struct nfs_fattr *fattr, struct iattr *sattr,
2192 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002194 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002195 struct nfs4_exception exception = {
2196 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002197 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002198 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 int err;
2200 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002201 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2202 switch (err) {
2203 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002204 if (!(sattr->ia_valid & ATTR_SIZE)) {
2205 pr_warn_once("NFSv4: server %s is incorrectly "
2206 "applying open mode checks to "
2207 "a SETATTR that is not "
2208 "changing file size.\n",
2209 server->nfs_client->cl_hostname);
2210 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002211 if (state && !(state->state & FMODE_WRITE)) {
2212 err = -EBADF;
2213 if (sattr->ia_valid & ATTR_OPEN)
2214 err = -EACCES;
2215 goto out;
2216 }
2217 }
2218 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002220out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return err;
2222}
2223
2224struct nfs4_closedata {
2225 struct inode *inode;
2226 struct nfs4_state *state;
2227 struct nfs_closeargs arg;
2228 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002229 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002230 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002231 bool roc;
2232 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233};
2234
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002235static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002236{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002237 struct nfs4_closedata *calldata = data;
2238 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002239 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002240
Fred Isamanf7e89172011-01-06 11:36:32 +00002241 if (calldata->roc)
2242 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002243 nfs4_put_open_state(calldata->state);
2244 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002245 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002246 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002247 kfree(calldata);
2248}
2249
Trond Myklebust88069f72009-12-08 08:33:16 -05002250static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2251 fmode_t fmode)
2252{
2253 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002254 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002255 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2256 case FMODE_WRITE:
2257 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2258 break;
2259 case FMODE_READ:
2260 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2261 break;
2262 case 0:
2263 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2264 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2265 clear_bit(NFS_OPEN_STATE, &state->flags);
2266 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002267 spin_unlock(&state->owner->so_lock);
2268}
2269
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002270static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002272 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 struct nfs_server *server = NFS_SERVER(calldata->inode);
2275
Chuck Levera3ca5652012-03-01 17:00:40 -05002276 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002277 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2278 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 /* hmm. we are done with the inode, and in the process of freeing
2280 * the state_owner. we keep this around to process errors
2281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 switch (task->tk_status) {
2283 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002284 if (calldata->roc)
2285 pnfs_roc_set_barrier(state->inode,
2286 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002287 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002288 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002289 nfs4_close_clear_stateid_flags(state,
2290 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 break;
2292 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002293 case -NFS4ERR_OLD_STATEID:
2294 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002296 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002297 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002299 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2300 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002302 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002303 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002304 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002307static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002309 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002310 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002311 struct inode *inode = calldata->inode;
Trond Myklebust569ae352014-08-25 22:33:12 -04002312 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002313 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002314
Chuck Levera3ca5652012-03-01 17:00:40 -05002315 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002316 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002317 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002318
Trond Myklebust88069f72009-12-08 08:33:16 -05002319 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002320 spin_lock(&state->owner->so_lock);
Trond Myklebust569ae352014-08-25 22:33:12 -04002321 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2322 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2323 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04002324 /* Calculate the change in open mode */
Trond Myklebusta6ac6002014-09-18 11:51:32 -04002325 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002326 if (state->n_rdwr == 0) {
Trond Myklebusta6ac6002014-09-18 11:51:32 -04002327 if (state->n_rdonly == 0)
2328 call_close |= is_rdonly;
2329 else if (is_rdonly)
2330 calldata->arg.fmode |= FMODE_READ;
2331 if (state->n_wronly == 0)
2332 call_close |= is_wronly;
2333 else if (is_wronly)
2334 calldata->arg.fmode |= FMODE_WRITE;
2335 } else if (is_rdwr)
2336 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2337
2338 if (calldata->arg.fmode == 0)
2339 call_close |= is_rdwr;
2340
Trond Myklebust5d422302013-03-14 16:57:48 -04002341 if (!nfs4_valid_open_stateid(state))
2342 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002343 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002344
2345 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002346 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002347 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002348 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002349
Fred Isamanf7e89172011-01-06 11:36:32 +00002350 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002351 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002352 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002353 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2354 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002355 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002356 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002357 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002358
Trond Myklebust516a6af2005-10-27 22:12:41 -04002359 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002360 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002361 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002362 &calldata->arg.seq_args,
2363 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002364 task) != 0)
2365 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002366 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002367 return;
2368out_no_action:
2369 task->tk_action = NULL;
2370out_wait:
2371 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002374static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002375 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002376 .rpc_call_done = nfs4_close_done,
2377 .rpc_release = nfs4_free_closedata,
2378};
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380/*
2381 * It is possible for data to be read/written from a mem-mapped file
2382 * after the sys_close call (which hits the vfs layer as a flush).
2383 * This means that we can't safely call nfsv4 close on a file until
2384 * the inode is cleared. This in turn means that we are not good
2385 * NFSv4 citizens - we do not indicate to the server to update the file's
2386 * share state even when we are done with one of the three share
2387 * stateid's in the inode.
2388 *
2389 * NOTE: Caller must be holding the sp->so_owner semaphore!
2390 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002391int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002393 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002395 struct nfs4_state_owner *sp = state->owner;
2396 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002397 struct rpc_message msg = {
2398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2399 .rpc_cred = state->owner->so_cred,
2400 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002401 struct rpc_task_setup task_setup_data = {
2402 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002403 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002404 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002405 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002406 .flags = RPC_TASK_ASYNC,
2407 };
Trond Myklebust95121352005-10-18 14:20:12 -07002408 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002410 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002412 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002413 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002414 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002416 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002417 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002419 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002420 if (calldata->arg.seqid == NULL)
2421 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002422 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002423 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002424 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002425 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002426 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002427 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002428 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002429
Trond Myklebust1174dd12010-12-21 10:52:24 -05002430 msg.rpc_argp = &calldata->arg;
2431 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002432 task_setup_data.callback_data = calldata;
2433 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002434 if (IS_ERR(task))
2435 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002436 status = 0;
2437 if (wait)
2438 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002439 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002440 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002441out_free_calldata:
2442 kfree(calldata);
2443out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002444 nfs4_put_open_state(state);
2445 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002446 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
Trond Myklebust2b484292010-09-17 10:56:51 -04002449static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002450nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 struct nfs4_state *state;
2453
Trond Myklebust565277f2007-10-15 18:17:53 -04002454 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002455 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2456 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002457 if (IS_ERR(state))
2458 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002459 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002460 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Trond Myklebust1185a552009-12-03 15:54:02 -05002463static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002464{
2465 if (ctx->state == NULL)
2466 return;
2467 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002468 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002469 else
Al Viro643168c2011-06-22 18:20:23 -04002470 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002471}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2474{
Benny Halevy43652ad2009-04-01 09:21:54 -04002475 struct nfs4_server_caps_arg args = {
2476 .fhandle = fhandle,
2477 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 struct nfs4_server_caps_res res = {};
2479 struct rpc_message msg = {
2480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002481 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 .rpc_resp = &res,
2483 };
2484 int status;
2485
Bryan Schumaker7c513052011-03-24 17:12:24 +00002486 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (status == 0) {
2488 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002489 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2490 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2491 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2492 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2493 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2495 server->caps |= NFS_CAP_ACLS;
2496 if (res.has_links != 0)
2497 server->caps |= NFS_CAP_HARDLINKS;
2498 if (res.has_symlinks != 0)
2499 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002500 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2501 server->caps |= NFS_CAP_FILEID;
2502 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2503 server->caps |= NFS_CAP_MODE;
2504 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2505 server->caps |= NFS_CAP_NLINK;
2506 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2507 server->caps |= NFS_CAP_OWNER;
2508 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2509 server->caps |= NFS_CAP_OWNER_GROUP;
2510 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2511 server->caps |= NFS_CAP_ATIME;
2512 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2513 server->caps |= NFS_CAP_CTIME;
2514 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2515 server->caps |= NFS_CAP_MTIME;
2516
Trond Myklebusta65318b2009-03-11 14:10:28 -04002517 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2518 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2519 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002521 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return status;
2525}
2526
Trond Myklebust55a97592006-06-09 09:34:19 -04002527int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
2529 struct nfs4_exception exception = { };
2530 int err;
2531 do {
2532 err = nfs4_handle_exception(server,
2533 _nfs4_server_capabilities(server, fhandle),
2534 &exception);
2535 } while (exception.retry);
2536 return err;
2537}
2538
2539static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2540 struct nfs_fsinfo *info)
2541{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 struct nfs4_lookup_root_arg args = {
2543 .bitmask = nfs4_fattr_bitmap,
2544 };
2545 struct nfs4_lookup_res res = {
2546 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002547 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 .fh = fhandle,
2549 };
2550 struct rpc_message msg = {
2551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2552 .rpc_argp = &args,
2553 .rpc_resp = &res,
2554 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002555
Trond Myklebust0e574af2005-10-27 22:12:38 -04002556 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002557 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
2560static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2561 struct nfs_fsinfo *info)
2562{
2563 struct nfs4_exception exception = { };
2564 int err;
2565 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002566 err = _nfs4_lookup_root(server, fhandle, info);
2567 switch (err) {
2568 case 0:
2569 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002570 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002571 default:
2572 err = nfs4_handle_exception(server, err, &exception);
2573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002575out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return err;
2577}
2578
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002579static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2580 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2581{
2582 struct rpc_auth *auth;
2583 int ret;
2584
2585 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002586 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002587 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002588 goto out;
2589 }
2590 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002591out:
2592 return ret;
2593}
2594
Chuck Lever9a744ba2013-03-16 15:56:02 -04002595/*
2596 * Retry pseudoroot lookup with various security flavors. We do this when:
2597 *
2598 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2599 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2600 *
2601 * Returns zero on success, or a negative NFS4ERR value, or a
2602 * negative errno value.
2603 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002604static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002605 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002607 /* Per 3530bis 15.33.5 */
2608 static const rpc_authflavor_t flav_array[] = {
2609 RPC_AUTH_GSS_KRB5P,
2610 RPC_AUTH_GSS_KRB5I,
2611 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002612 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002613 RPC_AUTH_NULL,
2614 };
2615 int status = -EPERM;
2616 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Chuck Lever9a744ba2013-03-16 15:56:02 -04002618 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002619 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002620 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002621 continue;
2622 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002623 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002624
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002625 /*
2626 * -EACCESS could mean that the user doesn't have correct permissions
2627 * to access the mount. It could also mean that we tried to mount
2628 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2629 * existing mount programs don't handle -EACCES very well so it should
2630 * be mapped to -EPERM instead.
2631 */
2632 if (status == -EACCES)
2633 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002634 return status;
2635}
2636
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002637static int nfs4_do_find_root_sec(struct nfs_server *server,
2638 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2639{
2640 int mv = server->nfs_client->cl_minorversion;
2641 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2642}
2643
2644/**
2645 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2646 * @server: initialized nfs_server handle
2647 * @fhandle: we fill in the pseudo-fs root file handle
2648 * @info: we fill in an FSINFO struct
2649 *
2650 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002651 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002652int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2653 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002654{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002655 int status;
2656
2657 status = nfs4_lookup_root(server, fhandle, info);
2658 if ((status == -NFS4ERR_WRONGSEC) &&
2659 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2660 status = nfs4_do_find_root_sec(server, fhandle, info);
2661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (status == 0)
2663 status = nfs4_server_capabilities(server, fhandle);
2664 if (status == 0)
2665 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002666
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002667 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
2669
Bryan Schumakerbae36242012-05-10 15:07:31 -04002670static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2671 struct nfs_fsinfo *info)
2672{
2673 int error;
2674 struct nfs_fattr *fattr = info->fattr;
2675
2676 error = nfs4_server_capabilities(server, mntfh);
2677 if (error < 0) {
2678 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2679 return error;
2680 }
2681
2682 error = nfs4_proc_getattr(server, mntfh, fattr);
2683 if (error < 0) {
2684 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2685 return error;
2686 }
2687
2688 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2689 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2690 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2691
2692 return error;
2693}
2694
Manoj Naik6b97fd32006-06-09 09:34:29 -04002695/*
2696 * Get locations and (maybe) other attributes of a referral.
2697 * Note that we'll actually follow the referral later when
2698 * we detect fsid mismatch in inode revalidation
2699 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002700static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2701 const struct qstr *name, struct nfs_fattr *fattr,
2702 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002703{
2704 int status = -ENOMEM;
2705 struct page *page = NULL;
2706 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002707
2708 page = alloc_page(GFP_KERNEL);
2709 if (page == NULL)
2710 goto out;
2711 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2712 if (locations == NULL)
2713 goto out;
2714
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002715 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002716 if (status != 0)
2717 goto out;
2718 /* Make sure server returned a different fsid for the referral */
2719 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002720 dprintk("%s: server did not return a different fsid for"
2721 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002722 status = -EIO;
2723 goto out;
2724 }
Andy Adamson533eb462011-06-13 18:25:56 -04002725 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2726 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002727
Andy Adamson533eb462011-06-13 18:25:56 -04002728 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002729 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002730 memset(fhandle, 0, sizeof(struct nfs_fh));
2731out:
2732 if (page)
2733 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002734 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002735 return status;
2736}
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2739{
2740 struct nfs4_getattr_arg args = {
2741 .fh = fhandle,
2742 .bitmask = server->attr_bitmask,
2743 };
2744 struct nfs4_getattr_res res = {
2745 .fattr = fattr,
2746 .server = server,
2747 };
2748 struct rpc_message msg = {
2749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2750 .rpc_argp = &args,
2751 .rpc_resp = &res,
2752 };
2753
Trond Myklebust0e574af2005-10-27 22:12:38 -04002754 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002755 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756}
2757
2758static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2759{
2760 struct nfs4_exception exception = { };
2761 int err;
2762 do {
2763 err = nfs4_handle_exception(server,
2764 _nfs4_proc_getattr(server, fhandle, fattr),
2765 &exception);
2766 } while (exception.retry);
2767 return err;
2768}
2769
2770/*
2771 * The file is not closed if it is opened due to the a request to change
2772 * the size of the file. The open call will not be needed once the
2773 * VFS layer lookup-intents are implemented.
2774 *
2775 * Close is called when the inode is destroyed.
2776 * If we haven't opened the file for O_WRONLY, we
2777 * need to in the size_change case to obtain a stateid.
2778 *
2779 * Got race?
2780 * Because OPEN is always done by name in nfsv4, it is
2781 * possible that we opened a different file by the same
2782 * name. We can recognize this race condition, but we
2783 * can't do anything about it besides returning an error.
2784 *
2785 * This will be fixed with VFS changes (lookup-intent).
2786 */
2787static int
2788nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2789 struct iattr *sattr)
2790{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002791 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002792 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002793 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 int status;
2795
Benny Halevy8a1636c2010-07-14 15:43:57 -04002796 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002797 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002798
Trond Myklebust0e574af2005-10-27 22:12:38 -04002799 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Andy Adamson26699402012-05-30 16:12:24 -04002801 /* Deal with open(O_TRUNC) */
2802 if (sattr->ia_valid & ATTR_OPEN)
2803 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2804
2805 /* Optimization: if the end result is no change, don't RPC */
2806 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2807 return 0;
2808
Trond Myklebustd5308382005-11-04 15:33:38 -05002809 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002810 if (sattr->ia_valid & ATTR_FILE) {
2811 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002812
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002813 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002814 if (ctx) {
2815 cred = ctx->cred;
2816 state = ctx->state;
2817 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002818 }
2819
2820 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002821 if (status == 0)
2822 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return status;
2824}
2825
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002826static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2827 const struct qstr *name, struct nfs_fh *fhandle,
2828 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002829{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002830 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002831 int status;
2832 struct nfs4_lookup_arg args = {
2833 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002834 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002835 .name = name,
2836 };
2837 struct nfs4_lookup_res res = {
2838 .server = server,
2839 .fattr = fattr,
2840 .fh = fhandle,
2841 };
2842 struct rpc_message msg = {
2843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2844 .rpc_argp = &args,
2845 .rpc_resp = &res,
2846 };
2847
2848 nfs_fattr_init(fattr);
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002851 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 dprintk("NFS reply lookup: %d\n", status);
2853 return status;
2854}
2855
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002856static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002857{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002858 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002859 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002860 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2861 fattr->nlink = 2;
2862}
2863
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002864static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2865 struct qstr *name, struct nfs_fh *fhandle,
2866 struct nfs_fattr *fattr)
2867{
2868 struct nfs4_exception exception = { };
2869 struct rpc_clnt *client = *clnt;
2870 int err;
2871 do {
2872 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2873 switch (err) {
2874 case -NFS4ERR_BADNAME:
2875 err = -ENOENT;
2876 goto out;
2877 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002878 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002879 goto out;
2880 case -NFS4ERR_WRONGSEC:
2881 err = -EPERM;
2882 if (client != *clnt)
2883 goto out;
2884
2885 client = nfs4_create_sec_client(client, dir, name);
2886 if (IS_ERR(client))
2887 return PTR_ERR(client);
2888
2889 exception.retry = 1;
2890 break;
2891 default:
2892 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2893 }
2894 } while (exception.retry);
2895
2896out:
2897 if (err == 0)
2898 *clnt = client;
2899 else if (client != *clnt)
2900 rpc_shutdown_client(client);
2901
2902 return err;
2903}
2904
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002905static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002906 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002908 int status;
2909 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002910
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002911 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2912 if (client != NFS_CLIENT(dir)) {
2913 rpc_shutdown_client(client);
2914 nfs_fixup_secinfo_attributes(fattr);
2915 }
2916 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917}
2918
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002919struct rpc_clnt *
2920nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2921 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2922{
2923 int status;
2924 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2925
2926 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2927 if (status < 0) {
2928 rpc_shutdown_client(client);
2929 return ERR_PTR(status);
2930 }
2931 return client;
2932}
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2935{
Trond Myklebust76b32992007-08-10 17:45:11 -04002936 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 struct nfs4_accessargs args = {
2938 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002939 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002941 struct nfs4_accessres res = {
2942 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002943 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 struct rpc_message msg = {
2945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2946 .rpc_argp = &args,
2947 .rpc_resp = &res,
2948 .rpc_cred = entry->cred,
2949 };
2950 int mode = entry->mask;
2951 int status;
2952
2953 /*
2954 * Determine which access bits we want to ask for...
2955 */
2956 if (mode & MAY_READ)
2957 args.access |= NFS4_ACCESS_READ;
2958 if (S_ISDIR(inode->i_mode)) {
2959 if (mode & MAY_WRITE)
2960 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2961 if (mode & MAY_EXEC)
2962 args.access |= NFS4_ACCESS_LOOKUP;
2963 } else {
2964 if (mode & MAY_WRITE)
2965 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2966 if (mode & MAY_EXEC)
2967 args.access |= NFS4_ACCESS_EXECUTE;
2968 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002969
2970 res.fattr = nfs_alloc_fattr();
2971 if (res.fattr == NULL)
2972 return -ENOMEM;
2973
Bryan Schumaker7c513052011-03-24 17:12:24 +00002974 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002976 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002977 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002979 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return status;
2981}
2982
2983static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2984{
2985 struct nfs4_exception exception = { };
2986 int err;
2987 do {
2988 err = nfs4_handle_exception(NFS_SERVER(inode),
2989 _nfs4_proc_access(inode, entry),
2990 &exception);
2991 } while (exception.retry);
2992 return err;
2993}
2994
2995/*
2996 * TODO: For the time being, we don't try to get any attributes
2997 * along with any of the zero-copy operations READ, READDIR,
2998 * READLINK, WRITE.
2999 *
3000 * In the case of the first three, we want to put the GETATTR
3001 * after the read-type operation -- this is because it is hard
3002 * to predict the length of a GETATTR response in v4, and thus
3003 * align the READ data correctly. This means that the GETATTR
3004 * may end up partially falling into the page cache, and we should
3005 * shift it into the 'tail' of the xdr_buf before processing.
3006 * To do this efficiently, we need to know the total length
3007 * of data received, which doesn't seem to be available outside
3008 * of the RPC layer.
3009 *
3010 * In the case of WRITE, we also want to put the GETATTR after
3011 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003012 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 *
3014 * Both of these changes to the XDR layer would in fact be quite
3015 * minor, but I decided to leave them for a subsequent patch.
3016 */
3017static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3018 unsigned int pgbase, unsigned int pglen)
3019{
3020 struct nfs4_readlink args = {
3021 .fh = NFS_FH(inode),
3022 .pgbase = pgbase,
3023 .pglen = pglen,
3024 .pages = &page,
3025 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003026 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 struct rpc_message msg = {
3028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3029 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003030 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 };
3032
Bryan Schumaker7c513052011-03-24 17:12:24 +00003033 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 -07003034}
3035
3036static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3037 unsigned int pgbase, unsigned int pglen)
3038{
3039 struct nfs4_exception exception = { };
3040 int err;
3041 do {
3042 err = nfs4_handle_exception(NFS_SERVER(inode),
3043 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3044 &exception);
3045 } while (exception.retry);
3046 return err;
3047}
3048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003050 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052static int
3053nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003054 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003056 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 int status = 0;
3059
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003060 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3061 if (IS_ERR(ctx))
3062 return PTR_ERR(ctx);
3063
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003064 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003065 state = nfs4_do_open(dir, dentry, ctx->mode,
3066 flags, sattr, ctx->cred,
3067 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003068 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 if (IS_ERR(state)) {
3070 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003071 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003073 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003074 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003075 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003077 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return status;
3079}
3080
3081static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3082{
Trond Myklebust16e42952005-10-27 22:12:44 -04003083 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003084 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003086 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003088 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003089 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003090 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3093 .rpc_argp = &args,
3094 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003096 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003097
Bryan Schumaker7c513052011-03-24 17:12:24 +00003098 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003099 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003100 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return status;
3102}
3103
3104static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3105{
3106 struct nfs4_exception exception = { };
3107 int err;
3108 do {
3109 err = nfs4_handle_exception(NFS_SERVER(dir),
3110 _nfs4_proc_remove(dir, name),
3111 &exception);
3112 } while (exception.retry);
3113 return err;
3114}
3115
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003116static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003118 struct nfs_server *server = NFS_SERVER(dir);
3119 struct nfs_removeargs *args = msg->rpc_argp;
3120 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003122 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003124 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003127static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3128{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003129 nfs4_setup_sequence(NFS_SERVER(data->dir),
3130 &data->args.seq_args,
3131 &data->res.seq_res,
3132 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133}
3134
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003135static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003137 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3138
Trond Myklebust14516c32010-07-31 14:29:06 -04003139 if (!nfs4_sequence_done(task, &res->seq_res))
3140 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003141 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003142 return 0;
3143 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003144 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
Jeff Laytond3d41522010-09-17 17:31:57 -04003147static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3148{
3149 struct nfs_server *server = NFS_SERVER(dir);
3150 struct nfs_renameargs *arg = msg->rpc_argp;
3151 struct nfs_renameres *res = msg->rpc_resp;
3152
3153 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003154 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003155 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003156}
3157
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003158static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3159{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003160 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3161 &data->args.seq_args,
3162 &data->res.seq_res,
3163 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003164}
3165
3166static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3167 struct inode *new_dir)
3168{
3169 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3170
3171 if (!nfs4_sequence_done(task, &res->seq_res))
3172 return 0;
3173 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3174 return 0;
3175
3176 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003177 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003178 return 1;
3179}
3180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3182 struct inode *new_dir, struct qstr *new_name)
3183{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003184 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003185 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 .old_dir = NFS_FH(old_dir),
3187 .new_dir = NFS_FH(new_dir),
3188 .old_name = old_name,
3189 .new_name = new_name,
3190 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003191 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003192 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003193 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 struct rpc_message msg = {
3195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3196 .rpc_argp = &arg,
3197 .rpc_resp = &res,
3198 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003199 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Bryan Schumaker7c513052011-03-24 17:12:24 +00003201 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (!status) {
3203 update_changeattr(old_dir, &res.old_cinfo);
3204 update_changeattr(new_dir, &res.new_cinfo);
3205 }
3206 return status;
3207}
3208
3209static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3210 struct inode *new_dir, struct qstr *new_name)
3211{
3212 struct nfs4_exception exception = { };
3213 int err;
3214 do {
3215 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3216 _nfs4_proc_rename(old_dir, old_name,
3217 new_dir, new_name),
3218 &exception);
3219 } while (exception.retry);
3220 return err;
3221}
3222
3223static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3224{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003225 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 struct nfs4_link_arg arg = {
3227 .fh = NFS_FH(inode),
3228 .dir_fh = NFS_FH(dir),
3229 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003230 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003232 struct nfs4_link_res res = {
3233 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003234 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 struct rpc_message msg = {
3236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3237 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003238 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003240 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Trond Myklebust136f2622010-04-16 16:22:49 -04003242 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003243 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003244 goto out;
3245
Bryan Schumaker7c513052011-03-24 17:12:24 +00003246 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003247 if (!status) {
3248 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003249 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003250 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003251out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003252 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 return status;
3254}
3255
3256static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3257{
3258 struct nfs4_exception exception = { };
3259 int err;
3260 do {
3261 err = nfs4_handle_exception(NFS_SERVER(inode),
3262 _nfs4_proc_link(inode, dir, name),
3263 &exception);
3264 } while (exception.retry);
3265 return err;
3266}
3267
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003268struct nfs4_createdata {
3269 struct rpc_message msg;
3270 struct nfs4_create_arg arg;
3271 struct nfs4_create_res res;
3272 struct nfs_fh fh;
3273 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003274};
3275
3276static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3277 struct qstr *name, struct iattr *sattr, u32 ftype)
3278{
3279 struct nfs4_createdata *data;
3280
3281 data = kzalloc(sizeof(*data), GFP_KERNEL);
3282 if (data != NULL) {
3283 struct nfs_server *server = NFS_SERVER(dir);
3284
3285 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3286 data->msg.rpc_argp = &data->arg;
3287 data->msg.rpc_resp = &data->res;
3288 data->arg.dir_fh = NFS_FH(dir);
3289 data->arg.server = server;
3290 data->arg.name = name;
3291 data->arg.attrs = sattr;
3292 data->arg.ftype = ftype;
3293 data->arg.bitmask = server->attr_bitmask;
3294 data->res.server = server;
3295 data->res.fh = &data->fh;
3296 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003297 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003298 }
3299 return data;
3300}
3301
3302static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3303{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003304 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003305 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003306 if (status == 0) {
3307 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003308 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3309 }
3310 return status;
3311}
3312
3313static void nfs4_free_createdata(struct nfs4_createdata *data)
3314{
3315 kfree(data);
3316}
3317
Chuck Lever4f390c12006-08-22 20:06:22 -04003318static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003319 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003321 struct nfs4_createdata *data;
3322 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Chuck Lever94a6d752006-08-22 20:06:23 -04003324 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003325 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003326
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003327 status = -ENOMEM;
3328 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3329 if (data == NULL)
3330 goto out;
3331
3332 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3333 data->arg.u.symlink.pages = &page;
3334 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003336 status = nfs4_do_create(dir, dentry, data);
3337
3338 nfs4_free_createdata(data);
3339out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return status;
3341}
3342
Chuck Lever4f390c12006-08-22 20:06:22 -04003343static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003344 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345{
3346 struct nfs4_exception exception = { };
3347 int err;
3348 do {
3349 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003350 _nfs4_proc_symlink(dir, dentry, page,
3351 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 &exception);
3353 } while (exception.retry);
3354 return err;
3355}
3356
3357static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3358 struct iattr *sattr)
3359{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003360 struct nfs4_createdata *data;
3361 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003363 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3364 if (data == NULL)
3365 goto out;
3366
3367 status = nfs4_do_create(dir, dentry, data);
3368
3369 nfs4_free_createdata(data);
3370out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 return status;
3372}
3373
3374static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3375 struct iattr *sattr)
3376{
3377 struct nfs4_exception exception = { };
3378 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003379
3380 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 do {
3382 err = nfs4_handle_exception(NFS_SERVER(dir),
3383 _nfs4_proc_mkdir(dir, dentry, sattr),
3384 &exception);
3385 } while (exception.retry);
3386 return err;
3387}
3388
3389static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003390 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
3392 struct inode *dir = dentry->d_inode;
3393 struct nfs4_readdir_arg args = {
3394 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003395 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 .pgbase = 0,
3397 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003398 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003399 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 };
3401 struct nfs4_readdir_res res;
3402 struct rpc_message msg = {
3403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3404 .rpc_argp = &args,
3405 .rpc_resp = &res,
3406 .rpc_cred = cred,
3407 };
3408 int status;
3409
Harvey Harrison3110ff82008-05-02 13:42:44 -07003410 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003411 dentry->d_parent->d_name.name,
3412 dentry->d_name.name,
3413 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003414 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003416 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 -05003417 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003418 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003419 status += args.pgbase;
3420 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003421
3422 nfs_invalidate_atime(dir);
3423
Harvey Harrison3110ff82008-05-02 13:42:44 -07003424 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return status;
3426}
3427
3428static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003429 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
3431 struct nfs4_exception exception = { };
3432 int err;
3433 do {
3434 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3435 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003436 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 &exception);
3438 } while (exception.retry);
3439 return err;
3440}
3441
3442static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3443 struct iattr *sattr, dev_t rdev)
3444{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003445 struct nfs4_createdata *data;
3446 int mode = sattr->ia_mode;
3447 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003449 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3450 if (data == NULL)
3451 goto out;
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003454 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003456 data->arg.ftype = NF4BLK;
3457 data->arg.u.device.specdata1 = MAJOR(rdev);
3458 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 }
3460 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003461 data->arg.ftype = NF4CHR;
3462 data->arg.u.device.specdata1 = MAJOR(rdev);
3463 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003464 } else if (!S_ISSOCK(mode)) {
3465 status = -EINVAL;
3466 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003469 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003470out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003471 nfs4_free_createdata(data);
3472out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 return status;
3474}
3475
3476static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3477 struct iattr *sattr, dev_t rdev)
3478{
3479 struct nfs4_exception exception = { };
3480 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003481
3482 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 do {
3484 err = nfs4_handle_exception(NFS_SERVER(dir),
3485 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3486 &exception);
3487 } while (exception.retry);
3488 return err;
3489}
3490
3491static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3492 struct nfs_fsstat *fsstat)
3493{
3494 struct nfs4_statfs_arg args = {
3495 .fh = fhandle,
3496 .bitmask = server->attr_bitmask,
3497 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003498 struct nfs4_statfs_res res = {
3499 .fsstat = fsstat,
3500 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 struct rpc_message msg = {
3502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3503 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003504 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 };
3506
Trond Myklebust0e574af2005-10-27 22:12:38 -04003507 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003508 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509}
3510
3511static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3512{
3513 struct nfs4_exception exception = { };
3514 int err;
3515 do {
3516 err = nfs4_handle_exception(server,
3517 _nfs4_proc_statfs(server, fhandle, fsstat),
3518 &exception);
3519 } while (exception.retry);
3520 return err;
3521}
3522
3523static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3524 struct nfs_fsinfo *fsinfo)
3525{
3526 struct nfs4_fsinfo_arg args = {
3527 .fh = fhandle,
3528 .bitmask = server->attr_bitmask,
3529 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003530 struct nfs4_fsinfo_res res = {
3531 .fsinfo = fsinfo,
3532 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 struct rpc_message msg = {
3534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3535 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003536 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 };
3538
Bryan Schumaker7c513052011-03-24 17:12:24 +00003539 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
3541
3542static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3543{
3544 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003545 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 int err;
3547
3548 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003549 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3550 if (err == 0) {
3551 struct nfs_client *clp = server->nfs_client;
3552
3553 spin_lock(&clp->cl_lock);
3554 clp->cl_lease_time = fsinfo->lease_time * HZ;
3555 clp->cl_last_renewal = now;
3556 spin_unlock(&clp->cl_lock);
3557 break;
3558 }
3559 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 } while (exception.retry);
3561 return err;
3562}
3563
3564static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3565{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003566 int error;
3567
Trond Myklebust0e574af2005-10-27 22:12:38 -04003568 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003569 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003570 if (error == 0) {
3571 /* block layout checks this! */
3572 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003573 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003574 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003575
3576 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
3579static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3580 struct nfs_pathconf *pathconf)
3581{
3582 struct nfs4_pathconf_arg args = {
3583 .fh = fhandle,
3584 .bitmask = server->attr_bitmask,
3585 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003586 struct nfs4_pathconf_res res = {
3587 .pathconf = pathconf,
3588 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 struct rpc_message msg = {
3590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3591 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003592 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 };
3594
3595 /* None of the pathconf attributes are mandatory to implement */
3596 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3597 memset(pathconf, 0, sizeof(*pathconf));
3598 return 0;
3599 }
3600
Trond Myklebust0e574af2005-10-27 22:12:38 -04003601 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003602 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
3605static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3606 struct nfs_pathconf *pathconf)
3607{
3608 struct nfs4_exception exception = { };
3609 int err;
3610
3611 do {
3612 err = nfs4_handle_exception(server,
3613 _nfs4_proc_pathconf(server, fhandle, pathconf),
3614 &exception);
3615 } while (exception.retry);
3616 return err;
3617}
3618
Trond Myklebust5521abf2013-03-16 20:54:34 -04003619int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003620 const struct nfs_open_context *ctx,
3621 const struct nfs_lock_context *l_ctx,
3622 fmode_t fmode)
3623{
3624 const struct nfs_lockowner *lockowner = NULL;
3625
3626 if (l_ctx != NULL)
3627 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003628 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003629}
3630EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3631
Trond Myklebust5521abf2013-03-16 20:54:34 -04003632static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3633 const struct nfs_open_context *ctx,
3634 const struct nfs_lock_context *l_ctx,
3635 fmode_t fmode)
3636{
3637 nfs4_stateid current_stateid;
3638
Trond Myklebustc8dd8fd2014-03-05 08:44:23 -05003639 /* If the current stateid represents a lost lock, then exit */
3640 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
3641 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003642 return nfs4_stateid_match(stateid, &current_stateid);
3643}
3644
3645static bool nfs4_error_stateid_expired(int err)
3646{
3647 switch (err) {
3648 case -NFS4ERR_DELEG_REVOKED:
3649 case -NFS4ERR_ADMIN_REVOKED:
3650 case -NFS4ERR_BAD_STATEID:
3651 case -NFS4ERR_STALE_STATEID:
3652 case -NFS4ERR_OLD_STATEID:
3653 case -NFS4ERR_OPENMODE:
3654 case -NFS4ERR_EXPIRED:
3655 return true;
3656 }
3657 return false;
3658}
3659
Benny Halevyd20581a2011-05-22 19:52:03 +03003660void __nfs4_read_done_cb(struct nfs_read_data *data)
3661{
Fred Isamancd841602012-04-20 14:47:44 -04003662 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003663}
3664
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003665static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666{
Fred Isamancd841602012-04-20 14:47:44 -04003667 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003669 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003670 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003671 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003673
Benny Halevyd20581a2011-05-22 19:52:03 +03003674 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003676 renew_lease(server, data->timestamp);
3677 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
3679
Trond Myklebust5521abf2013-03-16 20:54:34 -04003680static bool nfs4_read_stateid_changed(struct rpc_task *task,
3681 struct nfs_readargs *args)
3682{
3683
3684 if (!nfs4_error_stateid_expired(task->tk_status) ||
3685 nfs4_stateid_is_current(&args->stateid,
3686 args->context,
3687 args->lock_context,
3688 FMODE_READ))
3689 return false;
3690 rpc_restart_call_prepare(task);
3691 return true;
3692}
3693
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003694static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3695{
3696
3697 dprintk("--> %s\n", __func__);
3698
3699 if (!nfs4_sequence_done(task, &data->res.seq_res))
3700 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003701 if (nfs4_read_stateid_changed(task, &data->args))
3702 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003703 return data->read_done_cb ? data->read_done_cb(task, data) :
3704 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003705}
3706
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003707static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003710 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003711 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003712 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713}
3714
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003715static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3716{
Trond Myklebust9b206142013-03-17 15:52:00 -04003717 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003718 &data->args.seq_args,
3719 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003720 task))
3721 return;
3722 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3723 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724}
3725
Fred Isamanb029bc92011-03-03 15:13:42 +00003726static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727{
Fred Isamancd841602012-04-20 14:47:44 -04003728 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003730 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003731 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003732 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003734 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003736 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003737 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739}
3740
Trond Myklebust5521abf2013-03-16 20:54:34 -04003741static bool nfs4_write_stateid_changed(struct rpc_task *task,
3742 struct nfs_writeargs *args)
3743{
3744
3745 if (!nfs4_error_stateid_expired(task->tk_status) ||
3746 nfs4_stateid_is_current(&args->stateid,
3747 args->context,
3748 args->lock_context,
3749 FMODE_WRITE))
3750 return false;
3751 rpc_restart_call_prepare(task);
3752 return true;
3753}
3754
Fred Isamanb029bc92011-03-03 15:13:42 +00003755static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3756{
3757 if (!nfs4_sequence_done(task, &data->res.seq_res))
3758 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003759 if (nfs4_write_stateid_changed(task, &data->args))
3760 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003761 return data->write_done_cb ? data->write_done_cb(task, data) :
3762 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003763}
3764
Trond Myklebust5a37f852012-04-28 14:55:16 -04003765static
3766bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003767{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003768 const struct nfs_pgio_header *hdr = data->header;
3769
3770 /* Don't request attributes for pNFS or O_DIRECT writes */
3771 if (data->ds_clp != NULL || hdr->dreq != NULL)
3772 return false;
3773 /* Otherwise, request attributes if and only if we don't hold
3774 * a delegation
3775 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003776 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003777}
Fred Isamana69aef12011-03-03 15:13:47 +00003778
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003779static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
Fred Isamancd841602012-04-20 14:47:44 -04003781 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003782
Trond Myklebust5a37f852012-04-28 14:55:16 -04003783 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003784 data->args.bitmask = NULL;
3785 data->res.fattr = NULL;
3786 } else
3787 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003788
Fred Isamanb029bc92011-03-03 15:13:42 +00003789 if (!data->write_done_cb)
3790 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003791 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 data->timestamp = jiffies;
3793
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003794 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003795 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796}
3797
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003798static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3799{
Trond Myklebust9b206142013-03-17 15:52:00 -04003800 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003801 &data->args.seq_args,
3802 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003803 task))
3804 return;
3805 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3806 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
Fred Isaman0b7c0152012-04-20 14:47:39 -04003809static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3810{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003811 nfs4_setup_sequence(NFS_SERVER(data->inode),
3812 &data->args.seq_args,
3813 &data->res.seq_res,
3814 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815}
3816
Fred Isaman0b7c0152012-04-20 14:47:39 -04003817static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003820
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003821 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003822 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003823 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826}
3827
Fred Isaman0b7c0152012-04-20 14:47:39 -04003828static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003829{
3830 if (!nfs4_sequence_done(task, &data->res.seq_res))
3831 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003832 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003833}
3834
Fred Isaman0b7c0152012-04-20 14:47:39 -04003835static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003837 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003838
Fred Isaman0b7c0152012-04-20 14:47:39 -04003839 if (data->commit_done_cb == NULL)
3840 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003841 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003842 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003843 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844}
3845
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003846struct nfs4_renewdata {
3847 struct nfs_client *client;
3848 unsigned long timestamp;
3849};
3850
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851/*
3852 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3853 * standalone procedure for queueing an asynchronous RENEW.
3854 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003855static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003856{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003857 struct nfs4_renewdata *data = calldata;
3858 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003859
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003860 if (atomic_read(&clp->cl_count) > 1)
3861 nfs4_schedule_state_renewal(clp);
3862 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003863 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003864}
3865
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003866static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003868 struct nfs4_renewdata *data = calldata;
3869 struct nfs_client *clp = data->client;
3870 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
3872 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003873 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003874 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3875 return;
3876 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003877 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003878 return;
3879 }
3880 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003882 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883}
3884
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003885static const struct rpc_call_ops nfs4_renew_ops = {
3886 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003887 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003888};
3889
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003890static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
3892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3894 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003895 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003897 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003899 if (renew_flags == 0)
3900 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003901 if (!atomic_inc_not_zero(&clp->cl_count))
3902 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003903 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003904 if (data == NULL)
3905 return -ENOMEM;
3906 data->client = clp;
3907 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003908 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003909 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910}
3911
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003912static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913{
3914 struct rpc_message msg = {
3915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3916 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003917 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 };
3919 unsigned long now = jiffies;
3920 int status;
3921
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003922 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 if (status < 0)
3924 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003925 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 return 0;
3927}
3928
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003929static inline int nfs4_server_supports_acls(struct nfs_server *server)
3930{
3931 return (server->caps & NFS_CAP_ACLS)
3932 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3933 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3934}
3935
Trond Myklebust21f498c2012-08-24 10:59:25 -04003936/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3937 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003938 * the stack.
3939 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003940#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003941
Neil Hormane9e3d722011-03-04 19:26:03 -05003942static int buf_to_pages_noslab(const void *buf, size_t buflen,
3943 struct page **pages, unsigned int *pgbase)
3944{
3945 struct page *newpage, **spages;
3946 int rc = 0;
3947 size_t len;
3948 spages = pages;
3949
3950 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003951 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003952 newpage = alloc_page(GFP_KERNEL);
3953
3954 if (newpage == NULL)
3955 goto unwind;
3956 memcpy(page_address(newpage), buf, len);
3957 buf += len;
3958 buflen -= len;
3959 *pages++ = newpage;
3960 rc++;
3961 } while (buflen != 0);
3962
3963 return rc;
3964
3965unwind:
3966 for(; rc > 0; rc--)
3967 __free_page(spages[rc-1]);
3968 return -ENOMEM;
3969}
3970
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003971struct nfs4_cached_acl {
3972 int cached;
3973 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003974 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003975};
3976
3977static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003978{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003979 struct nfs_inode *nfsi = NFS_I(inode);
3980
3981 spin_lock(&inode->i_lock);
3982 kfree(nfsi->nfs4_acl);
3983 nfsi->nfs4_acl = acl;
3984 spin_unlock(&inode->i_lock);
3985}
3986
3987static void nfs4_zap_acl_attr(struct inode *inode)
3988{
3989 nfs4_set_cached_acl(inode, NULL);
3990}
3991
3992static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3993{
3994 struct nfs_inode *nfsi = NFS_I(inode);
3995 struct nfs4_cached_acl *acl;
3996 int ret = -ENOENT;
3997
3998 spin_lock(&inode->i_lock);
3999 acl = nfsi->nfs4_acl;
4000 if (acl == NULL)
4001 goto out;
4002 if (buf == NULL) /* user is just asking for length */
4003 goto out_len;
4004 if (acl->cached == 0)
4005 goto out;
4006 ret = -ERANGE; /* see getxattr(2) man page */
4007 if (acl->len > buflen)
4008 goto out;
4009 memcpy(buf, acl->data, acl->len);
4010out_len:
4011 ret = acl->len;
4012out:
4013 spin_unlock(&inode->i_lock);
4014 return ret;
4015}
4016
Sachin Prabhu5794d212012-04-17 14:36:40 +01004017static 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 +00004018{
4019 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004020 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004021
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004022 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004023 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004024 if (acl == NULL)
4025 goto out;
4026 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004027 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004028 } else {
4029 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4030 if (acl == NULL)
4031 goto out;
4032 acl->cached = 0;
4033 }
4034 acl->len = acl_len;
4035out:
4036 nfs4_set_cached_acl(inode, acl);
4037}
4038
Andy Adamsonbf118a32011-12-07 11:55:27 -05004039/*
4040 * The getxattr API returns the required buffer length when called with a
4041 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4042 * the required buf. On a NULL buf, we send a page of data to the server
4043 * guessing that the ACL request can be serviced by a page. If so, we cache
4044 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4045 * the cache. If not so, we throw away the page, and cache the required
4046 * length. The next getxattr call will then produce another round trip to
4047 * the server, this time with the input buf of the required size.
4048 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004049static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004050{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004051 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004052 struct nfs_getaclargs args = {
4053 .fh = NFS_FH(inode),
4054 .acl_pages = pages,
4055 .acl_len = buflen,
4056 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004057 struct nfs_getaclres res = {
4058 .acl_len = buflen,
4059 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004060 struct rpc_message msg = {
4061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4062 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004063 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004064 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004065 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4066 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004067
Andy Adamsonbf118a32011-12-07 11:55:27 -05004068 /* As long as we're doing a round trip to the server anyway,
4069 * let's be prepared for a page of acl data. */
4070 if (npages == 0)
4071 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004072 if (npages > ARRAY_SIZE(pages))
4073 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004074
Andy Adamsonbf118a32011-12-07 11:55:27 -05004075 for (i = 0; i < npages; i++) {
4076 pages[i] = alloc_page(GFP_KERNEL);
4077 if (!pages[i])
4078 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004079 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004080
4081 /* for decoding across pages */
4082 res.acl_scratch = alloc_page(GFP_KERNEL);
4083 if (!res.acl_scratch)
4084 goto out_free;
4085
Andy Adamsonbf118a32011-12-07 11:55:27 -05004086 args.acl_len = npages * PAGE_SIZE;
4087 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004088
Peng Taode040be2012-01-10 22:42:47 +08004089 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004090 __func__, buf, buflen, npages, args.acl_len);
4091 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4092 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004093 if (ret)
4094 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004095
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004096 /* Handle the case where the passed-in buffer is too short */
4097 if (res.acl_flags & NFS4_ACL_TRUNC) {
4098 /* Did the user only issue a request for the acl length? */
4099 if (buf == NULL)
4100 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004101 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004102 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004103 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004104 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004105 if (buf) {
4106 if (res.acl_len > buflen) {
4107 ret = -ERANGE;
4108 goto out_free;
4109 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004110 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004111 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004112out_ok:
4113 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004114out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004115 for (i = 0; i < npages; i++)
4116 if (pages[i])
4117 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004118 if (res.acl_scratch)
4119 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004120 return ret;
4121}
4122
Trond Myklebust16b42892006-08-24 12:27:15 -04004123static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4124{
4125 struct nfs4_exception exception = { };
4126 ssize_t ret;
4127 do {
4128 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4129 if (ret >= 0)
4130 break;
4131 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4132 } while (exception.retry);
4133 return ret;
4134}
4135
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004136static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4137{
4138 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004139 int ret;
4140
4141 if (!nfs4_server_supports_acls(server))
4142 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004143 ret = nfs_revalidate_inode(server, inode);
4144 if (ret < 0)
4145 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004146 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4147 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004148 ret = nfs4_read_cached_acl(inode, buf, buflen);
4149 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004150 /* -ENOENT is returned if there is no ACL or if there is an ACL
4151 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004152 return ret;
4153 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004154}
4155
Trond Myklebust16b42892006-08-24 12:27:15 -04004156static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004157{
4158 struct nfs_server *server = NFS_SERVER(inode);
4159 struct page *pages[NFS4ACL_MAXPAGES];
4160 struct nfs_setaclargs arg = {
4161 .fh = NFS_FH(inode),
4162 .acl_pages = pages,
4163 .acl_len = buflen,
4164 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004165 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004166 struct rpc_message msg = {
4167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4168 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004169 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004170 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004171 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004172 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004173
4174 if (!nfs4_server_supports_acls(server))
4175 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004176 if (npages > ARRAY_SIZE(pages))
4177 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004178 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4179 if (i < 0)
4180 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004181 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004182 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004183
4184 /*
4185 * Free each page after tx, so the only ref left is
4186 * held by the network stack
4187 */
4188 for (; i > 0; i--)
4189 put_page(pages[i-1]);
4190
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004191 /*
4192 * Acl update can result in inode attribute update.
4193 * so mark the attribute cache invalid.
4194 */
4195 spin_lock(&inode->i_lock);
4196 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4197 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004198 nfs_access_zap_cache(inode);
4199 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004200 return ret;
4201}
4202
Trond Myklebust16b42892006-08-24 12:27:15 -04004203static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4204{
4205 struct nfs4_exception exception = { };
4206 int err;
4207 do {
4208 err = nfs4_handle_exception(NFS_SERVER(inode),
4209 __nfs4_proc_set_acl(inode, buf, buflen),
4210 &exception);
4211 } while (exception.retry);
4212 return err;
4213}
4214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004216nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004218 struct nfs_client *clp = server->nfs_client;
4219
4220 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 return 0;
4222 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004223 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004224 case -NFS4ERR_ADMIN_REVOKED:
4225 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004226 if (state == NULL)
4227 break;
4228 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004229 case -NFS4ERR_OPENMODE:
4230 if (state == NULL)
4231 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004232 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4233 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004234 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004235 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004236 if (state != NULL) {
4237 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4238 goto stateid_invalid;
4239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004241 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004242 nfs4_schedule_lease_recovery(clp);
4243 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004244#if defined(CONFIG_NFS_V4_1)
4245 case -NFS4ERR_BADSESSION:
4246 case -NFS4ERR_BADSLOT:
4247 case -NFS4ERR_BAD_HIGH_SLOT:
4248 case -NFS4ERR_DEADSESSION:
4249 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4250 case -NFS4ERR_SEQ_FALSE_RETRY:
4251 case -NFS4ERR_SEQ_MISORDERED:
4252 dprintk("%s ERROR %d, Reset session\n", __func__,
4253 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004254 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson39be1c32013-11-15 16:36:16 -05004255 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004256#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004258 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004259 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4261 task->tk_status = 0;
4262 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004263 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 case -NFS4ERR_OLD_STATEID:
4265 task->tk_status = 0;
4266 return -EAGAIN;
4267 }
4268 task->tk_status = nfs4_map_errors(task->tk_status);
4269 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004270stateid_invalid:
4271 task->tk_status = -EIO;
4272 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004273wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004274 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004275 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4276 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4277 task->tk_status = 0;
4278 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
Chuck Leverf0920752012-05-21 22:45:41 -04004281static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4282 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004283{
4284 __be32 verf[2];
4285
Chuck Lever2c820d92012-05-21 22:45:33 -04004286 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4287 /* An impossible timestamp guarantees this value
4288 * will never match a generated boot time. */
4289 verf[0] = 0;
4290 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4291 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004292 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4293 verf[0] = (__be32)nn->boot_time.tv_sec;
4294 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004295 }
Chuck Levercd937102012-03-02 17:14:31 -05004296 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4297}
4298
Chuck Levere984a552012-09-14 17:24:21 -04004299static unsigned int
4300nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4301 char *buf, size_t len)
4302{
4303 unsigned int result;
4304
4305 rcu_read_lock();
4306 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4307 clp->cl_ipaddr,
4308 rpc_peeraddr2str(clp->cl_rpcclient,
4309 RPC_DISPLAY_ADDR),
4310 rpc_peeraddr2str(clp->cl_rpcclient,
4311 RPC_DISPLAY_PROTO));
4312 rcu_read_unlock();
4313 return result;
4314}
4315
4316static unsigned int
4317nfs4_init_uniform_client_string(const struct nfs_client *clp,
4318 char *buf, size_t len)
4319{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004320 char *nodename = clp->cl_rpcclient->cl_nodename;
4321
4322 if (nfs4_client_id_uniquifier[0] != '\0')
4323 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004324 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4325 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004326 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004327}
4328
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004329/**
4330 * nfs4_proc_setclientid - Negotiate client ID
4331 * @clp: state data structure
4332 * @program: RPC program for NFSv4 callback service
4333 * @port: IP port number for NFS4 callback service
4334 * @cred: RPC credential to use for this call
4335 * @res: where to place the result
4336 *
4337 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4338 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004339int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4340 unsigned short port, struct rpc_cred *cred,
4341 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
4343 nfs4_verifier sc_verifier;
4344 struct nfs4_setclientid setclientid = {
4345 .sc_verifier = &sc_verifier,
4346 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004347 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 };
4349 struct rpc_message msg = {
4350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4351 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004352 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004353 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004355 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Chuck Leverde734832012-07-11 16:30:50 -04004357 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004358 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004359 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4360 setclientid.sc_name_len =
4361 nfs4_init_uniform_client_string(clp,
4362 setclientid.sc_name,
4363 sizeof(setclientid.sc_name));
4364 else
4365 setclientid.sc_name_len =
4366 nfs4_init_nonuniform_client_string(clp,
4367 setclientid.sc_name,
4368 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004369 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004370 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004371 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004372 sizeof(setclientid.sc_netid),
4373 rpc_peeraddr2str(clp->cl_rpcclient,
4374 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004375 rcu_read_unlock();
4376 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004377 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 clp->cl_ipaddr, port >> 8, port & 255);
4379
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004380 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4381 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4382 setclientid.sc_name_len, setclientid.sc_name);
4383 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4384 dprintk("NFS reply setclientid: %d\n", status);
4385 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386}
4387
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004388/**
4389 * nfs4_proc_setclientid_confirm - Confirm client ID
4390 * @clp: state data structure
4391 * @res: result of a previous SETCLIENTID
4392 * @cred: RPC credential to use for this call
4393 *
4394 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4395 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004396int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004397 struct nfs4_setclientid_res *arg,
4398 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 struct rpc_message msg = {
4401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004402 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004403 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 int status;
4406
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004407 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4408 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4409 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004410 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004411 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 return status;
4413}
4414
Trond Myklebustfe650402006-01-03 09:55:18 +01004415struct nfs4_delegreturndata {
4416 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004417 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004418 struct nfs_fh fh;
4419 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004420 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004421 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004422 int rpc_status;
4423};
4424
Trond Myklebustfe650402006-01-03 09:55:18 +01004425static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4426{
4427 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004428
Trond Myklebust14516c32010-07-31 14:29:06 -04004429 if (!nfs4_sequence_done(task, &data->res.seq_res))
4430 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004431
Ricardo Labiaga79708862009-12-07 09:23:21 -05004432 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05004433 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004434 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004435 break;
Trond Myklebust0d08c382013-11-19 16:34:14 -05004436 case -NFS4ERR_ADMIN_REVOKED:
4437 case -NFS4ERR_DELEG_REVOKED:
4438 case -NFS4ERR_BAD_STATEID:
4439 case -NFS4ERR_OLD_STATEID:
4440 case -NFS4ERR_STALE_STATEID:
4441 case -NFS4ERR_EXPIRED:
4442 task->tk_status = 0;
4443 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05004444 default:
4445 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4446 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004447 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004448 return;
4449 }
4450 }
4451 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004452}
4453
4454static void nfs4_delegreturn_release(void *calldata)
4455{
Trond Myklebustfe650402006-01-03 09:55:18 +01004456 kfree(calldata);
4457}
4458
Andy Adamson938e1012009-04-01 09:22:28 -04004459#if defined(CONFIG_NFS_V4_1)
4460static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4461{
4462 struct nfs4_delegreturndata *d_data;
4463
4464 d_data = (struct nfs4_delegreturndata *)data;
4465
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004466 nfs4_setup_sequence(d_data->res.server,
4467 &d_data->args.seq_args,
4468 &d_data->res.seq_res,
4469 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004470}
4471#endif /* CONFIG_NFS_V4_1 */
4472
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004473static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004474#if defined(CONFIG_NFS_V4_1)
4475 .rpc_call_prepare = nfs4_delegreturn_prepare,
4476#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004477 .rpc_call_done = nfs4_delegreturn_done,
4478 .rpc_release = nfs4_delegreturn_release,
4479};
4480
Trond Myklebuste6f81072008-01-24 18:14:34 -05004481static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004482{
4483 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004484 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004485 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004486 struct rpc_message msg = {
4487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4488 .rpc_cred = cred,
4489 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004490 struct rpc_task_setup task_setup_data = {
4491 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004492 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004493 .callback_ops = &nfs4_delegreturn_ops,
4494 .flags = RPC_TASK_ASYNC,
4495 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004496 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004497
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004498 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004499 if (data == NULL)
4500 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004501 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004502 data->args.fhandle = &data->fh;
4503 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004504 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004505 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004506 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004507 data->res.fattr = &data->fattr;
4508 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004509 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004510 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004511 data->rpc_status = 0;
4512
Trond Myklebustc970aa82007-07-14 15:39:59 -04004513 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004514 msg.rpc_argp = &data->args;
4515 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004516 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004517 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004518 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004519 if (!issync)
4520 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004521 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004522 if (status != 0)
4523 goto out;
4524 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004525 if (status == 0)
4526 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4527 else
4528 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004529out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004530 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004531 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532}
4533
Trond Myklebuste6f81072008-01-24 18:14:34 -05004534int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
4536 struct nfs_server *server = NFS_SERVER(inode);
4537 struct nfs4_exception exception = { };
4538 int err;
4539 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004540 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 switch (err) {
4542 case -NFS4ERR_STALE_STATEID:
4543 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 case 0:
4545 return 0;
4546 }
4547 err = nfs4_handle_exception(server, err, &exception);
4548 } while (exception.retry);
4549 return err;
4550}
4551
4552#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4553#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4554
4555/*
4556 * sleep, with exponential backoff, and retry the LOCK operation.
4557 */
4558static unsigned long
4559nfs4_set_lock_task_retry(unsigned long timeout)
4560{
Jeff Laytond3103102011-12-01 22:44:39 +01004561 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 timeout <<= 1;
4563 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4564 return NFS4_LOCK_MAXTIMEOUT;
4565 return timeout;
4566}
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4569{
4570 struct inode *inode = state->inode;
4571 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004572 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004573 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004575 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004577 struct nfs_lockt_res res = {
4578 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 };
4580 struct rpc_message msg = {
4581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4582 .rpc_argp = &arg,
4583 .rpc_resp = &res,
4584 .rpc_cred = state->owner->so_cred,
4585 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 struct nfs4_lock_state *lsp;
4587 int status;
4588
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004589 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004590 status = nfs4_set_lock_state(state, request);
4591 if (status != 0)
4592 goto out;
4593 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004594 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004595 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004596 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004597 switch (status) {
4598 case 0:
4599 request->fl_type = F_UNLCK;
4600 break;
4601 case -NFS4ERR_DENIED:
4602 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004604 request->fl_ops->fl_release_private(request);
Trond Myklebust55fce162013-10-01 14:24:58 -04004605 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004606out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 return status;
4608}
4609
4610static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4611{
4612 struct nfs4_exception exception = { };
4613 int err;
4614
4615 do {
4616 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4617 _nfs4_proc_getlk(state, cmd, request),
4618 &exception);
4619 } while (exception.retry);
4620 return err;
4621}
4622
4623static int do_vfs_lock(struct file *file, struct file_lock *fl)
4624{
4625 int res = 0;
4626 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4627 case FL_POSIX:
4628 res = posix_lock_file_wait(file, fl);
4629 break;
4630 case FL_FLOCK:
4631 res = flock_lock_file_wait(file, fl);
4632 break;
4633 default:
4634 BUG();
4635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 return res;
4637}
4638
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004639struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004640 struct nfs_locku_args arg;
4641 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004642 struct nfs4_lock_state *lsp;
4643 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004644 struct file_lock fl;
4645 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004646 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004647};
4648
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004649static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4650 struct nfs_open_context *ctx,
4651 struct nfs4_lock_state *lsp,
4652 struct nfs_seqid *seqid)
4653{
4654 struct nfs4_unlockdata *p;
4655 struct inode *inode = lsp->ls_state->inode;
4656
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004657 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004658 if (p == NULL)
4659 return NULL;
4660 p->arg.fh = NFS_FH(inode);
4661 p->arg.fl = &p->fl;
4662 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004663 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004664 p->arg.stateid = &lsp->ls_stateid;
4665 p->lsp = lsp;
4666 atomic_inc(&lsp->ls_count);
4667 /* Ensure we don't close file until we're done freeing locks! */
4668 p->ctx = get_nfs_open_context(ctx);
4669 memcpy(&p->fl, fl, sizeof(p->fl));
4670 p->server = NFS_SERVER(inode);
4671 return p;
4672}
4673
Trond Myklebust06f814a2006-01-03 09:55:07 +01004674static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004675{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004676 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004677 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004678 nfs4_put_lock_state(calldata->lsp);
4679 put_nfs_open_context(calldata->ctx);
4680 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004681}
4682
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004683static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004684{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004685 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004686
Trond Myklebust14516c32010-07-31 14:29:06 -04004687 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4688 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004689 switch (task->tk_status) {
4690 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004691 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4692 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004693 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004694 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004695 case -NFS4ERR_BAD_STATEID:
4696 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004697 case -NFS4ERR_STALE_STATEID:
4698 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004699 break;
4700 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004701 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004702 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004703 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004704 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004705}
4706
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004707static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004708{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004709 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004711 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004712 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004713 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004714 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004715 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004716 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004717 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004718 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004719 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004720 &calldata->res.seq_res,
4721 task) != 0)
4722 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004723 return;
4724out_no_action:
4725 task->tk_action = NULL;
4726out_wait:
4727 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728}
4729
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004730static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004731 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004732 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004733 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004734};
4735
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004736static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4737 struct nfs_open_context *ctx,
4738 struct nfs4_lock_state *lsp,
4739 struct nfs_seqid *seqid)
4740{
4741 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004742 struct rpc_message msg = {
4743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4744 .rpc_cred = ctx->cred,
4745 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004746 struct rpc_task_setup task_setup_data = {
4747 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004748 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004749 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004750 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004751 .flags = RPC_TASK_ASYNC,
4752 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004753
Frank Filz137d6ac2007-07-09 15:32:29 -07004754 /* Ensure this is an unlock - when canceling a lock, the
4755 * canceled lock is passed in, and it won't be an unlock.
4756 */
4757 fl->fl_type = F_UNLCK;
4758
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004759 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4760 if (data == NULL) {
4761 nfs_free_seqid(seqid);
4762 return ERR_PTR(-ENOMEM);
4763 }
4764
Trond Myklebust9d12b212012-01-17 22:04:25 -05004765 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004766 msg.rpc_argp = &data->arg;
4767 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004768 task_setup_data.callback_data = data;
4769 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004770}
4771
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4773{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004774 struct inode *inode = state->inode;
4775 struct nfs4_state_owner *sp = state->owner;
4776 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004777 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004778 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004779 struct rpc_task *task;
4780 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004781 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
Trond Myklebust9b073572006-06-29 16:38:34 -04004783 status = nfs4_set_lock_state(state, request);
4784 /* Unlock _before_ we do the RPC call */
4785 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004786 /* Exclude nfs_delegation_claim_locks() */
4787 mutex_lock(&sp->so_delegreturn_mutex);
4788 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004789 down_read(&nfsi->rwsem);
4790 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4791 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004792 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004793 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004794 }
4795 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004796 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004797 if (status != 0)
4798 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004799 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004800 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04004801 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
4802 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004803 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004804 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004805 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004806 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004807 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004808 status = PTR_ERR(task);
4809 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004810 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004811 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004812 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004813out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004814 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004815 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004818struct nfs4_lockdata {
4819 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004820 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004821 struct nfs4_lock_state *lsp;
4822 struct nfs_open_context *ctx;
4823 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004824 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004825 int rpc_status;
4826 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004827 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004828};
4829
4830static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004831 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4832 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004833{
4834 struct nfs4_lockdata *p;
4835 struct inode *inode = lsp->ls_state->inode;
4836 struct nfs_server *server = NFS_SERVER(inode);
4837
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004838 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004839 if (p == NULL)
4840 return NULL;
4841
4842 p->arg.fh = NFS_FH(inode);
4843 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004844 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004845 if (p->arg.open_seqid == NULL)
4846 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004847 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004848 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004849 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004850 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004851 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004852 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004853 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004854 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004855 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004856 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004857 atomic_inc(&lsp->ls_count);
4858 p->ctx = get_nfs_open_context(ctx);
4859 memcpy(&p->fl, fl, sizeof(p->fl));
4860 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004861out_free_seqid:
4862 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004863out_free:
4864 kfree(p);
4865 return NULL;
4866}
4867
4868static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4869{
4870 struct nfs4_lockdata *data = calldata;
4871 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Harvey Harrison3110ff82008-05-02 13:42:44 -07004873 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004874 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004875 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004876 /* Do we need to do an open_to_lock_owner? */
4877 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004878 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004879 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004880 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04004881 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004882 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004883 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004884 } else
4885 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004886 if (!nfs4_valid_open_stateid(state)) {
4887 data->rpc_status = -EBADF;
4888 task->tk_action = NULL;
4889 goto out_release_open_seqid;
4890 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004891 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004892 if (nfs4_setup_sequence(data->server,
4893 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004894 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004895 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004896 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004897out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004898 nfs_release_seqid(data->arg.open_seqid);
4899out_release_lock_seqid:
4900 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004901out_wait:
4902 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004903 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004904}
4905
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004906static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4907{
4908 struct nfs4_lockdata *data = calldata;
4909
Harvey Harrison3110ff82008-05-02 13:42:44 -07004910 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004911
Trond Myklebust14516c32010-07-31 14:29:06 -04004912 if (!nfs4_sequence_done(task, &data->res.seq_res))
4913 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004914
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004915 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004916 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004917 if (data->rpc_status == 0)
4918 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4919 else
4920 goto out;
4921 }
4922 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004923 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004924 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004925 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004926 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004927out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004928 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004929}
4930
4931static void nfs4_lock_release(void *calldata)
4932{
4933 struct nfs4_lockdata *data = calldata;
4934
Harvey Harrison3110ff82008-05-02 13:42:44 -07004935 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004936 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004937 if (data->cancelled != 0) {
4938 struct rpc_task *task;
4939 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4940 data->arg.lock_seqid);
4941 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004942 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004943 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004944 } else
4945 nfs_free_seqid(data->arg.lock_seqid);
4946 nfs4_put_lock_state(data->lsp);
4947 put_nfs_open_context(data->ctx);
4948 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004949 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004950}
4951
4952static const struct rpc_call_ops nfs4_lock_ops = {
4953 .rpc_call_prepare = nfs4_lock_prepare,
4954 .rpc_call_done = nfs4_lock_done,
4955 .rpc_release = nfs4_lock_release,
4956};
4957
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004958static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4959{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004960 switch (error) {
4961 case -NFS4ERR_ADMIN_REVOKED:
4962 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004963 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004964 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004965 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004966 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004967 break;
4968 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004969 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004970 case -NFS4ERR_EXPIRED:
4971 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004972 };
4973}
4974
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004975static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004976{
4977 struct nfs4_lockdata *data;
4978 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004979 struct rpc_message msg = {
4980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4981 .rpc_cred = state->owner->so_cred,
4982 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004983 struct rpc_task_setup task_setup_data = {
4984 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004985 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004986 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004987 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004988 .flags = RPC_TASK_ASYNC,
4989 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004990 int ret;
4991
Harvey Harrison3110ff82008-05-02 13:42:44 -07004992 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004993 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004994 fl->fl_u.nfs4_fl.owner,
4995 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004996 if (data == NULL)
4997 return -ENOMEM;
4998 if (IS_SETLKW(cmd))
4999 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005000 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005001 msg.rpc_argp = &data->arg;
5002 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005003 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005004 if (recovery_type > NFS_LOCK_NEW) {
5005 if (recovery_type == NFS_LOCK_RECLAIM)
5006 data->arg.reclaim = NFS_LOCK_RECLAIM;
5007 nfs4_set_sequence_privileged(&data->arg.seq_args);
5008 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005009 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005010 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005011 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005012 ret = nfs4_wait_for_completion_rpc_task(task);
5013 if (ret == 0) {
5014 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005015 if (ret)
5016 nfs4_handle_setlk_error(data->server, data->lsp,
5017 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005018 } else
5019 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005020 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005021 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005022 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023}
5024
5025static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5026{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005027 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005028 struct nfs4_exception exception = {
5029 .inode = state->inode,
5030 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005031 int err;
5032
5033 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005034 /* Cache the lock if possible... */
5035 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5036 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005037 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005038 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005039 break;
5040 nfs4_handle_exception(server, err, &exception);
5041 } while (exception.retry);
5042 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043}
5044
5045static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5046{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005047 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005048 struct nfs4_exception exception = {
5049 .inode = state->inode,
5050 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005051 int err;
5052
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005053 err = nfs4_set_lock_state(state, request);
5054 if (err != 0)
5055 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005056 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005057 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5058 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005059 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005060 switch (err) {
5061 default:
5062 goto out;
5063 case -NFS4ERR_GRACE:
5064 case -NFS4ERR_DELAY:
5065 nfs4_handle_exception(server, err, &exception);
5066 err = 0;
5067 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005068 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005069out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005070 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071}
5072
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005073#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005074/**
5075 * nfs41_check_expired_locks - possibly free a lock stateid
5076 *
5077 * @state: NFSv4 state for an inode
5078 *
5079 * Returns NFS_OK if recovery for this stateid is now finished.
5080 * Otherwise a negative NFS4ERR value is returned.
5081 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005082static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005083{
Chuck Levereb64cf92012-07-11 16:30:05 -04005084 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005085 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005086 struct nfs_server *server = NFS_SERVER(state->inode);
5087
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005088 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005089 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005090 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5091 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005092 /* Free the stateid unless the server
5093 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005094 if (status != -NFS4ERR_BAD_STATEID)
5095 nfs41_free_stateid(server,
5096 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005097 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005098 ret = status;
5099 }
5100 }
5101 };
5102
5103 return ret;
5104}
5105
5106static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5107{
5108 int status = NFS_OK;
5109
5110 if (test_bit(LK_STATE_IN_USE, &state->flags))
5111 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005112 if (status != NFS_OK)
5113 status = nfs4_lock_expired(state, request);
5114 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005115}
5116#endif
5117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5119{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005120 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005121 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005122 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005123 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005124 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005126 if ((fl_flags & FL_POSIX) &&
5127 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5128 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005129 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005130 status = nfs4_set_lock_state(state, request);
5131 if (status != 0)
5132 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005133 request->fl_flags |= FL_ACCESS;
5134 status = do_vfs_lock(request->fl_file, request);
5135 if (status < 0)
5136 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005137 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005138 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005139 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005140 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005141 request->fl_flags = fl_flags & ~FL_SLEEP;
5142 status = do_vfs_lock(request->fl_file, request);
5143 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005144 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005145 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5146 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005147 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005148 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005149 goto out;
5150 down_read(&nfsi->rwsem);
5151 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5152 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005153 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005154 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005155 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005156 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005157 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005158 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5159 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005160out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005161 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005162out:
5163 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 return status;
5165}
5166
5167static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5168{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005169 struct nfs4_exception exception = {
5170 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005171 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005172 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 int err;
5174
5175 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005176 err = _nfs4_proc_setlk(state, cmd, request);
5177 if (err == -NFS4ERR_DENIED)
5178 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005180 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 } while (exception.retry);
5182 return err;
5183}
5184
5185static int
5186nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5187{
5188 struct nfs_open_context *ctx;
5189 struct nfs4_state *state;
5190 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5191 int status;
5192
5193 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005194 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 state = ctx->state;
5196
5197 if (request->fl_start < 0 || request->fl_end < 0)
5198 return -EINVAL;
5199
Trond Myklebustd9531262009-07-21 19:22:38 -04005200 if (IS_GETLK(cmd)) {
5201 if (state != NULL)
5202 return nfs4_proc_getlk(state, F_GETLK, request);
5203 return 0;
5204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
5206 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5207 return -EINVAL;
5208
Trond Myklebustd9531262009-07-21 19:22:38 -04005209 if (request->fl_type == F_UNLCK) {
5210 if (state != NULL)
5211 return nfs4_proc_unlck(state, cmd, request);
5212 return 0;
5213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Trond Myklebustd9531262009-07-21 19:22:38 -04005215 if (state == NULL)
5216 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005217 /*
5218 * Don't rely on the VFS having checked the file open mode,
5219 * since it won't do this for flock() locks.
5220 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005221 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005222 case F_RDLCK:
5223 if (!(filp->f_mode & FMODE_READ))
5224 return -EBADF;
5225 break;
5226 case F_WRLCK:
5227 if (!(filp->f_mode & FMODE_WRITE))
5228 return -EBADF;
5229 }
5230
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 do {
5232 status = nfs4_proc_setlk(state, cmd, request);
5233 if ((status != -EAGAIN) || IS_SETLK(cmd))
5234 break;
5235 timeout = nfs4_set_lock_task_retry(timeout);
5236 status = -ERESTARTSYS;
5237 if (signalled())
5238 break;
5239 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 return status;
5241}
5242
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005243int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005244{
5245 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005246 int err;
5247
5248 err = nfs4_set_lock_state(state, fl);
5249 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005250 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005251 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005252 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005253}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005254
Trond Myklebustcf470c32012-03-07 13:49:12 -05005255struct nfs_release_lockowner_data {
5256 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005257 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005258 struct nfs_release_lockowner_args args;
5259};
5260
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005261static void nfs4_release_lockowner_release(void *calldata)
5262{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005263 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005264 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005265 kfree(calldata);
5266}
5267
Trond Myklebust17280172012-03-11 13:11:00 -04005268static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005269 .rpc_release = nfs4_release_lockowner_release,
5270};
5271
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005272static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005273{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005274 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005275 struct rpc_message msg = {
5276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5277 };
5278
5279 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005280 return -EINVAL;
5281 data = kmalloc(sizeof(*data), GFP_NOFS);
5282 if (!data)
5283 return -ENOMEM;
5284 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005285 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005286 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5287 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5288 data->args.lock_owner.s_dev = server->s_dev;
5289 msg.rpc_argp = &data->args;
5290 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5291 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005292}
5293
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005294#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5295
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005296static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5297 const void *buf, size_t buflen,
5298 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005299{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005300 if (strcmp(key, "") != 0)
5301 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005302
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005303 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005304}
5305
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005306static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5307 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005308{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005309 if (strcmp(key, "") != 0)
5310 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005311
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005312 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005313}
5314
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005315static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5316 size_t list_len, const char *name,
5317 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005318{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005319 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005320
J. Bruce Fields096455a2006-03-20 23:23:42 -05005321 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5322 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005323
5324 if (list && len <= list_len)
5325 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005326 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005327}
5328
Andy Adamson533eb462011-06-13 18:25:56 -04005329/*
5330 * nfs_fhget will use either the mounted_on_fileid or the fileid
5331 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005332static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5333{
Andy Adamson533eb462011-06-13 18:25:56 -04005334 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5335 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5336 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005337 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005338 return;
5339
5340 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005341 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005342 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5343 fattr->nlink = 2;
5344}
5345
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005346static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5347 const struct qstr *name,
5348 struct nfs4_fs_locations *fs_locations,
5349 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005350{
5351 struct nfs_server *server = NFS_SERVER(dir);
5352 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005353 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005354 };
5355 struct nfs4_fs_locations_arg args = {
5356 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005357 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005358 .page = page,
5359 .bitmask = bitmask,
5360 };
Benny Halevy22958462009-04-01 09:22:02 -04005361 struct nfs4_fs_locations_res res = {
5362 .fs_locations = fs_locations,
5363 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005364 struct rpc_message msg = {
5365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5366 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005367 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005368 };
5369 int status;
5370
Harvey Harrison3110ff82008-05-02 13:42:44 -07005371 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005372
5373 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5374 * is not supported */
5375 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5376 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5377 else
5378 bitmask[0] |= FATTR4_WORD0_FILEID;
5379
Trond Myklebustc228fd32007-01-13 02:28:11 -05005380 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005381 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005382 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005383 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005384 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005385 return status;
5386}
5387
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005388int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5389 const struct qstr *name,
5390 struct nfs4_fs_locations *fs_locations,
5391 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005392{
5393 struct nfs4_exception exception = { };
5394 int err;
5395 do {
5396 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005397 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005398 &exception);
5399 } while (exception.retry);
5400 return err;
5401}
5402
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005403static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5404{
5405 int status;
5406 struct nfs4_secinfo_arg args = {
5407 .dir_fh = NFS_FH(dir),
5408 .name = name,
5409 };
5410 struct nfs4_secinfo_res res = {
5411 .flavors = flavors,
5412 };
5413 struct rpc_message msg = {
5414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5415 .rpc_argp = &args,
5416 .rpc_resp = &res,
5417 };
5418
5419 dprintk("NFS call secinfo %s\n", name->name);
5420 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5421 dprintk("NFS reply secinfo: %d\n", status);
5422 return status;
5423}
5424
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005425int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5426 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005427{
5428 struct nfs4_exception exception = { };
5429 int err;
5430 do {
5431 err = nfs4_handle_exception(NFS_SERVER(dir),
5432 _nfs4_proc_secinfo(dir, name, flavors),
5433 &exception);
5434 } while (exception.retry);
5435 return err;
5436}
5437
Andy Adamson557134a2009-04-01 09:21:53 -04005438#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005439/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005440 * Check the exchange flags returned by the server for invalid flags, having
5441 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5442 * DS flags set.
5443 */
5444static int nfs4_check_cl_exchange_flags(u32 flags)
5445{
5446 if (flags & ~EXCHGID4_FLAG_MASK_R)
5447 goto out_inval;
5448 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5449 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5450 goto out_inval;
5451 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5452 goto out_inval;
5453 return NFS_OK;
5454out_inval:
5455 return -NFS4ERR_INVAL;
5456}
5457
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005458static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005459nfs41_same_server_scope(struct nfs41_server_scope *a,
5460 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005461{
5462 if (a->server_scope_sz == b->server_scope_sz &&
5463 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5464 return true;
5465
5466 return false;
5467}
5468
Andy Adamson357f54d2010-12-14 10:11:57 -05005469/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005470 * nfs4_proc_bind_conn_to_session()
5471 *
5472 * The 4.1 client currently uses the same TCP connection for the
5473 * fore and backchannel.
5474 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005475int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005476{
5477 int status;
5478 struct nfs41_bind_conn_to_session_res res;
5479 struct rpc_message msg = {
5480 .rpc_proc =
5481 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5482 .rpc_argp = clp,
5483 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005484 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005485 };
5486
5487 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005488
5489 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5490 if (unlikely(res.session == NULL)) {
5491 status = -ENOMEM;
5492 goto out;
5493 }
5494
5495 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5496 if (status == 0) {
5497 if (memcmp(res.session->sess_id.data,
5498 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5499 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5500 status = -EIO;
5501 goto out_session;
5502 }
5503 if (res.dir != NFS4_CDFS4_BOTH) {
5504 dprintk("NFS: %s: Unexpected direction from server\n",
5505 __func__);
5506 status = -EIO;
5507 goto out_session;
5508 }
5509 if (res.use_conn_in_rdma_mode) {
5510 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5511 __func__);
5512 status = -EIO;
5513 goto out_session;
5514 }
5515 }
5516out_session:
5517 kfree(res.session);
5518out:
5519 dprintk("<-- %s status= %d\n", __func__, status);
5520 return status;
5521}
5522
5523/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005524 * nfs4_proc_exchange_id()
5525 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005526 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5527 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005528 * Since the clientid has expired, all compounds using sessions
5529 * associated with the stale clientid will be returning
5530 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5531 * be in some phase of session reset.
5532 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005533int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005534{
5535 nfs4_verifier verifier;
5536 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005537 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005538 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005539 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005540 };
5541 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005542 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005543 };
5544 int status;
5545 struct rpc_message msg = {
5546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5547 .rpc_argp = &args,
5548 .rpc_resp = &res,
5549 .rpc_cred = cred,
5550 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005551
Chuck Leverf0920752012-05-21 22:45:41 -04005552 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005553 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5554 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005555 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5556 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5557 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005558
Chuck Leveracdeb692012-05-21 22:46:16 -04005559 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005560 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005561 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005562 status = -ENOMEM;
5563 goto out;
5564 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005565
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005566 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005567 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005568 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005569 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005570 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005571 }
5572
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005573 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005574 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005575 status = -ENOMEM;
5576 goto out_server_scope;
5577 }
5578
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005579 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005580 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005581 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005582
Chuck Lever177313f2012-05-21 22:44:58 -04005583 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005584 clp->cl_clientid = res.clientid;
5585 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5586 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5587 clp->cl_seqid = res.seqid;
5588
Chuck Leveracdeb692012-05-21 22:46:16 -04005589 kfree(clp->cl_serverowner);
5590 clp->cl_serverowner = res.server_owner;
5591 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005592
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005593 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005594 kfree(clp->cl_implid);
5595 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005596
Chuck Lever177313f2012-05-21 22:44:58 -04005597 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005598 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005599 res.server_scope)) {
5600 dprintk("%s: server_scope mismatch detected\n",
5601 __func__);
5602 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005603 kfree(clp->cl_serverscope);
5604 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005605 }
5606
Chuck Lever177313f2012-05-21 22:44:58 -04005607 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005608 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005609 goto out;
5610 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005611 } else
5612 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005613
Chuck Leveracdeb692012-05-21 22:46:16 -04005614out_server_owner:
5615 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005616out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005617 kfree(res.server_scope);
5618out:
Chuck Lever177313f2012-05-21 22:44:58 -04005619 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005620 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005621 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005622 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005623 clp->cl_implid->date.seconds,
5624 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005625 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005626 return status;
5627}
5628
Trond Myklebust66245532012-05-25 17:18:09 -04005629static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5630 struct rpc_cred *cred)
5631{
5632 struct rpc_message msg = {
5633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5634 .rpc_argp = clp,
5635 .rpc_cred = cred,
5636 };
5637 int status;
5638
5639 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5640 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005641 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005642 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5643 return status;
5644}
5645
5646static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5647 struct rpc_cred *cred)
5648{
5649 unsigned int loop;
5650 int ret;
5651
5652 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5653 ret = _nfs4_proc_destroy_clientid(clp, cred);
5654 switch (ret) {
5655 case -NFS4ERR_DELAY:
5656 case -NFS4ERR_CLIENTID_BUSY:
5657 ssleep(1);
5658 break;
5659 default:
5660 return ret;
5661 }
5662 }
5663 return 0;
5664}
5665
5666int nfs4_destroy_clientid(struct nfs_client *clp)
5667{
5668 struct rpc_cred *cred;
5669 int ret = 0;
5670
5671 if (clp->cl_mvops->minor_version < 1)
5672 goto out;
5673 if (clp->cl_exchange_flags == 0)
5674 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005675 if (clp->cl_preserve_clid)
5676 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005677 cred = nfs4_get_exchange_id_cred(clp);
5678 ret = nfs4_proc_destroy_clientid(clp, cred);
5679 if (cred)
5680 put_rpccred(cred);
5681 switch (ret) {
5682 case 0:
5683 case -NFS4ERR_STALE_CLIENTID:
5684 clp->cl_exchange_flags = 0;
5685 }
5686out:
5687 return ret;
5688}
5689
Andy Adamson2050f0c2009-04-01 09:22:30 -04005690struct nfs4_get_lease_time_data {
5691 struct nfs4_get_lease_time_args *args;
5692 struct nfs4_get_lease_time_res *res;
5693 struct nfs_client *clp;
5694};
5695
5696static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5697 void *calldata)
5698{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005699 struct nfs4_get_lease_time_data *data =
5700 (struct nfs4_get_lease_time_data *)calldata;
5701
5702 dprintk("--> %s\n", __func__);
5703 /* just setup sequence, do not trigger session recovery
5704 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005705 nfs41_setup_sequence(data->clp->cl_session,
5706 &data->args->la_seq_args,
5707 &data->res->lr_seq_res,
5708 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005709 dprintk("<-- %s\n", __func__);
5710}
5711
5712/*
5713 * Called from nfs4_state_manager thread for session setup, so don't recover
5714 * from sequence operation or clientid errors.
5715 */
5716static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5717{
5718 struct nfs4_get_lease_time_data *data =
5719 (struct nfs4_get_lease_time_data *)calldata;
5720
5721 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005722 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5723 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005724 switch (task->tk_status) {
5725 case -NFS4ERR_DELAY:
5726 case -NFS4ERR_GRACE:
5727 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5728 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5729 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005730 /* fall through */
5731 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005732 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005733 return;
5734 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005735 dprintk("<-- %s\n", __func__);
5736}
5737
Trond Myklebust17280172012-03-11 13:11:00 -04005738static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005739 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5740 .rpc_call_done = nfs4_get_lease_time_done,
5741};
5742
5743int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5744{
5745 struct rpc_task *task;
5746 struct nfs4_get_lease_time_args args;
5747 struct nfs4_get_lease_time_res res = {
5748 .lr_fsinfo = fsinfo,
5749 };
5750 struct nfs4_get_lease_time_data data = {
5751 .args = &args,
5752 .res = &res,
5753 .clp = clp,
5754 };
5755 struct rpc_message msg = {
5756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5757 .rpc_argp = &args,
5758 .rpc_resp = &res,
5759 };
5760 struct rpc_task_setup task_setup = {
5761 .rpc_client = clp->cl_rpcclient,
5762 .rpc_message = &msg,
5763 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005764 .callback_data = &data,
5765 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005766 };
5767 int status;
5768
Trond Myklebust9d12b212012-01-17 22:04:25 -05005769 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005770 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005771 dprintk("--> %s\n", __func__);
5772 task = rpc_run_task(&task_setup);
5773
5774 if (IS_ERR(task))
5775 status = PTR_ERR(task);
5776 else {
5777 status = task->tk_status;
5778 rpc_put_task(task);
5779 }
5780 dprintk("<-- %s return %d\n", __func__, status);
5781
5782 return status;
5783}
5784
Andy Adamsonfc931582009-04-01 09:22:31 -04005785/*
5786 * Initialize the values to be used by the client in CREATE_SESSION
5787 * If nfs4_init_session set the fore channel request and response sizes,
5788 * use them.
5789 *
5790 * Set the back channel max_resp_sz_cached to zero to force the client to
5791 * always set csa_cachethis to FALSE because the current implementation
5792 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5793 */
5794static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5795{
5796 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005797 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5798 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005799
5800 if (mxrqst_sz == 0)
5801 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5802 if (mxresp_sz == 0)
5803 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5804 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005805 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5806 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005807 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005808 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005809
5810 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005811 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005812 __func__,
5813 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005814 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005815
5816 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005817 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5818 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5819 args->bc_attrs.max_resp_sz_cached = 0;
5820 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5821 args->bc_attrs.max_reqs = 1;
5822
5823 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5824 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5825 __func__,
5826 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5827 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5828 args->bc_attrs.max_reqs);
5829}
5830
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005831static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005832{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005833 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5834 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5835
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005836 if (rcvd->max_resp_sz > sent->max_resp_sz)
5837 return -EINVAL;
5838 /*
5839 * Our requested max_ops is the minimum we need; we're not
5840 * prepared to break up compounds into smaller pieces than that.
5841 * So, no point even trying to continue if the server won't
5842 * cooperate:
5843 */
5844 if (rcvd->max_ops < sent->max_ops)
5845 return -EINVAL;
5846 if (rcvd->max_reqs == 0)
5847 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005848 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5849 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005850 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005851}
5852
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005853static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5854{
5855 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5856 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005857
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005858 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5859 return -EINVAL;
5860 if (rcvd->max_resp_sz < sent->max_resp_sz)
5861 return -EINVAL;
5862 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5863 return -EINVAL;
5864 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005865 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005866 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005867 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005868 return -EINVAL;
5869 return 0;
5870}
Andy Adamson8d353012009-04-01 09:22:32 -04005871
Andy Adamson8d353012009-04-01 09:22:32 -04005872static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5873 struct nfs4_session *session)
5874{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005875 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005876
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005877 ret = nfs4_verify_fore_channel_attrs(args, session);
5878 if (ret)
5879 return ret;
5880 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005881}
5882
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005883static int _nfs4_proc_create_session(struct nfs_client *clp,
5884 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005885{
5886 struct nfs4_session *session = clp->cl_session;
5887 struct nfs41_create_session_args args = {
5888 .client = clp,
5889 .cb_program = NFS4_CALLBACK,
5890 };
5891 struct nfs41_create_session_res res = {
5892 .client = clp,
5893 };
5894 struct rpc_message msg = {
5895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5896 .rpc_argp = &args,
5897 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005898 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005899 };
5900 int status;
5901
5902 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005903 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005904
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005905 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005906
Trond Myklebust43095d32012-11-20 11:13:12 -05005907 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005908 /* Verify the session's negotiated channel_attrs values */
5909 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005910 /* Increment the clientid slot sequence id */
5911 clp->cl_seqid++;
5912 }
5913
5914 return status;
5915}
5916
5917/*
5918 * Issues a CREATE_SESSION operation to the server.
5919 * It is the responsibility of the caller to verify the session is
5920 * expired before calling this routine.
5921 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005922int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005923{
5924 int status;
5925 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005926 struct nfs4_session *session = clp->cl_session;
5927
5928 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5929
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005930 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005931 if (status)
5932 goto out;
5933
Andy Adamsonaacd5532011-11-09 13:58:21 -05005934 /* Init or reset the session slot tables */
5935 status = nfs4_setup_session_slot_tables(session);
5936 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005937 if (status)
5938 goto out;
5939
5940 ptr = (unsigned *)&session->sess_id.data[0];
5941 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5942 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005943out:
5944 dprintk("<-- %s\n", __func__);
5945 return status;
5946}
5947
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005948/*
5949 * Issue the over-the-wire RPC DESTROY_SESSION.
5950 * The caller must serialize access to this routine.
5951 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005952int nfs4_proc_destroy_session(struct nfs4_session *session,
5953 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005954{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005955 struct rpc_message msg = {
5956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5957 .rpc_argp = session,
5958 .rpc_cred = cred,
5959 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005960 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005961
5962 dprintk("--> nfs4_proc_destroy_session\n");
5963
5964 /* session is still being setup */
5965 if (session->clp->cl_cons_state != NFS_CS_READY)
5966 return status;
5967
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005968 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005969
5970 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005971 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005972 "Session has been destroyed regardless...\n", status);
5973
5974 dprintk("<-- nfs4_proc_destroy_session\n");
5975 return status;
5976}
5977
Trond Myklebust7b38c362012-05-23 13:23:31 -04005978/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005979 * Renew the cl_session lease.
5980 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005981struct nfs4_sequence_data {
5982 struct nfs_client *clp;
5983 struct nfs4_sequence_args args;
5984 struct nfs4_sequence_res res;
5985};
5986
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005987static void nfs41_sequence_release(void *data)
5988{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005989 struct nfs4_sequence_data *calldata = data;
5990 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005991
Alexandros Batsakis71358402010-02-05 03:45:05 -08005992 if (atomic_read(&clp->cl_count) > 1)
5993 nfs4_schedule_state_renewal(clp);
5994 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005995 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005996}
5997
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005998static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5999{
6000 switch(task->tk_status) {
6001 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006002 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6003 return -EAGAIN;
6004 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006005 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006006 }
6007 return 0;
6008}
6009
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006010static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006011{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006012 struct nfs4_sequence_data *calldata = data;
6013 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006014
Trond Myklebust14516c32010-07-31 14:29:06 -04006015 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6016 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006017
6018 if (task->tk_status < 0) {
6019 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006020 if (atomic_read(&clp->cl_count) == 1)
6021 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006022
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006023 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6024 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006025 return;
6026 }
6027 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006028 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006029out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006030 dprintk("<-- %s\n", __func__);
6031}
6032
6033static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6034{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006035 struct nfs4_sequence_data *calldata = data;
6036 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006037 struct nfs4_sequence_args *args;
6038 struct nfs4_sequence_res *res;
6039
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006040 args = task->tk_msg.rpc_argp;
6041 res = task->tk_msg.rpc_resp;
6042
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006043 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006044}
6045
6046static const struct rpc_call_ops nfs41_sequence_ops = {
6047 .rpc_call_done = nfs41_sequence_call_done,
6048 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006049 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006050};
6051
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006052static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6053 struct rpc_cred *cred,
6054 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006055{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006056 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006057 struct rpc_message msg = {
6058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6059 .rpc_cred = cred,
6060 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006061 struct rpc_task_setup task_setup_data = {
6062 .rpc_client = clp->cl_rpcclient,
6063 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006064 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006065 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006066 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006067
Alexandros Batsakis71358402010-02-05 03:45:05 -08006068 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006069 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006070 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006071 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006072 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006073 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006074 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006075 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006076 if (is_privileged)
6077 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006078 msg.rpc_argp = &calldata->args;
6079 msg.rpc_resp = &calldata->res;
6080 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006081 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006082
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006083 return rpc_run_task(&task_setup_data);
6084}
6085
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006086static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006087{
6088 struct rpc_task *task;
6089 int ret = 0;
6090
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006091 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsonf2368dc2014-09-29 12:31:57 -04006092 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006093 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006094 if (IS_ERR(task))
6095 ret = PTR_ERR(task);
6096 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006097 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006098 dprintk("<-- %s status=%d\n", __func__, ret);
6099 return ret;
6100}
6101
6102static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6103{
6104 struct rpc_task *task;
6105 int ret;
6106
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006107 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006108 if (IS_ERR(task)) {
6109 ret = PTR_ERR(task);
6110 goto out;
6111 }
6112 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006113 if (!ret) {
6114 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6115
6116 if (task->tk_status == 0)
6117 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006118 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006119 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006120 rpc_put_task(task);
6121out:
6122 dprintk("<-- %s status=%d\n", __func__, ret);
6123 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006124}
6125
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006126struct nfs4_reclaim_complete_data {
6127 struct nfs_client *clp;
6128 struct nfs41_reclaim_complete_args arg;
6129 struct nfs41_reclaim_complete_res res;
6130};
6131
6132static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6133{
6134 struct nfs4_reclaim_complete_data *calldata = data;
6135
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006136 nfs41_setup_sequence(calldata->clp->cl_session,
6137 &calldata->arg.seq_args,
6138 &calldata->res.seq_res,
6139 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006140}
6141
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006142static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6143{
6144 switch(task->tk_status) {
6145 case 0:
6146 case -NFS4ERR_COMPLETE_ALREADY:
6147 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6148 break;
6149 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006150 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006151 /* fall through */
6152 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006153 return -EAGAIN;
6154 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006155 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006156 }
6157 return 0;
6158}
6159
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006160static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6161{
6162 struct nfs4_reclaim_complete_data *calldata = data;
6163 struct nfs_client *clp = calldata->clp;
6164 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6165
6166 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006167 if (!nfs41_sequence_done(task, res))
6168 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006169
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006170 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6171 rpc_restart_call_prepare(task);
6172 return;
6173 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006174 dprintk("<-- %s\n", __func__);
6175}
6176
6177static void nfs4_free_reclaim_complete_data(void *data)
6178{
6179 struct nfs4_reclaim_complete_data *calldata = data;
6180
6181 kfree(calldata);
6182}
6183
6184static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6185 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6186 .rpc_call_done = nfs4_reclaim_complete_done,
6187 .rpc_release = nfs4_free_reclaim_complete_data,
6188};
6189
6190/*
6191 * Issue a global reclaim complete.
6192 */
6193static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6194{
6195 struct nfs4_reclaim_complete_data *calldata;
6196 struct rpc_task *task;
6197 struct rpc_message msg = {
6198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6199 };
6200 struct rpc_task_setup task_setup_data = {
6201 .rpc_client = clp->cl_rpcclient,
6202 .rpc_message = &msg,
6203 .callback_ops = &nfs4_reclaim_complete_call_ops,
6204 .flags = RPC_TASK_ASYNC,
6205 };
6206 int status = -ENOMEM;
6207
6208 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006209 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006210 if (calldata == NULL)
6211 goto out;
6212 calldata->clp = clp;
6213 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006214
Trond Myklebust9d12b212012-01-17 22:04:25 -05006215 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006216 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006217 msg.rpc_argp = &calldata->arg;
6218 msg.rpc_resp = &calldata->res;
6219 task_setup_data.callback_data = calldata;
6220 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006221 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006222 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006223 goto out;
6224 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006225 status = nfs4_wait_for_completion_rpc_task(task);
6226 if (status == 0)
6227 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006228 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006229 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006230out:
6231 dprintk("<-- %s status=%d\n", __func__, status);
6232 return status;
6233}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006234
6235static void
6236nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6237{
6238 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006239 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006240 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006241
6242 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006243 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6244 * right now covering the LAYOUTGET we are about to send.
6245 * However, that is not so catastrophic, and there seems
6246 * to be no way to prevent it completely.
6247 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006248 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006249 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006250 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006251 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6252 NFS_I(lgp->args.inode)->layout,
6253 lgp->args.ctx->state)) {
6254 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006255 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006256}
6257
6258static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6259{
6260 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006261 struct inode *inode = lgp->args.inode;
6262 struct nfs_server *server = NFS_SERVER(inode);
6263 struct pnfs_layout_hdr *lo;
6264 struct nfs4_state *state = NULL;
Boaz Harrosh305a7c62014-01-22 20:34:54 +02006265 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006266
Boaz Harrosh305a7c62014-01-22 20:34:54 +02006267 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006268
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006269 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006270 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006271
6272 switch (task->tk_status) {
6273 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006274 goto out;
Boaz Harrosh305a7c62014-01-22 20:34:54 +02006275 /*
6276 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
6277 * (or clients) writing to the same RAID stripe
6278 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006279 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harrosh305a7c62014-01-22 20:34:54 +02006280 /*
6281 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
6282 * existing layout before getting a new one).
6283 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006284 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006285 timeo = rpc_get_timeout(task->tk_client);
6286 giveup = lgp->args.timestamp + timeo;
Boaz Harrosh305a7c62014-01-22 20:34:54 +02006287 now = jiffies;
6288 if (time_after(giveup, now)) {
6289 unsigned long delay;
6290
6291 /* Delay for:
6292 * - Not less then NFS4_POLL_RETRY_MIN.
6293 * - One last time a jiffie before we give up
6294 * - exponential backoff (time_now minus start_attempt)
6295 */
6296 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
6297 min((giveup - now - 1),
6298 now - lgp->args.timestamp));
6299
6300 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
6301 __func__, delay);
6302 rpc_delay(task, delay);
6303 task->tk_status = 0;
6304 rpc_restart_call_prepare(task);
6305 goto out; /* Do not call nfs4_async_handle_error() */
6306 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006307 break;
6308 case -NFS4ERR_EXPIRED:
6309 case -NFS4ERR_BAD_STATEID:
6310 spin_lock(&inode->i_lock);
6311 lo = NFS_I(inode)->layout;
6312 if (!lo || list_empty(&lo->plh_segs)) {
6313 spin_unlock(&inode->i_lock);
6314 /* If the open stateid was bad, then recover it. */
6315 state = lgp->args.ctx->state;
6316 } else {
6317 LIST_HEAD(head);
6318
6319 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6320 spin_unlock(&inode->i_lock);
6321 /* Mark the bad layout state as invalid, then
6322 * retry using the open stateid. */
6323 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006324 }
6325 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006326 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6327 rpc_restart_call_prepare(task);
6328out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006329 dprintk("<-- %s\n", __func__);
6330}
6331
Idan Kedar85541162012-08-02 11:47:10 +03006332static size_t max_response_pages(struct nfs_server *server)
6333{
6334 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6335 return nfs_page_array_len(0, max_resp_sz);
6336}
6337
6338static void nfs4_free_pages(struct page **pages, size_t size)
6339{
6340 int i;
6341
6342 if (!pages)
6343 return;
6344
6345 for (i = 0; i < size; i++) {
6346 if (!pages[i])
6347 break;
6348 __free_page(pages[i]);
6349 }
6350 kfree(pages);
6351}
6352
6353static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6354{
6355 struct page **pages;
6356 int i;
6357
6358 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6359 if (!pages) {
6360 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6361 return NULL;
6362 }
6363
6364 for (i = 0; i < size; i++) {
6365 pages[i] = alloc_page(gfp_flags);
6366 if (!pages[i]) {
6367 dprintk("%s: failed to allocate page\n", __func__);
6368 nfs4_free_pages(pages, size);
6369 return NULL;
6370 }
6371 }
6372
6373 return pages;
6374}
6375
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006376static void nfs4_layoutget_release(void *calldata)
6377{
6378 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006379 struct inode *inode = lgp->args.inode;
6380 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006381 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006382
6383 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006384 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006385 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006386 put_nfs_open_context(lgp->args.ctx);
6387 kfree(calldata);
6388 dprintk("<-- %s\n", __func__);
6389}
6390
6391static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6392 .rpc_call_prepare = nfs4_layoutget_prepare,
6393 .rpc_call_done = nfs4_layoutget_done,
6394 .rpc_release = nfs4_layoutget_release,
6395};
6396
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006397struct pnfs_layout_segment *
6398nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006399{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006400 struct inode *inode = lgp->args.inode;
6401 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006402 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006403 struct rpc_task *task;
6404 struct rpc_message msg = {
6405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6406 .rpc_argp = &lgp->args,
6407 .rpc_resp = &lgp->res,
6408 };
6409 struct rpc_task_setup task_setup_data = {
6410 .rpc_client = server->client,
6411 .rpc_message = &msg,
6412 .callback_ops = &nfs4_layoutget_call_ops,
6413 .callback_data = lgp,
6414 .flags = RPC_TASK_ASYNC,
6415 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006416 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006417 int status = 0;
6418
6419 dprintk("--> %s\n", __func__);
6420
Peng Taof30d6372014-11-17 11:05:17 +08006421 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6422 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6423
Idan Kedar85541162012-08-02 11:47:10 +03006424 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6425 if (!lgp->args.layout.pages) {
6426 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006427 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006428 }
6429 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006430 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006431
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006432 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006433 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006434 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006435
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006436 task = rpc_run_task(&task_setup_data);
6437 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006438 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006439 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006440 if (status == 0)
6441 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006442 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6443 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006444 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006445 rpc_put_task(task);
6446 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006447 if (status)
6448 return ERR_PTR(status);
6449 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006450}
6451
Benny Halevycbe82602011-05-22 19:52:37 +03006452static void
6453nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6454{
6455 struct nfs4_layoutreturn *lrp = calldata;
6456
6457 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006458 nfs41_setup_sequence(lrp->clp->cl_session,
6459 &lrp->args.seq_args,
6460 &lrp->res.seq_res,
6461 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006462}
6463
6464static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6465{
6466 struct nfs4_layoutreturn *lrp = calldata;
6467 struct nfs_server *server;
6468
6469 dprintk("--> %s\n", __func__);
6470
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006471 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006472 return;
6473
6474 server = NFS_SERVER(lrp->args.inode);
6475 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006476 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006477 return;
6478 }
Benny Halevycbe82602011-05-22 19:52:37 +03006479 dprintk("<-- %s\n", __func__);
6480}
6481
6482static void nfs4_layoutreturn_release(void *calldata)
6483{
6484 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006485 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006486
6487 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006488 spin_lock(&lo->plh_inode->i_lock);
6489 if (lrp->res.lrs_present)
6490 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6491 lo->plh_block_lgets--;
6492 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006493 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006494 kfree(calldata);
6495 dprintk("<-- %s\n", __func__);
6496}
6497
6498static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6499 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6500 .rpc_call_done = nfs4_layoutreturn_done,
6501 .rpc_release = nfs4_layoutreturn_release,
6502};
6503
6504int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6505{
6506 struct rpc_task *task;
6507 struct rpc_message msg = {
6508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6509 .rpc_argp = &lrp->args,
6510 .rpc_resp = &lrp->res,
6511 };
6512 struct rpc_task_setup task_setup_data = {
6513 .rpc_client = lrp->clp->cl_rpcclient,
6514 .rpc_message = &msg,
6515 .callback_ops = &nfs4_layoutreturn_call_ops,
6516 .callback_data = lrp,
6517 };
6518 int status;
6519
6520 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006521 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006522 task = rpc_run_task(&task_setup_data);
6523 if (IS_ERR(task))
6524 return PTR_ERR(task);
6525 status = task->tk_status;
6526 dprintk("<-- %s status=%d\n", __func__, status);
6527 rpc_put_task(task);
6528 return status;
6529}
6530
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006531/*
6532 * Retrieve the list of Data Server devices from the MDS.
6533 */
6534static int _nfs4_getdevicelist(struct nfs_server *server,
6535 const struct nfs_fh *fh,
6536 struct pnfs_devicelist *devlist)
6537{
6538 struct nfs4_getdevicelist_args args = {
6539 .fh = fh,
6540 .layoutclass = server->pnfs_curr_ld->id,
6541 };
6542 struct nfs4_getdevicelist_res res = {
6543 .devlist = devlist,
6544 };
6545 struct rpc_message msg = {
6546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6547 .rpc_argp = &args,
6548 .rpc_resp = &res,
6549 };
6550 int status;
6551
6552 dprintk("--> %s\n", __func__);
6553 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6554 &res.seq_res, 0);
6555 dprintk("<-- %s status=%d\n", __func__, status);
6556 return status;
6557}
6558
6559int nfs4_proc_getdevicelist(struct nfs_server *server,
6560 const struct nfs_fh *fh,
6561 struct pnfs_devicelist *devlist)
6562{
6563 struct nfs4_exception exception = { };
6564 int err;
6565
6566 do {
6567 err = nfs4_handle_exception(server,
6568 _nfs4_getdevicelist(server, fh, devlist),
6569 &exception);
6570 } while (exception.retry);
6571
6572 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6573 err, devlist->num_devs);
6574
6575 return err;
6576}
6577EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6578
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006579static int
6580_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6581{
6582 struct nfs4_getdeviceinfo_args args = {
6583 .pdev = pdev,
6584 };
6585 struct nfs4_getdeviceinfo_res res = {
6586 .pdev = pdev,
6587 };
6588 struct rpc_message msg = {
6589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6590 .rpc_argp = &args,
6591 .rpc_resp = &res,
6592 };
6593 int status;
6594
6595 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006596 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006597 dprintk("<-- %s status=%d\n", __func__, status);
6598
6599 return status;
6600}
6601
6602int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6603{
6604 struct nfs4_exception exception = { };
6605 int err;
6606
6607 do {
6608 err = nfs4_handle_exception(server,
6609 _nfs4_proc_getdeviceinfo(server, pdev),
6610 &exception);
6611 } while (exception.retry);
6612 return err;
6613}
6614EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6615
Andy Adamson863a3c62011-03-23 13:27:54 +00006616static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6617{
6618 struct nfs4_layoutcommit_data *data = calldata;
6619 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006620 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006621
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006622 nfs41_setup_sequence(session,
6623 &data->args.seq_args,
6624 &data->res.seq_res,
6625 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006626}
6627
6628static void
6629nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6630{
6631 struct nfs4_layoutcommit_data *data = calldata;
6632 struct nfs_server *server = NFS_SERVER(data->args.inode);
6633
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006634 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006635 return;
6636
6637 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006638 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6639 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6640 case -NFS4ERR_BADLAYOUT: /* no layout */
6641 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006642 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006643 break;
6644 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006645 nfs_post_op_update_inode_force_wcc(data->args.inode,
6646 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006647 break;
6648 default:
6649 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6650 rpc_restart_call_prepare(task);
6651 return;
6652 }
6653 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006654}
6655
6656static void nfs4_layoutcommit_release(void *calldata)
6657{
6658 struct nfs4_layoutcommit_data *data = calldata;
6659
Andy Adamsondb29c082011-07-30 20:52:38 -04006660 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006661 put_rpccred(data->cred);
6662 kfree(data);
6663}
6664
6665static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6666 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6667 .rpc_call_done = nfs4_layoutcommit_done,
6668 .rpc_release = nfs4_layoutcommit_release,
6669};
6670
6671int
Andy Adamsonef311532011-03-12 02:58:10 -05006672nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006673{
6674 struct rpc_message msg = {
6675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6676 .rpc_argp = &data->args,
6677 .rpc_resp = &data->res,
6678 .rpc_cred = data->cred,
6679 };
6680 struct rpc_task_setup task_setup_data = {
6681 .task = &data->task,
6682 .rpc_client = NFS_CLIENT(data->args.inode),
6683 .rpc_message = &msg,
6684 .callback_ops = &nfs4_layoutcommit_ops,
6685 .callback_data = data,
6686 .flags = RPC_TASK_ASYNC,
6687 };
6688 struct rpc_task *task;
6689 int status = 0;
6690
6691 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6692 "lbw: %llu inode %lu\n",
6693 data->task.tk_pid, sync,
6694 data->args.lastbytewritten,
6695 data->args.inode->i_ino);
6696
Trond Myklebust9d12b212012-01-17 22:04:25 -05006697 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006698 task = rpc_run_task(&task_setup_data);
6699 if (IS_ERR(task))
6700 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006701 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006702 goto out;
6703 status = nfs4_wait_for_completion_rpc_task(task);
6704 if (status != 0)
6705 goto out;
6706 status = task->tk_status;
6707out:
6708 dprintk("%s: status %d\n", __func__, status);
6709 rpc_put_task(task);
6710 return status;
6711}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006712
6713static int
6714_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6715 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6716{
6717 struct nfs41_secinfo_no_name_args args = {
6718 .style = SECINFO_STYLE_CURRENT_FH,
6719 };
6720 struct nfs4_secinfo_res res = {
6721 .flavors = flavors,
6722 };
6723 struct rpc_message msg = {
6724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6725 .rpc_argp = &args,
6726 .rpc_resp = &res,
6727 };
6728 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6729}
6730
6731static int
6732nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6733 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6734{
6735 struct nfs4_exception exception = { };
6736 int err;
6737 do {
6738 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6739 switch (err) {
6740 case 0:
6741 case -NFS4ERR_WRONGSEC:
Weston Andros Adamsonc1942212014-01-13 16:54:45 -05006742 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006743 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006744 default:
6745 err = nfs4_handle_exception(server, err, &exception);
6746 }
6747 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006748out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006749 return err;
6750}
6751
6752static int
6753nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6754 struct nfs_fsinfo *info)
6755{
6756 int err;
6757 struct page *page;
6758 rpc_authflavor_t flavor;
6759 struct nfs4_secinfo_flavors *flavors;
6760
6761 page = alloc_page(GFP_KERNEL);
6762 if (!page) {
6763 err = -ENOMEM;
6764 goto out;
6765 }
6766
6767 flavors = page_address(page);
6768 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6769
6770 /*
6771 * Fall back on "guess and check" method if
6772 * the server doesn't support SECINFO_NO_NAME
6773 */
Weston Andros Adamsonc1942212014-01-13 16:54:45 -05006774 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006775 err = nfs4_find_root_sec(server, fhandle, info);
6776 goto out_freepage;
6777 }
6778 if (err)
6779 goto out_freepage;
6780
6781 flavor = nfs_find_best_sec(flavors);
6782 if (err == 0)
6783 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6784
6785out_freepage:
6786 put_page(page);
6787 if (err == -EACCES)
6788 return -EPERM;
6789out:
6790 return err;
6791}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006792
6793static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006794{
6795 int status;
6796 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006797 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006798 };
6799 struct nfs41_test_stateid_res res;
6800 struct rpc_message msg = {
6801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6802 .rpc_argp = &args,
6803 .rpc_resp = &res,
6804 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006805
Chuck Lever38527b12012-07-11 16:30:23 -04006806 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006807 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006808 nfs4_set_sequence_privileged(&args.seq_args);
6809 status = nfs4_call_sync_sequence(server->client, server, &msg,
6810 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006811 if (status != NFS_OK) {
6812 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006813 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006814 }
6815 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006816 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006817}
6818
Chuck Lever38527b12012-07-11 16:30:23 -04006819/**
6820 * nfs41_test_stateid - perform a TEST_STATEID operation
6821 *
6822 * @server: server / transport on which to perform the operation
6823 * @stateid: state ID to test
6824 *
6825 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6826 * Otherwise a negative NFS4ERR value is returned if the operation
6827 * failed or the state ID is not currently valid.
6828 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006829static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006830{
6831 struct nfs4_exception exception = { };
6832 int err;
6833 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006834 err = _nfs41_test_stateid(server, stateid);
6835 if (err != -NFS4ERR_DELAY)
6836 break;
6837 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006838 } while (exception.retry);
6839 return err;
6840}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006841
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006842struct nfs_free_stateid_data {
6843 struct nfs_server *server;
6844 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006845 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006846};
6847
6848static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
6849{
6850 struct nfs_free_stateid_data *data = calldata;
6851 nfs41_setup_sequence(nfs4_get_session(data->server),
6852 &data->args.seq_args,
6853 &data->res.seq_res,
6854 task);
6855}
6856
6857static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
6858{
6859 struct nfs_free_stateid_data *data = calldata;
6860
6861 nfs41_sequence_done(task, &data->res.seq_res);
6862
6863 switch (task->tk_status) {
6864 case -NFS4ERR_DELAY:
6865 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
6866 rpc_restart_call_prepare(task);
6867 }
6868}
6869
6870static void nfs41_free_stateid_release(void *calldata)
6871{
6872 kfree(calldata);
6873}
6874
6875const struct rpc_call_ops nfs41_free_stateid_ops = {
6876 .rpc_call_prepare = nfs41_free_stateid_prepare,
6877 .rpc_call_done = nfs41_free_stateid_done,
6878 .rpc_release = nfs41_free_stateid_release,
6879};
6880
6881static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
6882 nfs4_stateid *stateid,
6883 bool privileged)
6884{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006885 struct rpc_message msg = {
6886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006887 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006888 struct rpc_task_setup task_setup = {
6889 .rpc_client = server->client,
6890 .rpc_message = &msg,
6891 .callback_ops = &nfs41_free_stateid_ops,
6892 .flags = RPC_TASK_ASYNC,
6893 };
6894 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006895
Chuck Lever38527b12012-07-11 16:30:23 -04006896 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006897 data = kmalloc(sizeof(*data), GFP_NOFS);
6898 if (!data)
6899 return ERR_PTR(-ENOMEM);
6900 data->server = server;
6901 nfs4_stateid_copy(&data->args.stateid, stateid);
6902
6903 task_setup.callback_data = data;
6904
6905 msg.rpc_argp = &data->args;
6906 msg.rpc_resp = &data->res;
6907 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6908 if (privileged)
6909 nfs4_set_sequence_privileged(&data->args.seq_args);
6910
6911 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006912}
6913
Chuck Lever38527b12012-07-11 16:30:23 -04006914/**
6915 * nfs41_free_stateid - perform a FREE_STATEID operation
6916 *
6917 * @server: server / transport on which to perform the operation
6918 * @stateid: state ID to release
6919 *
6920 * Returns NFS_OK if the server freed "stateid". Otherwise a
6921 * negative NFS4ERR value is returned.
6922 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006923static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006924{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006925 struct rpc_task *task;
6926 int ret;
6927
6928 task = _nfs41_free_stateid(server, stateid, true);
6929 if (IS_ERR(task))
6930 return PTR_ERR(task);
6931 ret = rpc_wait_for_completion_task(task);
6932 if (!ret)
6933 ret = task->tk_status;
6934 rpc_put_task(task);
6935 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006936}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006937
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04006938static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
6939{
6940 struct rpc_task *task;
6941
6942 task = _nfs41_free_stateid(server, &lsp->ls_stateid, false);
6943 nfs4_free_lock_state(server, lsp);
6944 if (IS_ERR(task))
6945 return PTR_ERR(task);
6946 rpc_put_task(task);
6947 return 0;
6948}
6949
Trond Myklebust36281ca2012-03-04 18:13:56 -05006950static bool nfs41_match_stateid(const nfs4_stateid *s1,
6951 const nfs4_stateid *s2)
6952{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006953 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006954 return false;
6955
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006956 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006957 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006958 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006959 return true;
6960
6961 return false;
6962}
6963
Andy Adamson557134a2009-04-01 09:21:53 -04006964#endif /* CONFIG_NFS_V4_1 */
6965
Trond Myklebust36281ca2012-03-04 18:13:56 -05006966static bool nfs4_match_stateid(const nfs4_stateid *s1,
6967 const nfs4_stateid *s2)
6968{
Trond Myklebustf597c532012-03-04 18:13:56 -05006969 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006970}
6971
6972
Trond Myklebust17280172012-03-11 13:11:00 -04006973static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006974 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006975 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 .recover_open = nfs4_open_reclaim,
6977 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006978 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006979 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006980 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981};
6982
Andy Adamson591d71c2009-04-01 09:22:47 -04006983#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006984static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006985 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6986 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6987 .recover_open = nfs4_open_reclaim,
6988 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006989 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006990 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006991 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006992 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006993};
6994#endif /* CONFIG_NFS_V4_1 */
6995
Trond Myklebust17280172012-03-11 13:11:00 -04006996static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006997 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006998 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust361eeee2014-10-17 15:10:25 +03006999 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007001 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007002 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003};
7004
Andy Adamson591d71c2009-04-01 09:22:47 -04007005#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007006static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007007 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7008 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007009 .recover_open = nfs41_open_expired,
7010 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007011 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007012 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007013};
7014#endif /* CONFIG_NFS_V4_1 */
7015
Trond Myklebust17280172012-03-11 13:11:00 -04007016static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007017 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007018 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007019 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007020};
7021
7022#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007023static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007024 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007025 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007026 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007027};
7028#endif
7029
Trond Myklebust97dc1352010-06-16 09:52:26 -04007030static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7031 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007032 .init_caps = NFS_CAP_READDIRPLUS
7033 | NFS_CAP_ATOMIC_OPEN
7034 | NFS_CAP_CHANGE_ATTR
7035 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007036 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007037 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007038 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007039 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007040 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7041 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7042 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007043};
7044
7045#if defined(CONFIG_NFS_V4_1)
7046static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7047 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007048 .init_caps = NFS_CAP_READDIRPLUS
7049 | NFS_CAP_ATOMIC_OPEN
7050 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007051 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007052 | NFS_CAP_STATEID_NFSV41
7053 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007054 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007055 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007056 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007057 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007058 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7059 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7060 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007061};
7062#endif
7063
Trond Myklebust97dc1352010-06-16 09:52:26 -04007064const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7065 [0] = &nfs_v4_0_minor_ops,
7066#if defined(CONFIG_NFS_V4_1)
7067 [1] = &nfs_v4_1_minor_ops,
7068#endif
7069};
7070
Bryan Schumaker73a79702012-07-16 16:39:12 -04007071const struct inode_operations nfs4_dir_inode_operations = {
7072 .create = nfs_create,
7073 .lookup = nfs_lookup,
7074 .atomic_open = nfs_atomic_open,
7075 .link = nfs_link,
7076 .unlink = nfs_unlink,
7077 .symlink = nfs_symlink,
7078 .mkdir = nfs_mkdir,
7079 .rmdir = nfs_rmdir,
7080 .mknod = nfs_mknod,
7081 .rename = nfs_rename,
7082 .permission = nfs_permission,
7083 .getattr = nfs_getattr,
7084 .setattr = nfs_setattr,
7085 .getxattr = generic_getxattr,
7086 .setxattr = generic_setxattr,
7087 .listxattr = generic_listxattr,
7088 .removexattr = generic_removexattr,
7089};
7090
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007091static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007092 .permission = nfs_permission,
7093 .getattr = nfs_getattr,
7094 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007095 .getxattr = generic_getxattr,
7096 .setxattr = generic_setxattr,
7097 .listxattr = generic_listxattr,
7098 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007099};
7100
David Howells509de812006-08-22 20:06:11 -04007101const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 .version = 4, /* protocol version */
7103 .dentry_ops = &nfs4_dentry_operations,
7104 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007105 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007106 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007108 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007109 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110 .getattr = nfs4_proc_getattr,
7111 .setattr = nfs4_proc_setattr,
7112 .lookup = nfs4_proc_lookup,
7113 .access = nfs4_proc_access,
7114 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 .create = nfs4_proc_create,
7116 .remove = nfs4_proc_remove,
7117 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007118 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 .unlink_done = nfs4_proc_unlink_done,
7120 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007121 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007122 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007123 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 .link = nfs4_proc_link,
7125 .symlink = nfs4_proc_symlink,
7126 .mkdir = nfs4_proc_mkdir,
7127 .rmdir = nfs4_proc_remove,
7128 .readdir = nfs4_proc_readdir,
7129 .mknod = nfs4_proc_mknod,
7130 .statfs = nfs4_proc_statfs,
7131 .fsinfo = nfs4_proc_fsinfo,
7132 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007133 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 .decode_dirent = nfs4_decode_dirent,
7135 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007136 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007137 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007138 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007140 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007141 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007142 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007144 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007145 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007147 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007148 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007149 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007150 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007151 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007152 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007153 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007154 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007155 .create_server = nfs4_create_server,
7156 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157};
7158
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007159static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7160 .prefix = XATTR_NAME_NFSV4_ACL,
7161 .list = nfs4_xattr_list_nfs4_acl,
7162 .get = nfs4_xattr_get_nfs4_acl,
7163 .set = nfs4_xattr_set_nfs4_acl,
7164};
7165
7166const struct xattr_handler *nfs4_xattr_handlers[] = {
7167 &nfs4_xattr_nfs4_acl_handler,
7168 NULL
7169};
7170
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171/*
7172 * Local variables:
7173 * c-basic-offset: 8
7174 * End:
7175 */