blob: 004de208155495effdc0a2ac8a876c12376b17bc [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);
David Quigley1775fd32013-05-22 12:50:42 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
81static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
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,
David Quigley1775fd32013-05-22 12:50:42 -040084 struct nfs4_state *state, struct nfs4_label *ilabel,
85 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050091static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Trond Myklebust52567b02009-10-23 14:46:42 -040093 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -050097 case -NFS4ERR_LAYOUTTRYLATER:
98 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -040099 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000100 case -NFS4ERR_WRONGSEC:
101 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800102 case -NFS4ERR_BADOWNER:
103 case -NFS4ERR_BADNAME:
104 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400105 case -NFS4ERR_SHARE_DENIED:
106 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400107 case -NFS4ERR_MINOR_VERS_MISMATCH:
108 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400109 case -NFS4ERR_ACCESS:
110 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400111 case -NFS4ERR_FILE_OPEN:
112 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700115 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400116 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400118 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121/*
122 * This is our standard bitmap for GETATTR requests.
123 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400124const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 FATTR4_WORD0_TYPE
126 | FATTR4_WORD0_CHANGE
127 | FATTR4_WORD0_SIZE
128 | FATTR4_WORD0_FSID
129 | FATTR4_WORD0_FILEID,
130 FATTR4_WORD1_MODE
131 | FATTR4_WORD1_NUMLINKS
132 | FATTR4_WORD1_OWNER
133 | FATTR4_WORD1_OWNER_GROUP
134 | FATTR4_WORD1_RAWDEV
135 | FATTR4_WORD1_SPACE_USED
136 | FATTR4_WORD1_TIME_ACCESS
137 | FATTR4_WORD1_TIME_METADATA
138 | FATTR4_WORD1_TIME_MODIFY
139};
140
Trond Myklebust1549210f2012-06-05 09:16:47 -0400141static const u32 nfs4_pnfs_open_bitmap[3] = {
142 FATTR4_WORD0_TYPE
143 | FATTR4_WORD0_CHANGE
144 | FATTR4_WORD0_SIZE
145 | FATTR4_WORD0_FSID
146 | FATTR4_WORD0_FILEID,
147 FATTR4_WORD1_MODE
148 | FATTR4_WORD1_NUMLINKS
149 | FATTR4_WORD1_OWNER
150 | FATTR4_WORD1_OWNER_GROUP
151 | FATTR4_WORD1_RAWDEV
152 | FATTR4_WORD1_SPACE_USED
153 | FATTR4_WORD1_TIME_ACCESS
154 | FATTR4_WORD1_TIME_METADATA
155 | FATTR4_WORD1_TIME_MODIFY,
156 FATTR4_WORD2_MDSTHRESHOLD
157};
158
Andy Adamsone23008e2012-10-02 21:07:32 -0400159static const u32 nfs4_open_noattr_bitmap[3] = {
160 FATTR4_WORD0_TYPE
161 | FATTR4_WORD0_CHANGE
162 | FATTR4_WORD0_FILEID,
163};
164
David Quigleya09df2c2013-05-22 12:50:41 -0400165const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 FATTR4_WORD0_FILES_AVAIL
167 | FATTR4_WORD0_FILES_FREE
168 | FATTR4_WORD0_FILES_TOTAL,
169 FATTR4_WORD1_SPACE_AVAIL
170 | FATTR4_WORD1_SPACE_FREE
171 | FATTR4_WORD1_SPACE_TOTAL
172};
173
David Quigleya09df2c2013-05-22 12:50:41 -0400174const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 FATTR4_WORD0_MAXLINK
176 | FATTR4_WORD0_MAXNAME,
177 0
178};
179
Fred Isamandae100c2011-07-30 20:52:37 -0400180const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 | FATTR4_WORD0_MAXREAD
182 | FATTR4_WORD0_MAXWRITE
183 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700184 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400185 | FATTR4_WORD1_FS_LAYOUT_TYPES,
186 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
David Quigleya09df2c2013-05-22 12:50:41 -0400189const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400190 FATTR4_WORD0_TYPE
191 | FATTR4_WORD0_CHANGE
192 | FATTR4_WORD0_SIZE
193 | FATTR4_WORD0_FSID
194 | FATTR4_WORD0_FILEID
195 | FATTR4_WORD0_FS_LOCATIONS,
196 FATTR4_WORD1_MODE
197 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
204 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400206};
207
Al Virobc4785c2006-10-19 23:28:51 -0700208static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct nfs4_readdir_arg *readdir)
210{
Al Viro0dbb4c62006-10-19 23:28:49 -0700211 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000214 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
216 return;
217 }
218
219 readdir->cookie = 0;
220 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
221 if (cookie == 2)
222 return;
223
224 /*
225 * NFSv4 servers do not return entries for '.' and '..'
226 * Therefore, we fake these entries here. We let '.'
227 * have cookie 0 and '..' have cookie 1. Note that
228 * when talking to the server, we always send cookie 0
229 * instead of 1 or 2.
230 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800231 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (cookie == 0) {
234 *p++ = xdr_one; /* next */
235 *p++ = xdr_zero; /* cookie, first word */
236 *p++ = xdr_one; /* cookie, second word */
237 *p++ = xdr_one; /* entry len */
238 memcpy(p, ".\0\0\0", 4); /* entry */
239 p++;
240 *p++ = xdr_one; /* bitmap length */
241 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
242 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400243 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 *p++ = xdr_one; /* next */
247 *p++ = xdr_zero; /* cookie, first word */
248 *p++ = xdr_two; /* cookie, second word */
249 *p++ = xdr_two; /* entry len */
250 memcpy(p, "..\0\0", 4); /* entry */
251 p++;
252 *p++ = xdr_one; /* bitmap length */
253 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
254 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400255 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 readdir->pgbase = (char *)p - (char *)start;
258 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800259 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Trond Myklebust65de8722008-12-23 15:21:44 -0500262static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
263{
264 int res = 0;
265
266 might_sleep();
267
268 if (*timeout <= 0)
269 *timeout = NFS4_POLL_RETRY_MIN;
270 if (*timeout > NFS4_POLL_RETRY_MAX)
271 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100272 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500273 if (fatal_signal_pending(current))
274 res = -ERESTARTSYS;
275 *timeout <<= 1;
276 return res;
277}
278
279/* This is the error handling routine for processes that are allowed
280 * to sleep.
281 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800282static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500283{
284 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500285 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500286 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500287 int ret = errorcode;
288
289 exception->retry = 0;
290 switch(errorcode) {
291 case 0:
292 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500293 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400294 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400295 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500296 exception->retry = 1;
297 return 0;
298 }
299 if (state == NULL)
300 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400301 ret = nfs4_schedule_stateid_recovery(server, state);
302 if (ret < 0)
303 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500304 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500305 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500306 case -NFS4ERR_ADMIN_REVOKED:
307 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400308 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
309 nfs_remove_bad_delegation(inode);
310 exception->retry = 1;
311 break;
312 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500313 if (state == NULL)
314 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400315 ret = nfs4_schedule_stateid_recovery(server, state);
316 if (ret < 0)
317 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500318 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400319 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400320 if (state != NULL) {
321 ret = nfs4_schedule_stateid_recovery(server, state);
322 if (ret < 0)
323 break;
324 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500325 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500326 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500327 nfs4_schedule_lease_recovery(clp);
328 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500329#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400330 case -NFS4ERR_BADSESSION:
331 case -NFS4ERR_BADSLOT:
332 case -NFS4ERR_BAD_HIGH_SLOT:
333 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
334 case -NFS4ERR_DEADSESSION:
335 case -NFS4ERR_SEQ_FALSE_RETRY:
336 case -NFS4ERR_SEQ_MISORDERED:
337 dprintk("%s ERROR: %d Reset session\n", __func__,
338 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400339 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400340 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500341#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500342 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500343 if (exception->timeout > HZ) {
344 /* We have retried a decent amount, time to
345 * fail
346 */
347 ret = -EBUSY;
348 break;
349 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500350 case -NFS4ERR_GRACE:
351 case -NFS4ERR_DELAY:
352 ret = nfs4_delay(server->client, &exception->timeout);
353 if (ret != 0)
354 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400355 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 case -NFS4ERR_OLD_STATEID:
357 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800358 break;
359 case -NFS4ERR_BADOWNER:
360 /* The following works around a Linux server bug! */
361 case -NFS4ERR_BADNAME:
362 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
363 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
364 exception->retry = 1;
365 printk(KERN_WARNING "NFS: v4 server %s "
366 "does not accept raw "
367 "uid/gids. "
368 "Reenabling the idmapper.\n",
369 server->nfs_client->cl_hostname);
370 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500371 }
372 /* We failed to handle the error */
373 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500374wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500375 ret = nfs4_wait_clnt_recover(clp);
376 if (ret == 0)
377 exception->retry = 1;
378 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500379}
380
381
Trond Myklebust452e9352010-07-31 14:29:06 -0400382static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 spin_lock(&clp->cl_lock);
385 if (time_before(clp->cl_last_renewal,timestamp))
386 clp->cl_last_renewal = timestamp;
387 spin_unlock(&clp->cl_lock);
388}
389
Trond Myklebust452e9352010-07-31 14:29:06 -0400390static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
391{
392 do_renew_lease(server->nfs_client, timestamp);
393}
394
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400395#if defined(CONFIG_NFS_V4_1)
396
Trond Myklebustd185a332010-06-16 09:52:25 -0400397static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400398{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500399 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400400 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500401 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400402
Benny Halevydfb4f3092010-09-24 09:17:01 -0400403 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400404 /* just wake up the next guy waiting since
405 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500406 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 return;
Andy Adamson13615872009-04-01 09:22:17 -0400408 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500409 tbl = res->sr_slot->table;
410 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500411
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500412 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500413 /* Be nice to the server: try to ensure that the last transmitted
414 * value for highest_user_slotid <= target_highest_slotid
415 */
416 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
417 send_new_highest_used_slotid = true;
418
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500419 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
420 send_new_highest_used_slotid = false;
421 goto out_unlock;
422 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500423 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500424
425 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
426 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500427out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500428 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400429 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500430 if (send_new_highest_used_slotid)
431 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400432}
433
Trond Myklebust14516c32010-07-31 14:29:06 -0400434static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400435{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500436 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500437 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400438 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500439 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500440 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441
Bryan Schumaker468f8612011-04-18 15:57:32 -0400442 /* don't increment the sequence number if the task wasn't sent */
443 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400444 goto out;
445
Trond Myklebust933602e2012-11-16 12:12:38 -0500446 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500447 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500448
Trond Myklebustac20d162012-12-15 15:36:07 -0500449 if (slot->interrupted) {
450 slot->interrupted = 0;
451 interrupted = true;
452 }
453
Andy Adamson691daf32009-12-04 15:55:39 -0500454 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400455 switch (res->sr_status) {
456 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400457 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500458 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500459 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500460 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500461 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500462 if (res->sr_status_flags != 0)
463 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500464 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400465 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500466 case 1:
467 /*
468 * sr_status remains 1 if an RPC level error occurred.
469 * The server may or may not have processed the sequence
470 * operation..
471 * Mark the slot as having hosted an interrupted RPC call.
472 */
473 slot->interrupted = 1;
474 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400475 case -NFS4ERR_DELAY:
476 /* The server detected a resend of the RPC call and
477 * returned NFS4ERR_DELAY as per Section 2.10.6.2
478 * of RFC5661.
479 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500480 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400481 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500482 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500483 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400484 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500485 case -NFS4ERR_BADSLOT:
486 /*
487 * The slot id we used was probably retired. Try again
488 * using a different slot id.
489 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500490 goto retry_nowait;
491 case -NFS4ERR_SEQ_MISORDERED:
492 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500493 * Was the last operation on this sequence interrupted?
494 * If so, retry after bumping the sequence number.
495 */
496 if (interrupted) {
497 ++slot->seq_nr;
498 goto retry_nowait;
499 }
500 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500501 * Could this slot have been previously retired?
502 * If so, then the server may be expecting seq_nr = 1!
503 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500504 if (slot->seq_nr != 1) {
505 slot->seq_nr = 1;
506 goto retry_nowait;
507 }
508 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500509 case -NFS4ERR_SEQ_FALSE_RETRY:
510 ++slot->seq_nr;
511 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400512 default:
513 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500514 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400515 }
516out:
517 /* The session may be reset by one of the error handlers. */
518 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400519 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500520 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500521retry_nowait:
522 if (rpc_restart_call_prepare(task)) {
523 task->tk_status = 0;
524 ret = 0;
525 }
526 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400527out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400528 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400529 goto out;
530 rpc_delay(task, NFS4_POLL_RETRY_MAX);
531 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400532}
533
Trond Myklebust14516c32010-07-31 14:29:06 -0400534static int nfs4_sequence_done(struct rpc_task *task,
535 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400536{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500537 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400538 return 1;
539 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400540}
541
Trond Myklebust9d12b212012-01-17 22:04:25 -0500542static void nfs41_init_sequence(struct nfs4_sequence_args *args,
543 struct nfs4_sequence_res *res, int cache_reply)
544{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500545 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500546 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400547 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500548 if (cache_reply)
549 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500550 res->sr_slot = NULL;
551}
552
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400553static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
554{
555 args->sa_privileged = 1;
556}
557
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000558int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400559 struct nfs4_sequence_args *args,
560 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400561 struct rpc_task *task)
562{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400563 struct nfs4_slot *slot;
564 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400565
566 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400568 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400569 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400570
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400571 tbl = &session->fc_slot_table;
572
Trond Myklebust69d206b2012-11-22 13:21:02 -0500573 task->tk_timeout = 0;
574
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400575 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400576 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400577 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500578 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500579 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400580 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400581 }
582
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500583 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500584 if (IS_ERR(slot)) {
585 /* If out of memory, try again in 1/4 second */
586 if (slot == ERR_PTR(-ENOMEM))
587 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400588 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400589 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400590 }
591 spin_unlock(&tbl->slot_tbl_lock);
592
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500593 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400594
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500595 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
596 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400597
Benny Halevydfb4f3092010-09-24 09:17:01 -0400598 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500599 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400600 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400601 /*
602 * sr_status is only set in decode_sequence, and so will remain
603 * set to 1 if an rpc level failure occurs.
604 */
605 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400606out_success:
607 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400609out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400610 /* Privileged tasks are queued with top priority */
611 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400612 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
613 NULL, RPC_PRIORITY_PRIVILEGED);
614 else
615 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400616 spin_unlock(&tbl->slot_tbl_lock);
617 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000619EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620
Trond Myklebust035168a2010-06-16 09:52:26 -0400621int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622 struct nfs4_sequence_args *args,
623 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 struct rpc_task *task)
625{
Trond Myklebust035168a2010-06-16 09:52:26 -0400626 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627 int ret = 0;
628
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400629 if (session == NULL) {
630 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400632 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400633
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500634 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400635 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500636 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400637
Trond Myklebust9d12b212012-01-17 22:04:25 -0500638 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639out:
640 dprintk("<-- %s status=%d\n", __func__, ret);
641 return ret;
642}
643
644struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400645 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400646 struct nfs4_sequence_args *seq_args;
647 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400648};
649
650static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
651{
652 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400653 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654
Trond Myklebust035168a2010-06-16 09:52:26 -0400655 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
656
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400657 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658}
659
Andy Adamson69ab40c2009-04-01 09:22:19 -0400660static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
661{
662 struct nfs41_call_sync_data *data = calldata;
663
Trond Myklebust14516c32010-07-31 14:29:06 -0400664 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400665}
666
Trond Myklebust17280172012-03-11 13:11:00 -0400667static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400668 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400669 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400670};
671
Bryan Schumaker7c513052011-03-24 17:12:24 +0000672static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
673 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400674 struct rpc_message *msg,
675 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400676 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677{
678 int ret;
679 struct rpc_task *task;
680 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400681 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 .seq_args = args,
683 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 };
685 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000686 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 .rpc_message = msg,
688 .callback_ops = &nfs41_call_sync_ops,
689 .callback_data = &data
690 };
691
Andy Adamsonce5039c2009-04-01 09:22:13 -0400692 task = rpc_run_task(&task_setup);
693 if (IS_ERR(task))
694 ret = PTR_ERR(task);
695 else {
696 ret = task->tk_status;
697 rpc_put_task(task);
698 }
699 return ret;
700}
701
Trond Myklebustdf896452010-06-16 09:52:26 -0400702#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400703static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500704void nfs41_init_sequence(struct nfs4_sequence_args *args,
705 struct nfs4_sequence_res *res, int cache_reply)
706{
707}
708
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400709static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
710{
711}
712
713
Trond Myklebust14516c32010-07-31 14:29:06 -0400714static int nfs4_sequence_done(struct rpc_task *task,
715 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400716{
Trond Myklebust14516c32010-07-31 14:29:06 -0400717 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400718}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400719#endif /* CONFIG_NFS_V4_1 */
720
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400721static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000722int _nfs4_call_sync(struct rpc_clnt *clnt,
723 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724 struct rpc_message *msg,
725 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400726 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400727{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000728 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400729}
730
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400731static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000732int nfs4_call_sync(struct rpc_clnt *clnt,
733 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000734 struct rpc_message *msg,
735 struct nfs4_sequence_args *args,
736 struct nfs4_sequence_res *res,
737 int cache_reply)
738{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400739 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000740 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400741 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000742}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400743
Trond Myklebust38478b22006-05-25 01:40:57 -0400744static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Trond Myklebust38478b22006-05-25 01:40:57 -0400746 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Trond Myklebust38478b22006-05-25 01:40:57 -0400748 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400749 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700750 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400751 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700752 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000753 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400754 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400758 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759 struct nfs_openargs o_arg;
760 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100761 struct nfs_open_confirmargs c_arg;
762 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500763 struct nfs4_string owner_name;
764 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400766 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400768 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400770 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100772 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400773 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400774 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100775 int rpc_status;
776 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777};
778
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400779static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
780 int err, struct nfs4_exception *exception)
781{
782 if (err != -EINVAL)
783 return false;
784 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
785 return false;
786 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
787 exception->retry = 1;
788 return true;
789}
790
791static enum open_claim_type4
792nfs4_map_atomic_open_claim(struct nfs_server *server,
793 enum open_claim_type4 claim)
794{
795 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
796 return claim;
797 switch (claim) {
798 default:
799 return claim;
800 case NFS4_OPEN_CLAIM_FH:
801 return NFS4_OPEN_CLAIM_NULL;
802 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
803 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
804 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
805 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
806 }
807}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400808
809static void nfs4_init_opendata_res(struct nfs4_opendata *p)
810{
811 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400812 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400813 p->o_res.seqid = p->o_arg.seqid;
814 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400815 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400816 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400817 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500818 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400819}
820
Al Viro82a2c1b2011-06-22 18:30:55 -0400821static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400823 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400824 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400825 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400826 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827{
Al Viro82a2c1b2011-06-22 18:30:55 -0400828 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 struct inode *dir = parent->d_inode;
830 struct nfs_server *server = NFS_SERVER(dir);
831 struct nfs4_opendata *p;
832
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400833 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834 if (p == NULL)
835 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400836 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 if (p->o_arg.seqid == NULL)
838 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400839 nfs_sb_active(dentry->d_sb);
840 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100841 p->dir = parent;
842 p->owner = sp;
843 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844 p->o_arg.open_flags = flags;
845 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700846 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
847 * will return permission denied for all bits until close */
848 if (!(flags & O_EXCL)) {
849 /* ask server to check for all possible rights as results
850 * are cached */
851 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
852 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700853 }
David Howells7539bba2006-08-22 20:06:09 -0400854 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400855 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
856 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400857 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858 p->o_arg.server = server;
859 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400860 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400861 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400862 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
863 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400864 case NFS4_OPEN_CLAIM_NULL:
865 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
866 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
867 p->o_arg.fh = NFS_FH(dir);
868 break;
869 case NFS4_OPEN_CLAIM_PREVIOUS:
870 case NFS4_OPEN_CLAIM_FH:
871 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
872 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
873 p->o_arg.fh = NFS_FH(dentry->d_inode);
874 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500875 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500876 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400877
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100878 p->o_arg.u.attrs = &p->attrs;
879 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500880
881 verf[0] = jiffies;
882 verf[1] = current->pid;
883 memcpy(p->o_arg.u.verifier.data, verf,
884 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100885 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100886 p->c_arg.fh = &p->o_res.fh;
887 p->c_arg.stateid = &p->o_res.stateid;
888 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400889 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400890 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100891 return p;
892err_free:
893 kfree(p);
894err:
895 dput(parent);
896 return NULL;
897}
898
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400899static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100900{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400901 struct nfs4_opendata *p = container_of(kref,
902 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400903 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400904
905 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400906 if (p->state != NULL)
907 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400908 nfs4_put_state_owner(p->owner);
909 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400910 dput(p->dentry);
911 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500912 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400913 kfree(p);
914}
915
916static void nfs4_opendata_put(struct nfs4_opendata *p)
917{
918 if (p != NULL)
919 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100920}
921
Trond Myklebust06f814a2006-01-03 09:55:07 +0100922static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
923{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100924 int ret;
925
Trond Myklebust06f814a2006-01-03 09:55:07 +0100926 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100927 return ret;
928}
929
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400931{
932 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933
Trond Myklebust536e43d2012-01-17 22:04:26 -0500934 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500935 goto out;
936 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400937 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500938 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
939 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400940 break;
941 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500942 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
943 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944 break;
945 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500946 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
947 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400948 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400950 return ret;
951}
952
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500955 if (delegation == NULL)
956 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500959 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400960 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500961 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
962 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500963 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400964 return 1;
965}
966
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100968{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100970 case FMODE_WRITE:
971 state->n_wronly++;
972 break;
973 case FMODE_READ:
974 state->n_rdonly++;
975 break;
976 case FMODE_READ|FMODE_WRITE:
977 state->n_rdwr++;
978 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100980}
981
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500982static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400983{
984 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500985 nfs4_stateid_copy(&state->stateid, stateid);
986 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -0400987 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500988 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400989 case FMODE_READ:
990 set_bit(NFS_O_RDONLY_STATE, &state->flags);
991 break;
992 case FMODE_WRITE:
993 set_bit(NFS_O_WRONLY_STATE, &state->flags);
994 break;
995 case FMODE_READ|FMODE_WRITE:
996 set_bit(NFS_O_RDWR_STATE, &state->flags);
997 }
998}
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001001{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001002 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001004 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001005}
1006
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007static 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 -07001008{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001009 /*
1010 * Protect the call to nfs4_state_set_mode_locked and
1011 * serialise the stateid update
1012 */
1013 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001014 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001015 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001016 set_bit(NFS_DELEGATED_STATE, &state->flags);
1017 }
1018 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001020 write_sequnlock(&state->seqlock);
1021 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001023 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026static 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 -05001027{
1028 struct nfs_inode *nfsi = NFS_I(state->inode);
1029 struct nfs_delegation *deleg_cur;
1030 int ret = 0;
1031
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001033
1034 rcu_read_lock();
1035 deleg_cur = rcu_dereference(nfsi->delegation);
1036 if (deleg_cur == NULL)
1037 goto no_delegation;
1038
1039 spin_lock(&deleg_cur->lock);
1040 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001041 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001043 goto no_delegation_unlock;
1044
1045 if (delegation == NULL)
1046 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001047 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001048 goto no_delegation_unlock;
1049
Trond Myklebustb7391f42008-12-23 15:21:52 -05001050 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001052 ret = 1;
1053no_delegation_unlock:
1054 spin_unlock(&deleg_cur->lock);
1055no_delegation:
1056 rcu_read_unlock();
1057
1058 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001060 ret = 1;
1061 }
1062
1063 return ret;
1064}
1065
1066
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001067static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068{
1069 struct nfs_delegation *delegation;
1070
1071 rcu_read_lock();
1072 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074 rcu_read_unlock();
1075 return;
1076 }
1077 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001078 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001079}
1080
Trond Myklebust6ee41262007-07-08 14:11:36 -04001081static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001082{
1083 struct nfs4_state *state = opendata->state;
1084 struct nfs_inode *nfsi = NFS_I(state->inode);
1085 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001086 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088 nfs4_stateid stateid;
1089 int ret = -EAGAIN;
1090
Trond Myklebustaac00a82007-07-05 19:02:21 -04001091 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001092 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001093 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001094 if (can_open_cached(state, fmode, open_mode)) {
1095 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001096 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001097 goto out_return_state;
1098 }
1099 spin_unlock(&state->owner->so_lock);
1100 }
Trond Myklebust34310432008-12-23 15:21:38 -05001101 rcu_read_lock();
1102 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001103 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001104 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001105 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001106 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001108 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001109 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001110 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001111 if (!opendata->is_recover) {
1112 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1113 if (ret != 0)
1114 goto out;
1115 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001117
1118 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001119 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001120 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001122out:
1123 return ERR_PTR(ret);
1124out_return_state:
1125 atomic_inc(&state->count);
1126 return state;
1127}
1128
Andy Adamsone23008e2012-10-02 21:07:32 -04001129static void
1130nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1131{
1132 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1133 struct nfs_delegation *delegation;
1134 int delegation_flags = 0;
1135
1136 rcu_read_lock();
1137 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1138 if (delegation)
1139 delegation_flags = delegation->flags;
1140 rcu_read_unlock();
1141 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1142 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1143 "returning a delegation for "
1144 "OPEN(CLAIM_DELEGATE_CUR)\n",
1145 clp->cl_hostname);
1146 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1147 nfs_inode_set_delegation(state->inode,
1148 data->owner->so_cred,
1149 &data->o_res);
1150 else
1151 nfs_inode_reclaim_delegation(state->inode,
1152 data->owner->so_cred,
1153 &data->o_res);
1154}
1155
1156/*
1157 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1158 * and update the nfs4_state.
1159 */
1160static struct nfs4_state *
1161_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1162{
1163 struct inode *inode = data->state->inode;
1164 struct nfs4_state *state = data->state;
1165 int ret;
1166
1167 if (!data->rpc_done) {
1168 ret = data->rpc_status;
1169 goto err;
1170 }
1171
1172 ret = -ESTALE;
1173 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1174 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1175 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1176 goto err;
1177
1178 ret = -ENOMEM;
1179 state = nfs4_get_open_state(inode, data->owner);
1180 if (state == NULL)
1181 goto err;
1182
1183 ret = nfs_refresh_inode(inode, &data->f_attr);
1184 if (ret)
1185 goto err;
1186
1187 if (data->o_res.delegation_type != 0)
1188 nfs4_opendata_check_deleg(data, state);
1189 update_open_stateid(state, &data->o_res.stateid, NULL,
1190 data->o_arg.fmode);
1191
1192 return state;
1193err:
1194 return ERR_PTR(ret);
1195
1196}
1197
1198static struct nfs4_state *
1199_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001200{
1201 struct inode *inode;
1202 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001203 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001204
Trond Myklebustaac00a82007-07-05 19:02:21 -04001205 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001206 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001207 goto out;
1208 }
1209
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001210 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001211 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001212 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001213 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001214 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001215 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001216 goto err;
1217 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001218 state = nfs4_get_open_state(inode, data->owner);
1219 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001220 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001221 if (data->o_res.delegation_type != 0)
1222 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001223 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001224 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001225 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001226out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001227 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001228 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001229err_put_inode:
1230 iput(inode);
1231err:
1232 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001233}
1234
Andy Adamsone23008e2012-10-02 21:07:32 -04001235static struct nfs4_state *
1236nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1237{
1238 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1239 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1240 return _nfs4_opendata_to_nfs4_state(data);
1241}
1242
Trond Myklebust864472e2006-01-03 09:55:15 +01001243static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1244{
1245 struct nfs_inode *nfsi = NFS_I(state->inode);
1246 struct nfs_open_context *ctx;
1247
1248 spin_lock(&state->inode->i_lock);
1249 list_for_each_entry(ctx, &nfsi->open_files, list) {
1250 if (ctx->state != state)
1251 continue;
1252 get_nfs_open_context(ctx);
1253 spin_unlock(&state->inode->i_lock);
1254 return ctx;
1255 }
1256 spin_unlock(&state->inode->i_lock);
1257 return ERR_PTR(-ENOENT);
1258}
1259
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001260static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1261 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001262{
1263 struct nfs4_opendata *opendata;
1264
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001265 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001266 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001267 if (opendata == NULL)
1268 return ERR_PTR(-ENOMEM);
1269 opendata->state = state;
1270 atomic_inc(&state->count);
1271 return opendata;
1272}
1273
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001274static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001275{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001276 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001277 int ret;
1278
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001279 opendata->o_arg.open_flags = 0;
1280 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001281 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1282 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1283 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001284 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001285 if (ret != 0)
1286 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001287 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001288 if (IS_ERR(newstate))
1289 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001290 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001291 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001292 return 0;
1293}
1294
1295static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1296{
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001298 int ret;
1299
1300 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001301 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001302 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 smp_rmb();
1304 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001305 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001306 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001307 if (ret != 0)
1308 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001309 if (newstate != state)
1310 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001311 }
1312 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001313 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001314 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001315 if (ret != 0)
1316 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001317 if (newstate != state)
1318 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001319 }
1320 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001321 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001322 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001323 if (ret != 0)
1324 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001325 if (newstate != state)
1326 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001327 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001328 /*
1329 * We may have performed cached opens for all three recoveries.
1330 * Check if we need to update the current stateid.
1331 */
1332 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001333 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001334 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001335 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001336 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001337 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001338 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001339 return 0;
1340}
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342/*
1343 * OPEN_RECLAIM:
1344 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001346static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001348 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001349 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001350 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int status;
1352
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001353 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1354 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001355 if (IS_ERR(opendata))
1356 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001357 rcu_read_lock();
1358 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001359 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001360 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001361 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001362 opendata->o_arg.u.delegation_type = delegation_type;
1363 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001364 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return status;
1366}
1367
Trond Myklebust539cd032007-06-05 11:46:42 -04001368static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct nfs_server *server = NFS_SERVER(state->inode);
1371 struct nfs4_exception exception = { };
1372 int err;
1373 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001374 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001375 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1376 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001377 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001378 break;
1379 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 } while (exception.retry);
1381 return err;
1382}
1383
Trond Myklebust864472e2006-01-03 09:55:15 +01001384static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1385{
1386 struct nfs_open_context *ctx;
1387 int ret;
1388
1389 ctx = nfs4_state_find_open_context(state);
1390 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001391 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001392 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001393 put_nfs_open_context(ctx);
1394 return ret;
1395}
1396
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001397static 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 -07001398{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001399 switch (err) {
1400 default:
1401 printk(KERN_ERR "NFS: %s: unhandled error "
1402 "%d.\n", __func__, err);
1403 case 0:
1404 case -ENOENT:
1405 case -ESTALE:
1406 break;
1407 case -NFS4ERR_BADSESSION:
1408 case -NFS4ERR_BADSLOT:
1409 case -NFS4ERR_BAD_HIGH_SLOT:
1410 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1411 case -NFS4ERR_DEADSESSION:
1412 set_bit(NFS_DELEGATED_STATE, &state->flags);
1413 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1414 return -EAGAIN;
1415 case -NFS4ERR_STALE_CLIENTID:
1416 case -NFS4ERR_STALE_STATEID:
1417 set_bit(NFS_DELEGATED_STATE, &state->flags);
1418 case -NFS4ERR_EXPIRED:
1419 /* Don't recall a delegation if it was lost */
1420 nfs4_schedule_lease_recovery(server->nfs_client);
1421 return -EAGAIN;
1422 case -NFS4ERR_DELEG_REVOKED:
1423 case -NFS4ERR_ADMIN_REVOKED:
1424 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001425 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001426 nfs_inode_find_state_and_recover(state->inode,
1427 stateid);
1428 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001429 return 0;
1430 case -NFS4ERR_DELAY:
1431 case -NFS4ERR_GRACE:
1432 set_bit(NFS_DELEGATED_STATE, &state->flags);
1433 ssleep(1);
1434 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001435 case -ENOMEM:
1436 case -NFS4ERR_DENIED:
1437 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1438 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return err;
1441}
1442
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001443int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1444{
1445 struct nfs_server *server = NFS_SERVER(state->inode);
1446 struct nfs4_opendata *opendata;
1447 int err;
1448
1449 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1450 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1451 if (IS_ERR(opendata))
1452 return PTR_ERR(opendata);
1453 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1454 err = nfs4_open_recover(opendata, state);
1455 nfs4_opendata_put(opendata);
1456 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1457}
1458
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001459static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1460{
1461 struct nfs4_opendata *data = calldata;
1462
1463 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001464 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001465 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001466 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001467 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001468 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001469 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001470}
1471
1472static void nfs4_open_confirm_release(void *calldata)
1473{
1474 struct nfs4_opendata *data = calldata;
1475 struct nfs4_state *state = NULL;
1476
1477 /* If this request hasn't been cancelled, do nothing */
1478 if (data->cancelled == 0)
1479 goto out_free;
1480 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001481 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001482 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001483 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001484 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001485 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001486out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001487 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001488}
1489
1490static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001491 .rpc_call_done = nfs4_open_confirm_done,
1492 .rpc_release = nfs4_open_confirm_release,
1493};
1494
1495/*
1496 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1497 */
1498static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1499{
1500 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1501 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001502 struct rpc_message msg = {
1503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1504 .rpc_argp = &data->c_arg,
1505 .rpc_resp = &data->c_res,
1506 .rpc_cred = data->owner->so_cred,
1507 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001508 struct rpc_task_setup task_setup_data = {
1509 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001510 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001511 .callback_ops = &nfs4_open_confirm_ops,
1512 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001513 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001514 .flags = RPC_TASK_ASYNC,
1515 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 int status;
1517
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001518 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001519 data->rpc_done = 0;
1520 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001521 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001522 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001523 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001524 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001525 status = nfs4_wait_for_completion_rpc_task(task);
1526 if (status != 0) {
1527 data->cancelled = 1;
1528 smp_wmb();
1529 } else
1530 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001531 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return status;
1533}
1534
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001537 struct nfs4_opendata *data = calldata;
1538 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001539 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001540
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001541 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001542 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001543 /*
1544 * Check if we still need to send an OPEN call, or if we can use
1545 * a delegation instead.
1546 */
1547 if (data->state != NULL) {
1548 struct nfs_delegation *delegation;
1549
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001550 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001551 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001552 rcu_read_lock();
1553 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001554 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001555 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001556 can_open_delegated(delegation, data->o_arg.fmode))
1557 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001558 rcu_read_unlock();
1559 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001560 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001561 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001562 switch (data->o_arg.claim) {
1563 case NFS4_OPEN_CLAIM_PREVIOUS:
1564 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1565 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001566 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001567 case NFS4_OPEN_CLAIM_FH:
1568 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001569 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1570 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001571 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001572 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001573 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001574 &data->o_res.seq_res,
1575 task) != 0)
1576 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001577
1578 /* Set the create mode (note dependency on the session type) */
1579 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1580 if (data->o_arg.open_flags & O_EXCL) {
1581 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1582 if (nfs4_has_persistent_session(clp))
1583 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1584 else if (clp->cl_mvops->minor_version > 0)
1585 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1586 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001587 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001588unlock_no_action:
1589 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001590out_no_action:
1591 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001592out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001593 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001594}
1595
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001596static void nfs4_open_done(struct rpc_task *task, void *calldata)
1597{
1598 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001600 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001601
Trond Myklebust14516c32010-07-31 14:29:06 -04001602 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1603 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001604
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001605 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001606 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1607 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001608 case S_IFREG:
1609 break;
1610 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001611 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001612 break;
1613 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001614 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001615 break;
1616 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001617 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001618 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001619 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001620 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001621 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1622 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001623 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001624 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001625}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001626
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001627static void nfs4_open_release(void *calldata)
1628{
1629 struct nfs4_opendata *data = calldata;
1630 struct nfs4_state *state = NULL;
1631
1632 /* If this request hasn't been cancelled, do nothing */
1633 if (data->cancelled == 0)
1634 goto out_free;
1635 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001636 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001637 goto out_free;
1638 /* In case we need an open_confirm, no cleanup! */
1639 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1640 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001642 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001643 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001644out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001645 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001646}
1647
1648static const struct rpc_call_ops nfs4_open_ops = {
1649 .rpc_call_prepare = nfs4_open_prepare,
1650 .rpc_call_done = nfs4_open_done,
1651 .rpc_release = nfs4_open_release,
1652};
1653
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001654static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001655{
1656 struct inode *dir = data->dir->d_inode;
1657 struct nfs_server *server = NFS_SERVER(dir);
1658 struct nfs_openargs *o_arg = &data->o_arg;
1659 struct nfs_openres *o_res = &data->o_res;
1660 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001661 struct rpc_message msg = {
1662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1663 .rpc_argp = o_arg,
1664 .rpc_resp = o_res,
1665 .rpc_cred = data->owner->so_cred,
1666 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001667 struct rpc_task_setup task_setup_data = {
1668 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001669 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001670 .callback_ops = &nfs4_open_ops,
1671 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001672 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001673 .flags = RPC_TASK_ASYNC,
1674 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001675 int status;
1676
Trond Myklebust9d12b212012-01-17 22:04:25 -05001677 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001678 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001679 data->rpc_done = 0;
1680 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001681 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001682 data->is_recover = 0;
1683 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001684 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001685 data->is_recover = 1;
1686 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001687 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001688 if (IS_ERR(task))
1689 return PTR_ERR(task);
1690 status = nfs4_wait_for_completion_rpc_task(task);
1691 if (status != 0) {
1692 data->cancelled = 1;
1693 smp_wmb();
1694 } else
1695 status = data->rpc_status;
1696 rpc_put_task(task);
1697
1698 return status;
1699}
1700
1701static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1702{
1703 struct inode *dir = data->dir->d_inode;
1704 struct nfs_openres *o_res = &data->o_res;
1705 int status;
1706
1707 status = nfs4_run_open_task(data, 1);
1708 if (status != 0 || !data->rpc_done)
1709 return status;
1710
Trond Myklebust6926afd2012-01-07 13:22:46 -05001711 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1712
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001713 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1714 status = _nfs4_proc_open_confirm(data);
1715 if (status != 0)
1716 return status;
1717 }
1718
1719 return status;
1720}
1721
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001722static int nfs4_opendata_access(struct rpc_cred *cred,
1723 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001724 struct nfs4_state *state, fmode_t fmode,
1725 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001726{
1727 struct nfs_access_entry cache;
1728 u32 mask;
1729
1730 /* access call failed or for some reason the server doesn't
1731 * support any access modes -- defer access call until later */
1732 if (opendata->o_res.access_supported == 0)
1733 return 0;
1734
1735 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001736 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001737 * write mode bits, but POSIX allows the creating process to write.
1738 * use openflags to check for exec, because fmode won't
1739 * always have FMODE_EXEC set when file open for exec. */
1740 if (openflags & __FMODE_EXEC) {
1741 /* ONLY check for exec rights */
1742 mask = MAY_EXEC;
1743 } else if (fmode & FMODE_READ)
1744 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001745
1746 cache.cred = cred;
1747 cache.jiffies = jiffies;
1748 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1749 nfs_access_add_cache(state->inode, &cache);
1750
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001751 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001752 return 0;
1753
1754 /* even though OPEN succeeded, access is denied. Close the file */
1755 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001756 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001757}
1758
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001759/*
1760 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1761 */
1762static int _nfs4_proc_open(struct nfs4_opendata *data)
1763{
1764 struct inode *dir = data->dir->d_inode;
1765 struct nfs_server *server = NFS_SERVER(dir);
1766 struct nfs_openargs *o_arg = &data->o_arg;
1767 struct nfs_openres *o_res = &data->o_res;
1768 int status;
1769
1770 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001771 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001773 if (status != 0) {
1774 if (status == -NFS4ERR_BADNAME &&
1775 !(o_arg->open_flags & O_CREAT))
1776 return -ENOENT;
1777 return status;
1778 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779
Trond Myklebust6926afd2012-01-07 13:22:46 -05001780 nfs_fattr_map_and_free_names(server, &data->f_attr);
1781
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001782 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001783 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001784 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1785 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001787 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001789 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001792 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
Andy Adamsond83217c2011-03-01 01:34:17 +00001796static int nfs4_recover_expired_lease(struct nfs_server *server)
1797{
1798 return nfs4_client_recover_expired_lease(server->nfs_client);
1799}
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801/*
1802 * OPEN_EXPIRED:
1803 * reclaim state on the server after a network partition.
1804 * Assumes caller holds the appropriate lock
1805 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001806static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001808 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001809 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001811 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001812 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001813 if (IS_ERR(opendata))
1814 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001815 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001816 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001817 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001818 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001819 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001822static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001823{
Trond Myklebust539cd032007-06-05 11:46:42 -04001824 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001825 struct nfs4_exception exception = { };
1826 int err;
1827
1828 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001829 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001830 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1831 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001832 switch (err) {
1833 default:
1834 goto out;
1835 case -NFS4ERR_GRACE:
1836 case -NFS4ERR_DELAY:
1837 nfs4_handle_exception(server, err, &exception);
1838 err = 0;
1839 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001840 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001841out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001842 return err;
1843}
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1846{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001848 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Trond Myklebust864472e2006-01-03 09:55:15 +01001850 ctx = nfs4_state_find_open_context(state);
1851 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001852 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001853 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001854 put_nfs_open_context(ctx);
1855 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001858#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001859static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001860{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001861 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001862 nfs4_stateid *stateid = &state->stateid;
1863 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001864
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001865 /* If a state reset has been done, test_stateid is unneeded */
1866 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1867 return;
1868
1869 status = nfs41_test_stateid(server, stateid);
1870 if (status != NFS_OK) {
1871 /* Free the stateid unless the server explicitly
1872 * informs us the stateid is unrecognized. */
1873 if (status != -NFS4ERR_BAD_STATEID)
1874 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001875 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001876
Bryan Schumaker69388672012-09-26 15:25:52 -04001877 write_seqlock(&state->seqlock);
1878 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1879 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001880 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1881 }
1882}
1883
1884/**
1885 * nfs41_check_open_stateid - possibly free an open stateid
1886 *
1887 * @state: NFSv4 state for an inode
1888 *
1889 * Returns NFS_OK if recovery for this stateid is now finished.
1890 * Otherwise a negative NFS4ERR value is returned.
1891 */
1892static int nfs41_check_open_stateid(struct nfs4_state *state)
1893{
1894 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001895 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001896 int status;
1897
1898 /* If a state reset has been done, test_stateid is unneeded */
1899 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1900 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1901 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1902 return -NFS4ERR_BAD_STATEID;
1903
1904 status = nfs41_test_stateid(server, stateid);
1905 if (status != NFS_OK) {
1906 /* Free the stateid unless the server explicitly
1907 * informs us the stateid is unrecognized. */
1908 if (status != -NFS4ERR_BAD_STATEID)
1909 nfs41_free_stateid(server, stateid);
1910
1911 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1912 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1913 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001914 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001915 }
1916 return status;
1917}
1918
1919static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1920{
Chuck Levereb64cf92012-07-11 16:30:05 -04001921 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001922
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001923 nfs41_clear_delegation_stateid(state);
1924 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001925 if (status != NFS_OK)
1926 status = nfs4_open_expired(sp, state);
1927 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001928}
1929#endif
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001932 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1933 * fields corresponding to attributes that were used to store the verifier.
1934 * Make sure we clobber those fields in the later setattr call
1935 */
1936static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1937{
1938 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1939 !(sattr->ia_valid & ATTR_ATIME_SET))
1940 sattr->ia_valid |= ATTR_ATIME;
1941
1942 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1943 !(sattr->ia_valid & ATTR_MTIME_SET))
1944 sattr->ia_valid |= ATTR_MTIME;
1945}
1946
Trond Myklebustc21443c2013-02-07 14:26:21 -05001947static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1948 fmode_t fmode,
1949 int flags,
1950 struct nfs4_state **res)
1951{
1952 struct nfs4_state_owner *sp = opendata->owner;
1953 struct nfs_server *server = sp->so_server;
1954 struct nfs4_state *state;
1955 unsigned int seq;
1956 int ret;
1957
1958 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1959
1960 ret = _nfs4_proc_open(opendata);
1961 if (ret != 0)
1962 goto out;
1963
1964 state = nfs4_opendata_to_nfs4_state(opendata);
1965 ret = PTR_ERR(state);
1966 if (IS_ERR(state))
1967 goto out;
1968 if (server->caps & NFS_CAP_POSIX_LOCK)
1969 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1970
1971 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1972 if (ret != 0)
1973 goto out;
1974
Trond Myklebust91876b12013-03-28 14:01:33 -04001975 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001976 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001977 *res = state;
1978out:
1979 return ret;
1980}
1981
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001982/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001983 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 */
Andy Adamson82be4172012-05-23 05:02:35 -04001985static int _nfs4_do_open(struct inode *dir,
1986 struct dentry *dentry,
1987 fmode_t fmode,
1988 int flags,
1989 struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04001990 struct nfs4_label *label,
Andy Adamson82be4172012-05-23 05:02:35 -04001991 struct rpc_cred *cred,
1992 struct nfs4_state **res,
1993 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 struct nfs4_state_owner *sp;
1996 struct nfs4_state *state = NULL;
1997 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001998 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001999 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002000 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002001 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002005 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2006 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2008 goto out_err;
2009 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002010 status = nfs4_recover_expired_lease(server);
2011 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002012 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002013 if (dentry->d_inode != NULL)
2014 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002015 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002016 if (dentry->d_inode)
2017 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002018 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002019 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002020 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002021 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Andy Adamson82be4172012-05-23 05:02:35 -04002023 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2024 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2025 if (!opendata->f_attr.mdsthreshold)
2026 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002027 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002028 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002029 if (dentry->d_inode != NULL)
2030 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002031
Trond Myklebustc21443c2013-02-07 14:26:21 -05002032 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002033 if (status != 0)
2034 goto err_opendata_put;
2035
Trond Myklebust549b19c2013-04-16 18:42:34 -04002036 if ((opendata->o_arg.open_flags & O_EXCL) &&
2037 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002038 nfs4_exclusive_attrset(opendata, sattr);
2039
2040 nfs_fattr_init(opendata->o_res.f_attr);
2041 status = nfs4_do_setattr(state->inode, cred,
2042 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002043 state, label, olabel);
2044 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002045 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002046 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2047 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002048 }
Andy Adamson82be4172012-05-23 05:02:35 -04002049
2050 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2051 *ctx_th = opendata->f_attr.mdsthreshold;
2052 else
2053 kfree(opendata->f_attr.mdsthreshold);
2054 opendata->f_attr.mdsthreshold = NULL;
2055
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002056 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 *res = state;
2059 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002060err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002061 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002062 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002063err_put_state_owner:
2064 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 *res = NULL;
2067 return status;
2068}
2069
2070
Andy Adamson82be4172012-05-23 05:02:35 -04002071static struct nfs4_state *nfs4_do_open(struct inode *dir,
2072 struct dentry *dentry,
2073 fmode_t fmode,
2074 int flags,
2075 struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002076 struct nfs4_label *label,
Andy Adamson82be4172012-05-23 05:02:35 -04002077 struct rpc_cred *cred,
2078 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002080 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 struct nfs4_exception exception = { };
2082 struct nfs4_state *res;
2083 int status;
2084
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002085 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 do {
David Quigley1775fd32013-05-22 12:50:42 -04002087 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, label, cred,
Andy Adamson82be4172012-05-23 05:02:35 -04002088 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (status == 0)
2090 break;
2091 /* NOTE: BAD_SEQID means the server and client disagree about the
2092 * book-keeping w.r.t. state-changing operations
2093 * (OPEN/CLOSE/LOCK/LOCKU...)
2094 * It is actually a sign of a bug on the client or on the server.
2095 *
2096 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002097 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 * have unhashed the old state_owner for us, and that we can
2099 * therefore safely retry using a new one. We should still warn
2100 * the user though...
2101 */
2102 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002103 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002104 " returned a bad sequence-id error!\n",
2105 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 exception.retry = 1;
2107 continue;
2108 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002109 /*
2110 * BAD_STATEID on OPEN means that the server cancelled our
2111 * state before it received the OPEN_CONFIRM.
2112 * Recover by retrying the request as per the discussion
2113 * on Page 181 of RFC3530.
2114 */
2115 if (status == -NFS4ERR_BAD_STATEID) {
2116 exception.retry = 1;
2117 continue;
2118 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002119 if (status == -EAGAIN) {
2120 /* We must have found a delegation */
2121 exception.retry = 1;
2122 continue;
2123 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002124 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2125 continue;
2126 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 status, &exception));
2128 } while (exception.retry);
2129 return res;
2130}
2131
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002132static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2133 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002134 struct nfs4_state *state, struct nfs4_label *ilabel,
2135 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002137 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002139 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 .iap = sattr,
2141 .server = server,
2142 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002143 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 };
2145 struct nfs_setattrres res = {
2146 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002147 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 .server = server,
2149 };
2150 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2152 .rpc_argp = &arg,
2153 .rpc_resp = &res,
2154 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002156 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002157 fmode_t fmode;
2158 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002159 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Trond Myklebust0e574af2005-10-27 22:12:38 -04002161 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Trond Myklebustee3ae842013-04-29 10:35:36 -04002163 /* Servers should only apply open mode checks for file size changes */
2164 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2165 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2166
2167 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2168 /* Use that stateid */
2169 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002170 struct nfs_lockowner lockowner = {
2171 .l_owner = current->files,
2172 .l_pid = current->tgid,
2173 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002174 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002175 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002176 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002177 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Bryan Schumaker7c513052011-03-24 17:12:24 +00002179 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002180 if (status == 0 && state != NULL)
2181 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002182 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002185static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2186 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002187 struct nfs4_state *state, struct nfs4_label *ilabel,
2188 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002190 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002191 struct nfs4_exception exception = {
2192 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002193 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002194 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 int err;
2196 do {
David Quigley1775fd32013-05-22 12:50:42 -04002197 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebust451146b2012-04-18 16:29:11 -04002198 switch (err) {
2199 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002200 if (!(sattr->ia_valid & ATTR_SIZE)) {
2201 pr_warn_once("NFSv4: server %s is incorrectly "
2202 "applying open mode checks to "
2203 "a SETATTR that is not "
2204 "changing file size.\n",
2205 server->nfs_client->cl_hostname);
2206 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002207 if (state && !(state->state & FMODE_WRITE)) {
2208 err = -EBADF;
2209 if (sattr->ia_valid & ATTR_OPEN)
2210 err = -EACCES;
2211 goto out;
2212 }
2213 }
2214 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002216out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return err;
2218}
2219
2220struct nfs4_closedata {
2221 struct inode *inode;
2222 struct nfs4_state *state;
2223 struct nfs_closeargs arg;
2224 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002225 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002226 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002227 bool roc;
2228 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229};
2230
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002231static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002232{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002233 struct nfs4_closedata *calldata = data;
2234 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002235 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002236
Fred Isamanf7e89172011-01-06 11:36:32 +00002237 if (calldata->roc)
2238 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002239 nfs4_put_open_state(calldata->state);
2240 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002241 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002242 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002243 kfree(calldata);
2244}
2245
Trond Myklebust88069f72009-12-08 08:33:16 -05002246static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2247 fmode_t fmode)
2248{
2249 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002250 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002251 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2252 case FMODE_WRITE:
2253 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2254 break;
2255 case FMODE_READ:
2256 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2257 break;
2258 case 0:
2259 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2260 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2261 clear_bit(NFS_OPEN_STATE, &state->flags);
2262 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002263 spin_unlock(&state->owner->so_lock);
2264}
2265
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002266static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002268 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 struct nfs_server *server = NFS_SERVER(calldata->inode);
2271
Chuck Levera3ca5652012-03-01 17:00:40 -05002272 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002273 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2274 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 /* hmm. we are done with the inode, and in the process of freeing
2276 * the state_owner. we keep this around to process errors
2277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 switch (task->tk_status) {
2279 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002280 if (calldata->roc)
2281 pnfs_roc_set_barrier(state->inode,
2282 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002283 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002284 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002285 nfs4_close_clear_stateid_flags(state,
2286 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 break;
2288 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002289 case -NFS4ERR_OLD_STATEID:
2290 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002292 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002293 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002295 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2296 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002298 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002299 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002300 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002303static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002305 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002306 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002307 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002308 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002309
Chuck Levera3ca5652012-03-01 17:00:40 -05002310 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002311 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002312 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002313
Trond Myklebust88069f72009-12-08 08:33:16 -05002314 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2315 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002316 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002317 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002318 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002319 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002320 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2321 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2322 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002323 }
2324 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002325 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2326 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2327 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002328 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002329 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002330 if (!nfs4_valid_open_stateid(state))
2331 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002332 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002333
2334 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002335 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002336 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002337 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002338
Fred Isamanf7e89172011-01-06 11:36:32 +00002339 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002340 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002341 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002342 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2343 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002344 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002345 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002346 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002347
Trond Myklebust516a6af2005-10-27 22:12:41 -04002348 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002349 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002350 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002351 &calldata->arg.seq_args,
2352 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002353 task) != 0)
2354 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002355 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002356 return;
2357out_no_action:
2358 task->tk_action = NULL;
2359out_wait:
2360 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002363static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002364 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002365 .rpc_call_done = nfs4_close_done,
2366 .rpc_release = nfs4_free_closedata,
2367};
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369/*
2370 * It is possible for data to be read/written from a mem-mapped file
2371 * after the sys_close call (which hits the vfs layer as a flush).
2372 * This means that we can't safely call nfsv4 close on a file until
2373 * the inode is cleared. This in turn means that we are not good
2374 * NFSv4 citizens - we do not indicate to the server to update the file's
2375 * share state even when we are done with one of the three share
2376 * stateid's in the inode.
2377 *
2378 * NOTE: Caller must be holding the sp->so_owner semaphore!
2379 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002380int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002382 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002384 struct nfs4_state_owner *sp = state->owner;
2385 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002386 struct rpc_message msg = {
2387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2388 .rpc_cred = state->owner->so_cred,
2389 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002390 struct rpc_task_setup task_setup_data = {
2391 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002392 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002393 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002394 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002395 .flags = RPC_TASK_ASYNC,
2396 };
Trond Myklebust95121352005-10-18 14:20:12 -07002397 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002399 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002401 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002402 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002403 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002405 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002406 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002408 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002409 if (calldata->arg.seqid == NULL)
2410 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002411 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002412 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002413 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002414 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002415 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002416 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002417 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002418
Trond Myklebust1174dd12010-12-21 10:52:24 -05002419 msg.rpc_argp = &calldata->arg;
2420 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002421 task_setup_data.callback_data = calldata;
2422 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002423 if (IS_ERR(task))
2424 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002425 status = 0;
2426 if (wait)
2427 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002428 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002429 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002430out_free_calldata:
2431 kfree(calldata);
2432out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002433 nfs4_put_open_state(state);
2434 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002435 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
Trond Myklebust2b484292010-09-17 10:56:51 -04002438static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002439nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 struct nfs4_state *state;
David Quigley1775fd32013-05-22 12:50:42 -04002442 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Trond Myklebust565277f2007-10-15 18:17:53 -04002444 /* Protect against concurrent sillydeletes */
David Quigley1775fd32013-05-22 12:50:42 -04002445 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, label,
Andy Adamson82be4172012-05-23 05:02:35 -04002446 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002447 if (IS_ERR(state))
2448 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002449 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002450 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
Trond Myklebust1185a552009-12-03 15:54:02 -05002453static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002454{
2455 if (ctx->state == NULL)
2456 return;
2457 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002458 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002459 else
Al Viro643168c2011-06-22 18:20:23 -04002460 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002461}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2464{
Benny Halevy43652ad2009-04-01 09:21:54 -04002465 struct nfs4_server_caps_arg args = {
2466 .fhandle = fhandle,
2467 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 struct nfs4_server_caps_res res = {};
2469 struct rpc_message msg = {
2470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002471 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 .rpc_resp = &res,
2473 };
2474 int status;
2475
Bryan Schumaker7c513052011-03-24 17:12:24 +00002476 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (status == 0) {
2478 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002479 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2480 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2481 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2482 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2483 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2485 server->caps |= NFS_CAP_ACLS;
2486 if (res.has_links != 0)
2487 server->caps |= NFS_CAP_HARDLINKS;
2488 if (res.has_symlinks != 0)
2489 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002490 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2491 server->caps |= NFS_CAP_FILEID;
2492 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2493 server->caps |= NFS_CAP_MODE;
2494 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2495 server->caps |= NFS_CAP_NLINK;
2496 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2497 server->caps |= NFS_CAP_OWNER;
2498 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2499 server->caps |= NFS_CAP_OWNER_GROUP;
2500 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2501 server->caps |= NFS_CAP_ATIME;
2502 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2503 server->caps |= NFS_CAP_CTIME;
2504 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2505 server->caps |= NFS_CAP_MTIME;
2506
Trond Myklebusta65318b2009-03-11 14:10:28 -04002507 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2508 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2509 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002511 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return status;
2515}
2516
Trond Myklebust55a97592006-06-09 09:34:19 -04002517int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
2519 struct nfs4_exception exception = { };
2520 int err;
2521 do {
2522 err = nfs4_handle_exception(server,
2523 _nfs4_server_capabilities(server, fhandle),
2524 &exception);
2525 } while (exception.retry);
2526 return err;
2527}
2528
2529static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2530 struct nfs_fsinfo *info)
2531{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 struct nfs4_lookup_root_arg args = {
2533 .bitmask = nfs4_fattr_bitmap,
2534 };
2535 struct nfs4_lookup_res res = {
2536 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002537 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 .fh = fhandle,
2539 };
2540 struct rpc_message msg = {
2541 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2542 .rpc_argp = &args,
2543 .rpc_resp = &res,
2544 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002545
Trond Myklebust0e574af2005-10-27 22:12:38 -04002546 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002547 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
2550static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2551 struct nfs_fsinfo *info)
2552{
2553 struct nfs4_exception exception = { };
2554 int err;
2555 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002556 err = _nfs4_lookup_root(server, fhandle, info);
2557 switch (err) {
2558 case 0:
2559 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002560 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002561 default:
2562 err = nfs4_handle_exception(server, err, &exception);
2563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002565out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 return err;
2567}
2568
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002569static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2570 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2571{
2572 struct rpc_auth *auth;
2573 int ret;
2574
2575 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002576 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002577 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002578 goto out;
2579 }
2580 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002581out:
2582 return ret;
2583}
2584
Chuck Lever9a744ba2013-03-16 15:56:02 -04002585/*
2586 * Retry pseudoroot lookup with various security flavors. We do this when:
2587 *
2588 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2589 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2590 *
2591 * Returns zero on success, or a negative NFS4ERR value, or a
2592 * negative errno value.
2593 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002594static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002595 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002597 /* Per 3530bis 15.33.5 */
2598 static const rpc_authflavor_t flav_array[] = {
2599 RPC_AUTH_GSS_KRB5P,
2600 RPC_AUTH_GSS_KRB5I,
2601 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002602 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002603 RPC_AUTH_NULL,
2604 };
2605 int status = -EPERM;
2606 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Chuck Lever9a744ba2013-03-16 15:56:02 -04002608 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002609 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002610 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002611 continue;
2612 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002613 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002614
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002615 /*
2616 * -EACCESS could mean that the user doesn't have correct permissions
2617 * to access the mount. It could also mean that we tried to mount
2618 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2619 * existing mount programs don't handle -EACCES very well so it should
2620 * be mapped to -EPERM instead.
2621 */
2622 if (status == -EACCES)
2623 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002624 return status;
2625}
2626
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002627static int nfs4_do_find_root_sec(struct nfs_server *server,
2628 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2629{
2630 int mv = server->nfs_client->cl_minorversion;
2631 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2632}
2633
2634/**
2635 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2636 * @server: initialized nfs_server handle
2637 * @fhandle: we fill in the pseudo-fs root file handle
2638 * @info: we fill in an FSINFO struct
2639 *
2640 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002641 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002642int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2643 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002644{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002645 int status;
2646
2647 status = nfs4_lookup_root(server, fhandle, info);
2648 if ((status == -NFS4ERR_WRONGSEC) &&
2649 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2650 status = nfs4_do_find_root_sec(server, fhandle, info);
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if (status == 0)
2653 status = nfs4_server_capabilities(server, fhandle);
2654 if (status == 0)
2655 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002656
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002657 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Bryan Schumakerbae36242012-05-10 15:07:31 -04002660static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2661 struct nfs_fsinfo *info)
2662{
2663 int error;
2664 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002665 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002666
2667 error = nfs4_server_capabilities(server, mntfh);
2668 if (error < 0) {
2669 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2670 return error;
2671 }
2672
David Quigley1775fd32013-05-22 12:50:42 -04002673 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002674 if (error < 0) {
2675 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2676 return error;
2677 }
2678
2679 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2680 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2681 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2682
2683 return error;
2684}
2685
Manoj Naik6b97fd32006-06-09 09:34:29 -04002686/*
2687 * Get locations and (maybe) other attributes of a referral.
2688 * Note that we'll actually follow the referral later when
2689 * we detect fsid mismatch in inode revalidation
2690 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002691static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2692 const struct qstr *name, struct nfs_fattr *fattr,
2693 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002694{
2695 int status = -ENOMEM;
2696 struct page *page = NULL;
2697 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002698
2699 page = alloc_page(GFP_KERNEL);
2700 if (page == NULL)
2701 goto out;
2702 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2703 if (locations == NULL)
2704 goto out;
2705
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002706 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002707 if (status != 0)
2708 goto out;
2709 /* Make sure server returned a different fsid for the referral */
2710 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002711 dprintk("%s: server did not return a different fsid for"
2712 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002713 status = -EIO;
2714 goto out;
2715 }
Andy Adamson533eb462011-06-13 18:25:56 -04002716 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2717 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002718
Andy Adamson533eb462011-06-13 18:25:56 -04002719 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002720 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002721 memset(fhandle, 0, sizeof(struct nfs_fh));
2722out:
2723 if (page)
2724 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002725 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002726 return status;
2727}
2728
David Quigley1775fd32013-05-22 12:50:42 -04002729static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2730 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
2732 struct nfs4_getattr_arg args = {
2733 .fh = fhandle,
2734 .bitmask = server->attr_bitmask,
2735 };
2736 struct nfs4_getattr_res res = {
2737 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002738 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 .server = server,
2740 };
2741 struct rpc_message msg = {
2742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2743 .rpc_argp = &args,
2744 .rpc_resp = &res,
2745 };
2746
Trond Myklebust0e574af2005-10-27 22:12:38 -04002747 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002748 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
David Quigley1775fd32013-05-22 12:50:42 -04002751static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2752 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
2754 struct nfs4_exception exception = { };
2755 int err;
2756 do {
2757 err = nfs4_handle_exception(server,
David Quigley1775fd32013-05-22 12:50:42 -04002758 _nfs4_proc_getattr(server, fhandle, fattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 &exception);
2760 } while (exception.retry);
2761 return err;
2762}
2763
2764/*
2765 * The file is not closed if it is opened due to the a request to change
2766 * the size of the file. The open call will not be needed once the
2767 * VFS layer lookup-intents are implemented.
2768 *
2769 * Close is called when the inode is destroyed.
2770 * If we haven't opened the file for O_WRONLY, we
2771 * need to in the size_change case to obtain a stateid.
2772 *
2773 * Got race?
2774 * Because OPEN is always done by name in nfsv4, it is
2775 * possible that we opened a different file by the same
2776 * name. We can recognize this race condition, but we
2777 * can't do anything about it besides returning an error.
2778 *
2779 * This will be fixed with VFS changes (lookup-intent).
2780 */
2781static int
2782nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2783 struct iattr *sattr)
2784{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002785 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002786 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002787 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 int status;
2789
Benny Halevy8a1636c2010-07-14 15:43:57 -04002790 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002791 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002792
Trond Myklebust0e574af2005-10-27 22:12:38 -04002793 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Andy Adamson26699402012-05-30 16:12:24 -04002795 /* Deal with open(O_TRUNC) */
2796 if (sattr->ia_valid & ATTR_OPEN)
2797 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2798
2799 /* Optimization: if the end result is no change, don't RPC */
2800 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2801 return 0;
2802
Trond Myklebustd5308382005-11-04 15:33:38 -05002803 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002804 if (sattr->ia_valid & ATTR_FILE) {
2805 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002806
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002807 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002808 if (ctx) {
2809 cred = ctx->cred;
2810 state = ctx->state;
2811 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002812 }
2813
David Quigley1775fd32013-05-22 12:50:42 -04002814 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, NULL);
Trond Myklebust65e43082005-08-16 11:49:44 -04002815 if (status == 0)
2816 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return status;
2818}
2819
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002820static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2821 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04002822 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04002823{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002824 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002825 int status;
2826 struct nfs4_lookup_arg args = {
2827 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002828 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002829 .name = name,
2830 };
2831 struct nfs4_lookup_res res = {
2832 .server = server,
2833 .fattr = fattr,
2834 .fh = fhandle,
2835 };
2836 struct rpc_message msg = {
2837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2838 .rpc_argp = &args,
2839 .rpc_resp = &res,
2840 };
2841
2842 nfs_fattr_init(fattr);
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002845 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 dprintk("NFS reply lookup: %d\n", status);
2847 return status;
2848}
2849
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002850static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002851{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002852 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002853 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002854 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2855 fattr->nlink = 2;
2856}
2857
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002858static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2859 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04002860 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002861{
2862 struct nfs4_exception exception = { };
2863 struct rpc_clnt *client = *clnt;
2864 int err;
2865 do {
David Quigley1775fd32013-05-22 12:50:42 -04002866 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002867 switch (err) {
2868 case -NFS4ERR_BADNAME:
2869 err = -ENOENT;
2870 goto out;
2871 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002872 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002873 goto out;
2874 case -NFS4ERR_WRONGSEC:
2875 err = -EPERM;
2876 if (client != *clnt)
2877 goto out;
2878
2879 client = nfs4_create_sec_client(client, dir, name);
2880 if (IS_ERR(client))
2881 return PTR_ERR(client);
2882
2883 exception.retry = 1;
2884 break;
2885 default:
2886 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2887 }
2888 } while (exception.retry);
2889
2890out:
2891 if (err == 0)
2892 *clnt = client;
2893 else if (client != *clnt)
2894 rpc_shutdown_client(client);
2895
2896 return err;
2897}
2898
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002899static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04002900 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
2901 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002903 int status;
2904 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002905
David Quigley1775fd32013-05-22 12:50:42 -04002906 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002907 if (client != NFS_CLIENT(dir)) {
2908 rpc_shutdown_client(client);
2909 nfs_fixup_secinfo_attributes(fattr);
2910 }
2911 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002914struct rpc_clnt *
2915nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2916 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2917{
2918 int status;
2919 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2920
David Quigley1775fd32013-05-22 12:50:42 -04002921 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002922 if (status < 0) {
2923 rpc_shutdown_client(client);
2924 return ERR_PTR(status);
2925 }
2926 return client;
2927}
2928
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2930{
Trond Myklebust76b32992007-08-10 17:45:11 -04002931 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 struct nfs4_accessargs args = {
2933 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002934 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002936 struct nfs4_accessres res = {
2937 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002938 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 struct rpc_message msg = {
2940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2941 .rpc_argp = &args,
2942 .rpc_resp = &res,
2943 .rpc_cred = entry->cred,
2944 };
2945 int mode = entry->mask;
2946 int status;
2947
2948 /*
2949 * Determine which access bits we want to ask for...
2950 */
2951 if (mode & MAY_READ)
2952 args.access |= NFS4_ACCESS_READ;
2953 if (S_ISDIR(inode->i_mode)) {
2954 if (mode & MAY_WRITE)
2955 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2956 if (mode & MAY_EXEC)
2957 args.access |= NFS4_ACCESS_LOOKUP;
2958 } else {
2959 if (mode & MAY_WRITE)
2960 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2961 if (mode & MAY_EXEC)
2962 args.access |= NFS4_ACCESS_EXECUTE;
2963 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002964
2965 res.fattr = nfs_alloc_fattr();
2966 if (res.fattr == NULL)
2967 return -ENOMEM;
2968
Bryan Schumaker7c513052011-03-24 17:12:24 +00002969 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002971 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002972 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002974 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return status;
2976}
2977
2978static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2979{
2980 struct nfs4_exception exception = { };
2981 int err;
2982 do {
2983 err = nfs4_handle_exception(NFS_SERVER(inode),
2984 _nfs4_proc_access(inode, entry),
2985 &exception);
2986 } while (exception.retry);
2987 return err;
2988}
2989
2990/*
2991 * TODO: For the time being, we don't try to get any attributes
2992 * along with any of the zero-copy operations READ, READDIR,
2993 * READLINK, WRITE.
2994 *
2995 * In the case of the first three, we want to put the GETATTR
2996 * after the read-type operation -- this is because it is hard
2997 * to predict the length of a GETATTR response in v4, and thus
2998 * align the READ data correctly. This means that the GETATTR
2999 * may end up partially falling into the page cache, and we should
3000 * shift it into the 'tail' of the xdr_buf before processing.
3001 * To do this efficiently, we need to know the total length
3002 * of data received, which doesn't seem to be available outside
3003 * of the RPC layer.
3004 *
3005 * In the case of WRITE, we also want to put the GETATTR after
3006 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003007 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 *
3009 * Both of these changes to the XDR layer would in fact be quite
3010 * minor, but I decided to leave them for a subsequent patch.
3011 */
3012static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3013 unsigned int pgbase, unsigned int pglen)
3014{
3015 struct nfs4_readlink args = {
3016 .fh = NFS_FH(inode),
3017 .pgbase = pgbase,
3018 .pglen = pglen,
3019 .pages = &page,
3020 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003021 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 struct rpc_message msg = {
3023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3024 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003025 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 };
3027
Bryan Schumaker7c513052011-03-24 17:12:24 +00003028 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 -07003029}
3030
3031static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3032 unsigned int pgbase, unsigned int pglen)
3033{
3034 struct nfs4_exception exception = { };
3035 int err;
3036 do {
3037 err = nfs4_handle_exception(NFS_SERVER(inode),
3038 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3039 &exception);
3040 } while (exception.retry);
3041 return err;
3042}
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003045 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047static int
3048nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003049 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
David Quigley1775fd32013-05-22 12:50:42 -04003051 struct nfs4_label *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003052 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 int status = 0;
3055
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003056 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3057 if (IS_ERR(ctx))
3058 return PTR_ERR(ctx);
3059
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003060 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003061 state = nfs4_do_open(dir, dentry, ctx->mode,
David Quigley1775fd32013-05-22 12:50:42 -04003062 flags, sattr, ilabel, ctx->cred,
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003063 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003064 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (IS_ERR(state)) {
3066 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003067 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003069 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003070 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003071 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003073 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return status;
3075}
3076
3077static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3078{
Trond Myklebust16e42952005-10-27 22:12:44 -04003079 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003080 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003082 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003084 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003085 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003086 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3089 .rpc_argp = &args,
3090 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003092 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003093
Bryan Schumaker7c513052011-03-24 17:12:24 +00003094 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003095 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003096 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return status;
3098}
3099
3100static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3101{
3102 struct nfs4_exception exception = { };
3103 int err;
3104 do {
3105 err = nfs4_handle_exception(NFS_SERVER(dir),
3106 _nfs4_proc_remove(dir, name),
3107 &exception);
3108 } while (exception.retry);
3109 return err;
3110}
3111
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003112static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003114 struct nfs_server *server = NFS_SERVER(dir);
3115 struct nfs_removeargs *args = msg->rpc_argp;
3116 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003118 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003120 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003123static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3124{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003125 nfs4_setup_sequence(NFS_SERVER(data->dir),
3126 &data->args.seq_args,
3127 &data->res.seq_res,
3128 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003131static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003133 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3134
Trond Myklebust14516c32010-07-31 14:29:06 -04003135 if (!nfs4_sequence_done(task, &res->seq_res))
3136 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003137 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003138 return 0;
3139 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003140 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
Jeff Laytond3d41522010-09-17 17:31:57 -04003143static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3144{
3145 struct nfs_server *server = NFS_SERVER(dir);
3146 struct nfs_renameargs *arg = msg->rpc_argp;
3147 struct nfs_renameres *res = msg->rpc_resp;
3148
3149 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003150 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003151 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003152}
3153
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003154static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3155{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003156 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3157 &data->args.seq_args,
3158 &data->res.seq_res,
3159 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003160}
3161
3162static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3163 struct inode *new_dir)
3164{
3165 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3166
3167 if (!nfs4_sequence_done(task, &res->seq_res))
3168 return 0;
3169 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3170 return 0;
3171
3172 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003173 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003174 return 1;
3175}
3176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3178 struct inode *new_dir, struct qstr *new_name)
3179{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003180 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003181 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 .old_dir = NFS_FH(old_dir),
3183 .new_dir = NFS_FH(new_dir),
3184 .old_name = old_name,
3185 .new_name = new_name,
3186 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003187 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003188 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003189 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 struct rpc_message msg = {
3191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3192 .rpc_argp = &arg,
3193 .rpc_resp = &res,
3194 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003195 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Bryan Schumaker7c513052011-03-24 17:12:24 +00003197 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (!status) {
3199 update_changeattr(old_dir, &res.old_cinfo);
3200 update_changeattr(new_dir, &res.new_cinfo);
3201 }
3202 return status;
3203}
3204
3205static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3206 struct inode *new_dir, struct qstr *new_name)
3207{
3208 struct nfs4_exception exception = { };
3209 int err;
3210 do {
3211 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3212 _nfs4_proc_rename(old_dir, old_name,
3213 new_dir, new_name),
3214 &exception);
3215 } while (exception.retry);
3216 return err;
3217}
3218
3219static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3220{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003221 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 struct nfs4_link_arg arg = {
3223 .fh = NFS_FH(inode),
3224 .dir_fh = NFS_FH(dir),
3225 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003226 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003228 struct nfs4_link_res res = {
3229 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003230 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003231 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 struct rpc_message msg = {
3233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3234 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003235 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003237 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Trond Myklebust136f2622010-04-16 16:22:49 -04003239 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003240 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003241 goto out;
3242
Bryan Schumaker7c513052011-03-24 17:12:24 +00003243 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003244 if (!status) {
3245 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003246 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003247 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003248out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003249 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return status;
3251}
3252
3253static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3254{
3255 struct nfs4_exception exception = { };
3256 int err;
3257 do {
3258 err = nfs4_handle_exception(NFS_SERVER(inode),
3259 _nfs4_proc_link(inode, dir, name),
3260 &exception);
3261 } while (exception.retry);
3262 return err;
3263}
3264
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003265struct nfs4_createdata {
3266 struct rpc_message msg;
3267 struct nfs4_create_arg arg;
3268 struct nfs4_create_res res;
3269 struct nfs_fh fh;
3270 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003271 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003272};
3273
3274static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3275 struct qstr *name, struct iattr *sattr, u32 ftype)
3276{
3277 struct nfs4_createdata *data;
3278
3279 data = kzalloc(sizeof(*data), GFP_KERNEL);
3280 if (data != NULL) {
3281 struct nfs_server *server = NFS_SERVER(dir);
3282
3283 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3284 data->msg.rpc_argp = &data->arg;
3285 data->msg.rpc_resp = &data->res;
3286 data->arg.dir_fh = NFS_FH(dir);
3287 data->arg.server = server;
3288 data->arg.name = name;
3289 data->arg.attrs = sattr;
3290 data->arg.ftype = ftype;
3291 data->arg.bitmask = server->attr_bitmask;
3292 data->res.server = server;
3293 data->res.fh = &data->fh;
3294 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003295 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003296 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003297 }
3298 return data;
3299}
3300
3301static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3302{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003303 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003304 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003305 if (status == 0) {
3306 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003307 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003308 }
3309 return status;
3310}
3311
3312static void nfs4_free_createdata(struct nfs4_createdata *data)
3313{
3314 kfree(data);
3315}
3316
Chuck Lever4f390c12006-08-22 20:06:22 -04003317static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003318 struct page *page, unsigned int len, struct iattr *sattr,
3319 struct nfs4_label *label)
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;
David Quigley1775fd32013-05-22 12:50:42 -04003335 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003337 status = nfs4_do_create(dir, dentry, data);
3338
3339 nfs4_free_createdata(data);
3340out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 return status;
3342}
3343
Chuck Lever4f390c12006-08-22 20:06:22 -04003344static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003345 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
3347 struct nfs4_exception exception = { };
David Quigley1775fd32013-05-22 12:50:42 -04003348 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 int err;
3350 do {
3351 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003352 _nfs4_proc_symlink(dir, dentry, page,
David Quigley1775fd32013-05-22 12:50:42 -04003353 len, sattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 &exception);
3355 } while (exception.retry);
3356 return err;
3357}
3358
3359static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003360 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003362 struct nfs4_createdata *data;
3363 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003365 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3366 if (data == NULL)
3367 goto out;
3368
David Quigley1775fd32013-05-22 12:50:42 -04003369 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003370 status = nfs4_do_create(dir, dentry, data);
3371
3372 nfs4_free_createdata(data);
3373out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 return status;
3375}
3376
3377static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3378 struct iattr *sattr)
3379{
3380 struct nfs4_exception exception = { };
David Quigley1775fd32013-05-22 12:50:42 -04003381 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003383
3384 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 do {
3386 err = nfs4_handle_exception(NFS_SERVER(dir),
David Quigley1775fd32013-05-22 12:50:42 -04003387 _nfs4_proc_mkdir(dir, dentry, sattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 &exception);
3389 } while (exception.retry);
3390 return err;
3391}
3392
3393static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003394 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
3396 struct inode *dir = dentry->d_inode;
3397 struct nfs4_readdir_arg args = {
3398 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003399 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 .pgbase = 0,
3401 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003402 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003403 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 };
3405 struct nfs4_readdir_res res;
3406 struct rpc_message msg = {
3407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3408 .rpc_argp = &args,
3409 .rpc_resp = &res,
3410 .rpc_cred = cred,
3411 };
3412 int status;
3413
Harvey Harrison3110ff82008-05-02 13:42:44 -07003414 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003415 dentry->d_parent->d_name.name,
3416 dentry->d_name.name,
3417 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003418 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003420 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 -05003421 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003422 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003423 status += args.pgbase;
3424 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003425
3426 nfs_invalidate_atime(dir);
3427
Harvey Harrison3110ff82008-05-02 13:42:44 -07003428 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 return status;
3430}
3431
3432static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003433 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
3435 struct nfs4_exception exception = { };
3436 int err;
3437 do {
3438 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3439 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003440 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 &exception);
3442 } while (exception.retry);
3443 return err;
3444}
3445
3446static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3447 struct iattr *sattr, dev_t rdev)
3448{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003449 struct nfs4_createdata *data;
3450 int mode = sattr->ia_mode;
3451 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003453 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3454 if (data == NULL)
3455 goto out;
3456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003458 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003460 data->arg.ftype = NF4BLK;
3461 data->arg.u.device.specdata1 = MAJOR(rdev);
3462 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 }
3464 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003465 data->arg.ftype = NF4CHR;
3466 data->arg.u.device.specdata1 = MAJOR(rdev);
3467 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003468 } else if (!S_ISSOCK(mode)) {
3469 status = -EINVAL;
3470 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
David Quigley1775fd32013-05-22 12:50:42 -04003472
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003473 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003474out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003475 nfs4_free_createdata(data);
3476out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 return status;
3478}
3479
3480static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3481 struct iattr *sattr, dev_t rdev)
3482{
3483 struct nfs4_exception exception = { };
3484 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003485
3486 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 do {
3488 err = nfs4_handle_exception(NFS_SERVER(dir),
3489 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3490 &exception);
3491 } while (exception.retry);
3492 return err;
3493}
3494
3495static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3496 struct nfs_fsstat *fsstat)
3497{
3498 struct nfs4_statfs_arg args = {
3499 .fh = fhandle,
3500 .bitmask = server->attr_bitmask,
3501 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003502 struct nfs4_statfs_res res = {
3503 .fsstat = fsstat,
3504 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 struct rpc_message msg = {
3506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3507 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003508 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 };
3510
Trond Myklebust0e574af2005-10-27 22:12:38 -04003511 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003512 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
3514
3515static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3516{
3517 struct nfs4_exception exception = { };
3518 int err;
3519 do {
3520 err = nfs4_handle_exception(server,
3521 _nfs4_proc_statfs(server, fhandle, fsstat),
3522 &exception);
3523 } while (exception.retry);
3524 return err;
3525}
3526
3527static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3528 struct nfs_fsinfo *fsinfo)
3529{
3530 struct nfs4_fsinfo_arg args = {
3531 .fh = fhandle,
3532 .bitmask = server->attr_bitmask,
3533 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003534 struct nfs4_fsinfo_res res = {
3535 .fsinfo = fsinfo,
3536 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 struct rpc_message msg = {
3538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3539 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003540 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 };
3542
Bryan Schumaker7c513052011-03-24 17:12:24 +00003543 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
3546static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3547{
3548 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003549 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 int err;
3551
3552 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003553 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3554 if (err == 0) {
3555 struct nfs_client *clp = server->nfs_client;
3556
3557 spin_lock(&clp->cl_lock);
3558 clp->cl_lease_time = fsinfo->lease_time * HZ;
3559 clp->cl_last_renewal = now;
3560 spin_unlock(&clp->cl_lock);
3561 break;
3562 }
3563 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 } while (exception.retry);
3565 return err;
3566}
3567
3568static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3569{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003570 int error;
3571
Trond Myklebust0e574af2005-10-27 22:12:38 -04003572 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003573 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003574 if (error == 0) {
3575 /* block layout checks this! */
3576 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003577 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003578 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003579
3580 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
3583static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3584 struct nfs_pathconf *pathconf)
3585{
3586 struct nfs4_pathconf_arg args = {
3587 .fh = fhandle,
3588 .bitmask = server->attr_bitmask,
3589 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003590 struct nfs4_pathconf_res res = {
3591 .pathconf = pathconf,
3592 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 struct rpc_message msg = {
3594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3595 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003596 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 };
3598
3599 /* None of the pathconf attributes are mandatory to implement */
3600 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3601 memset(pathconf, 0, sizeof(*pathconf));
3602 return 0;
3603 }
3604
Trond Myklebust0e574af2005-10-27 22:12:38 -04003605 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003606 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
3608
3609static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3610 struct nfs_pathconf *pathconf)
3611{
3612 struct nfs4_exception exception = { };
3613 int err;
3614
3615 do {
3616 err = nfs4_handle_exception(server,
3617 _nfs4_proc_pathconf(server, fhandle, pathconf),
3618 &exception);
3619 } while (exception.retry);
3620 return err;
3621}
3622
Trond Myklebust5521abf2013-03-16 20:54:34 -04003623int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003624 const struct nfs_open_context *ctx,
3625 const struct nfs_lock_context *l_ctx,
3626 fmode_t fmode)
3627{
3628 const struct nfs_lockowner *lockowner = NULL;
3629
3630 if (l_ctx != NULL)
3631 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003632 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003633}
3634EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3635
Trond Myklebust5521abf2013-03-16 20:54:34 -04003636static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3637 const struct nfs_open_context *ctx,
3638 const struct nfs_lock_context *l_ctx,
3639 fmode_t fmode)
3640{
3641 nfs4_stateid current_stateid;
3642
3643 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3644 return false;
3645 return nfs4_stateid_match(stateid, &current_stateid);
3646}
3647
3648static bool nfs4_error_stateid_expired(int err)
3649{
3650 switch (err) {
3651 case -NFS4ERR_DELEG_REVOKED:
3652 case -NFS4ERR_ADMIN_REVOKED:
3653 case -NFS4ERR_BAD_STATEID:
3654 case -NFS4ERR_STALE_STATEID:
3655 case -NFS4ERR_OLD_STATEID:
3656 case -NFS4ERR_OPENMODE:
3657 case -NFS4ERR_EXPIRED:
3658 return true;
3659 }
3660 return false;
3661}
3662
Benny Halevyd20581a2011-05-22 19:52:03 +03003663void __nfs4_read_done_cb(struct nfs_read_data *data)
3664{
Fred Isamancd841602012-04-20 14:47:44 -04003665 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003666}
3667
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003668static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669{
Fred Isamancd841602012-04-20 14:47:44 -04003670 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003672 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003673 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003674 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003676
Benny Halevyd20581a2011-05-22 19:52:03 +03003677 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003679 renew_lease(server, data->timestamp);
3680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
Trond Myklebust5521abf2013-03-16 20:54:34 -04003683static bool nfs4_read_stateid_changed(struct rpc_task *task,
3684 struct nfs_readargs *args)
3685{
3686
3687 if (!nfs4_error_stateid_expired(task->tk_status) ||
3688 nfs4_stateid_is_current(&args->stateid,
3689 args->context,
3690 args->lock_context,
3691 FMODE_READ))
3692 return false;
3693 rpc_restart_call_prepare(task);
3694 return true;
3695}
3696
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003697static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3698{
3699
3700 dprintk("--> %s\n", __func__);
3701
3702 if (!nfs4_sequence_done(task, &data->res.seq_res))
3703 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003704 if (nfs4_read_stateid_changed(task, &data->args))
3705 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003706 return data->read_done_cb ? data->read_done_cb(task, data) :
3707 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003708}
3709
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003710static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003713 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003714 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003715 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716}
3717
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003718static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3719{
Trond Myklebust9b206142013-03-17 15:52:00 -04003720 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003721 &data->args.seq_args,
3722 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003723 task))
3724 return;
3725 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3726 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727}
3728
Fred Isamanb029bc92011-03-03 15:13:42 +00003729static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
Fred Isamancd841602012-04-20 14:47:44 -04003731 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003733 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003734 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003735 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003737 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003739 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003740 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
Trond Myklebust5521abf2013-03-16 20:54:34 -04003744static bool nfs4_write_stateid_changed(struct rpc_task *task,
3745 struct nfs_writeargs *args)
3746{
3747
3748 if (!nfs4_error_stateid_expired(task->tk_status) ||
3749 nfs4_stateid_is_current(&args->stateid,
3750 args->context,
3751 args->lock_context,
3752 FMODE_WRITE))
3753 return false;
3754 rpc_restart_call_prepare(task);
3755 return true;
3756}
3757
Fred Isamanb029bc92011-03-03 15:13:42 +00003758static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3759{
3760 if (!nfs4_sequence_done(task, &data->res.seq_res))
3761 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003762 if (nfs4_write_stateid_changed(task, &data->args))
3763 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003764 return data->write_done_cb ? data->write_done_cb(task, data) :
3765 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003766}
3767
Trond Myklebust5a37f852012-04-28 14:55:16 -04003768static
3769bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003770{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003771 const struct nfs_pgio_header *hdr = data->header;
3772
3773 /* Don't request attributes for pNFS or O_DIRECT writes */
3774 if (data->ds_clp != NULL || hdr->dreq != NULL)
3775 return false;
3776 /* Otherwise, request attributes if and only if we don't hold
3777 * a delegation
3778 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003779 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003780}
Fred Isamana69aef12011-03-03 15:13:47 +00003781
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003782static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
Fred Isamancd841602012-04-20 14:47:44 -04003784 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003785
Trond Myklebust5a37f852012-04-28 14:55:16 -04003786 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003787 data->args.bitmask = NULL;
3788 data->res.fattr = NULL;
3789 } else
3790 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003791
Fred Isamanb029bc92011-03-03 15:13:42 +00003792 if (!data->write_done_cb)
3793 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003794 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 data->timestamp = jiffies;
3796
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003797 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003798 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799}
3800
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003801static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3802{
Trond Myklebust9b206142013-03-17 15:52:00 -04003803 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003804 &data->args.seq_args,
3805 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003806 task))
3807 return;
3808 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3809 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810}
3811
Fred Isaman0b7c0152012-04-20 14:47:39 -04003812static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3813{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003814 nfs4_setup_sequence(NFS_SERVER(data->inode),
3815 &data->args.seq_args,
3816 &data->res.seq_res,
3817 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818}
3819
Fred Isaman0b7c0152012-04-20 14:47:39 -04003820static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003823
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003824 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003825 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003826 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003828 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829}
3830
Fred Isaman0b7c0152012-04-20 14:47:39 -04003831static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003832{
3833 if (!nfs4_sequence_done(task, &data->res.seq_res))
3834 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003835 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003836}
3837
Fred Isaman0b7c0152012-04-20 14:47:39 -04003838static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003840 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003841
Fred Isaman0b7c0152012-04-20 14:47:39 -04003842 if (data->commit_done_cb == NULL)
3843 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003844 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003845 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003846 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847}
3848
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003849struct nfs4_renewdata {
3850 struct nfs_client *client;
3851 unsigned long timestamp;
3852};
3853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854/*
3855 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3856 * standalone procedure for queueing an asynchronous RENEW.
3857 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003858static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003859{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003860 struct nfs4_renewdata *data = calldata;
3861 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003862
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003863 if (atomic_read(&clp->cl_count) > 1)
3864 nfs4_schedule_state_renewal(clp);
3865 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003866 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003867}
3868
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003869static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003871 struct nfs4_renewdata *data = calldata;
3872 struct nfs_client *clp = data->client;
3873 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
3875 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003876 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003877 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3878 return;
3879 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003880 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003881 return;
3882 }
3883 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003885 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886}
3887
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003888static const struct rpc_call_ops nfs4_renew_ops = {
3889 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003890 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003891};
3892
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003893static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894{
3895 struct rpc_message msg = {
3896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3897 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003898 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003900 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003902 if (renew_flags == 0)
3903 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003904 if (!atomic_inc_not_zero(&clp->cl_count))
3905 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003906 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003907 if (data == NULL)
3908 return -ENOMEM;
3909 data->client = clp;
3910 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003911 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003912 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913}
3914
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003915static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
3917 struct rpc_message msg = {
3918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3919 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003920 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 };
3922 unsigned long now = jiffies;
3923 int status;
3924
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003925 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 if (status < 0)
3927 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003928 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return 0;
3930}
3931
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003932static inline int nfs4_server_supports_acls(struct nfs_server *server)
3933{
3934 return (server->caps & NFS_CAP_ACLS)
3935 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3936 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3937}
3938
Trond Myklebust21f498c2012-08-24 10:59:25 -04003939/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3940 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003941 * the stack.
3942 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003943#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003944
Neil Hormane9e3d722011-03-04 19:26:03 -05003945static int buf_to_pages_noslab(const void *buf, size_t buflen,
3946 struct page **pages, unsigned int *pgbase)
3947{
3948 struct page *newpage, **spages;
3949 int rc = 0;
3950 size_t len;
3951 spages = pages;
3952
3953 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003954 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003955 newpage = alloc_page(GFP_KERNEL);
3956
3957 if (newpage == NULL)
3958 goto unwind;
3959 memcpy(page_address(newpage), buf, len);
3960 buf += len;
3961 buflen -= len;
3962 *pages++ = newpage;
3963 rc++;
3964 } while (buflen != 0);
3965
3966 return rc;
3967
3968unwind:
3969 for(; rc > 0; rc--)
3970 __free_page(spages[rc-1]);
3971 return -ENOMEM;
3972}
3973
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003974struct nfs4_cached_acl {
3975 int cached;
3976 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003977 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003978};
3979
3980static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003981{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003982 struct nfs_inode *nfsi = NFS_I(inode);
3983
3984 spin_lock(&inode->i_lock);
3985 kfree(nfsi->nfs4_acl);
3986 nfsi->nfs4_acl = acl;
3987 spin_unlock(&inode->i_lock);
3988}
3989
3990static void nfs4_zap_acl_attr(struct inode *inode)
3991{
3992 nfs4_set_cached_acl(inode, NULL);
3993}
3994
3995static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3996{
3997 struct nfs_inode *nfsi = NFS_I(inode);
3998 struct nfs4_cached_acl *acl;
3999 int ret = -ENOENT;
4000
4001 spin_lock(&inode->i_lock);
4002 acl = nfsi->nfs4_acl;
4003 if (acl == NULL)
4004 goto out;
4005 if (buf == NULL) /* user is just asking for length */
4006 goto out_len;
4007 if (acl->cached == 0)
4008 goto out;
4009 ret = -ERANGE; /* see getxattr(2) man page */
4010 if (acl->len > buflen)
4011 goto out;
4012 memcpy(buf, acl->data, acl->len);
4013out_len:
4014 ret = acl->len;
4015out:
4016 spin_unlock(&inode->i_lock);
4017 return ret;
4018}
4019
Sachin Prabhu5794d212012-04-17 14:36:40 +01004020static 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 +00004021{
4022 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004023 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004024
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004025 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004026 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004027 if (acl == NULL)
4028 goto out;
4029 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004030 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004031 } else {
4032 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4033 if (acl == NULL)
4034 goto out;
4035 acl->cached = 0;
4036 }
4037 acl->len = acl_len;
4038out:
4039 nfs4_set_cached_acl(inode, acl);
4040}
4041
Andy Adamsonbf118a32011-12-07 11:55:27 -05004042/*
4043 * The getxattr API returns the required buffer length when called with a
4044 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4045 * the required buf. On a NULL buf, we send a page of data to the server
4046 * guessing that the ACL request can be serviced by a page. If so, we cache
4047 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4048 * the cache. If not so, we throw away the page, and cache the required
4049 * length. The next getxattr call will then produce another round trip to
4050 * the server, this time with the input buf of the required size.
4051 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004052static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004053{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004054 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004055 struct nfs_getaclargs args = {
4056 .fh = NFS_FH(inode),
4057 .acl_pages = pages,
4058 .acl_len = buflen,
4059 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004060 struct nfs_getaclres res = {
4061 .acl_len = buflen,
4062 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004063 struct rpc_message msg = {
4064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4065 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004066 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004067 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004068 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4069 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004070
Andy Adamsonbf118a32011-12-07 11:55:27 -05004071 /* As long as we're doing a round trip to the server anyway,
4072 * let's be prepared for a page of acl data. */
4073 if (npages == 0)
4074 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004075 if (npages > ARRAY_SIZE(pages))
4076 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004077
Andy Adamsonbf118a32011-12-07 11:55:27 -05004078 for (i = 0; i < npages; i++) {
4079 pages[i] = alloc_page(GFP_KERNEL);
4080 if (!pages[i])
4081 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004082 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004083
4084 /* for decoding across pages */
4085 res.acl_scratch = alloc_page(GFP_KERNEL);
4086 if (!res.acl_scratch)
4087 goto out_free;
4088
Andy Adamsonbf118a32011-12-07 11:55:27 -05004089 args.acl_len = npages * PAGE_SIZE;
4090 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004091
Peng Taode040be2012-01-10 22:42:47 +08004092 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004093 __func__, buf, buflen, npages, args.acl_len);
4094 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4095 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004096 if (ret)
4097 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004098
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004099 /* Handle the case where the passed-in buffer is too short */
4100 if (res.acl_flags & NFS4_ACL_TRUNC) {
4101 /* Did the user only issue a request for the acl length? */
4102 if (buf == NULL)
4103 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004104 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004105 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004106 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004107 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004108 if (buf) {
4109 if (res.acl_len > buflen) {
4110 ret = -ERANGE;
4111 goto out_free;
4112 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004113 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004114 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004115out_ok:
4116 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004117out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004118 for (i = 0; i < npages; i++)
4119 if (pages[i])
4120 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004121 if (res.acl_scratch)
4122 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004123 return ret;
4124}
4125
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004126static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4127{
4128 struct nfs4_exception exception = { };
4129 ssize_t ret;
4130 do {
4131 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4132 if (ret >= 0)
4133 break;
4134 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4135 } while (exception.retry);
4136 return ret;
4137}
4138
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004139static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4140{
4141 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004142 int ret;
4143
4144 if (!nfs4_server_supports_acls(server))
4145 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004146 ret = nfs_revalidate_inode(server, inode);
4147 if (ret < 0)
4148 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004149 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4150 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004151 ret = nfs4_read_cached_acl(inode, buf, buflen);
4152 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004153 /* -ENOENT is returned if there is no ACL or if there is an ACL
4154 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004155 return ret;
4156 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004157}
4158
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004159static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004160{
4161 struct nfs_server *server = NFS_SERVER(inode);
4162 struct page *pages[NFS4ACL_MAXPAGES];
4163 struct nfs_setaclargs arg = {
4164 .fh = NFS_FH(inode),
4165 .acl_pages = pages,
4166 .acl_len = buflen,
4167 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004168 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004169 struct rpc_message msg = {
4170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4171 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004172 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004173 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004174 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004175 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004176
4177 if (!nfs4_server_supports_acls(server))
4178 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004179 if (npages > ARRAY_SIZE(pages))
4180 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004181 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4182 if (i < 0)
4183 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004184 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004185 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004186
4187 /*
4188 * Free each page after tx, so the only ref left is
4189 * held by the network stack
4190 */
4191 for (; i > 0; i--)
4192 put_page(pages[i-1]);
4193
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004194 /*
4195 * Acl update can result in inode attribute update.
4196 * so mark the attribute cache invalid.
4197 */
4198 spin_lock(&inode->i_lock);
4199 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4200 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004201 nfs_access_zap_cache(inode);
4202 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004203 return ret;
4204}
4205
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004206static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4207{
4208 struct nfs4_exception exception = { };
4209 int err;
4210 do {
4211 err = nfs4_handle_exception(NFS_SERVER(inode),
4212 __nfs4_proc_set_acl(inode, buf, buflen),
4213 &exception);
4214 } while (exception.retry);
4215 return err;
4216}
4217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004219nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004221 struct nfs_client *clp = server->nfs_client;
4222
4223 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 return 0;
4225 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004226 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004227 case -NFS4ERR_ADMIN_REVOKED:
4228 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004229 if (state == NULL)
4230 break;
4231 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004232 case -NFS4ERR_OPENMODE:
4233 if (state == NULL)
4234 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004235 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4236 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004237 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004238 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004239 if (state != NULL) {
4240 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4241 goto stateid_invalid;
4242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004244 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004245 nfs4_schedule_lease_recovery(clp);
4246 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004247#if defined(CONFIG_NFS_V4_1)
4248 case -NFS4ERR_BADSESSION:
4249 case -NFS4ERR_BADSLOT:
4250 case -NFS4ERR_BAD_HIGH_SLOT:
4251 case -NFS4ERR_DEADSESSION:
4252 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4253 case -NFS4ERR_SEQ_FALSE_RETRY:
4254 case -NFS4ERR_SEQ_MISORDERED:
4255 dprintk("%s ERROR %d, Reset session\n", __func__,
4256 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004257 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004258 task->tk_status = 0;
4259 return -EAGAIN;
4260#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004262 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004263 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4265 task->tk_status = 0;
4266 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004267 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 case -NFS4ERR_OLD_STATEID:
4269 task->tk_status = 0;
4270 return -EAGAIN;
4271 }
4272 task->tk_status = nfs4_map_errors(task->tk_status);
4273 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004274stateid_invalid:
4275 task->tk_status = -EIO;
4276 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004277wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004278 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004279 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4280 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4281 task->tk_status = 0;
4282 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283}
4284
Chuck Leverf0920752012-05-21 22:45:41 -04004285static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4286 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004287{
4288 __be32 verf[2];
4289
Chuck Lever2c820d92012-05-21 22:45:33 -04004290 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4291 /* An impossible timestamp guarantees this value
4292 * will never match a generated boot time. */
4293 verf[0] = 0;
4294 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4295 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004296 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4297 verf[0] = (__be32)nn->boot_time.tv_sec;
4298 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004299 }
Chuck Levercd937102012-03-02 17:14:31 -05004300 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4301}
4302
Chuck Levere984a552012-09-14 17:24:21 -04004303static unsigned int
4304nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4305 char *buf, size_t len)
4306{
4307 unsigned int result;
4308
4309 rcu_read_lock();
4310 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4311 clp->cl_ipaddr,
4312 rpc_peeraddr2str(clp->cl_rpcclient,
4313 RPC_DISPLAY_ADDR),
4314 rpc_peeraddr2str(clp->cl_rpcclient,
4315 RPC_DISPLAY_PROTO));
4316 rcu_read_unlock();
4317 return result;
4318}
4319
4320static unsigned int
4321nfs4_init_uniform_client_string(const struct nfs_client *clp,
4322 char *buf, size_t len)
4323{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004324 char *nodename = clp->cl_rpcclient->cl_nodename;
4325
4326 if (nfs4_client_id_uniquifier[0] != '\0')
4327 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004328 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4329 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004330 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004331}
4332
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004333/**
4334 * nfs4_proc_setclientid - Negotiate client ID
4335 * @clp: state data structure
4336 * @program: RPC program for NFSv4 callback service
4337 * @port: IP port number for NFS4 callback service
4338 * @cred: RPC credential to use for this call
4339 * @res: where to place the result
4340 *
4341 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4342 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004343int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4344 unsigned short port, struct rpc_cred *cred,
4345 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
4347 nfs4_verifier sc_verifier;
4348 struct nfs4_setclientid setclientid = {
4349 .sc_verifier = &sc_verifier,
4350 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004351 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 };
4353 struct rpc_message msg = {
4354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4355 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004356 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004357 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004359 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Chuck Leverde734832012-07-11 16:30:50 -04004361 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004362 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004363 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4364 setclientid.sc_name_len =
4365 nfs4_init_uniform_client_string(clp,
4366 setclientid.sc_name,
4367 sizeof(setclientid.sc_name));
4368 else
4369 setclientid.sc_name_len =
4370 nfs4_init_nonuniform_client_string(clp,
4371 setclientid.sc_name,
4372 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004373 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004374 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004375 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004376 sizeof(setclientid.sc_netid),
4377 rpc_peeraddr2str(clp->cl_rpcclient,
4378 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004379 rcu_read_unlock();
4380 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004381 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 clp->cl_ipaddr, port >> 8, port & 255);
4383
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004384 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4385 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4386 setclientid.sc_name_len, setclientid.sc_name);
4387 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4388 dprintk("NFS reply setclientid: %d\n", status);
4389 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390}
4391
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004392/**
4393 * nfs4_proc_setclientid_confirm - Confirm client ID
4394 * @clp: state data structure
4395 * @res: result of a previous SETCLIENTID
4396 * @cred: RPC credential to use for this call
4397 *
4398 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4399 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004400int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004401 struct nfs4_setclientid_res *arg,
4402 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 struct rpc_message msg = {
4405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004406 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004407 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 int status;
4410
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004411 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4412 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4413 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004414 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004415 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 return status;
4417}
4418
Trond Myklebustfe650402006-01-03 09:55:18 +01004419struct nfs4_delegreturndata {
4420 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004421 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004422 struct nfs_fh fh;
4423 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004424 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004425 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004426 int rpc_status;
4427};
4428
Trond Myklebustfe650402006-01-03 09:55:18 +01004429static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4430{
4431 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004432
Trond Myklebust14516c32010-07-31 14:29:06 -04004433 if (!nfs4_sequence_done(task, &data->res.seq_res))
4434 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004435
Ricardo Labiaga79708862009-12-07 09:23:21 -05004436 switch (task->tk_status) {
4437 case -NFS4ERR_STALE_STATEID:
4438 case -NFS4ERR_EXPIRED:
4439 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004440 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004441 break;
4442 default:
4443 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4444 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004445 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004446 return;
4447 }
4448 }
4449 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004450}
4451
4452static void nfs4_delegreturn_release(void *calldata)
4453{
Trond Myklebustfe650402006-01-03 09:55:18 +01004454 kfree(calldata);
4455}
4456
Andy Adamson938e1012009-04-01 09:22:28 -04004457#if defined(CONFIG_NFS_V4_1)
4458static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4459{
4460 struct nfs4_delegreturndata *d_data;
4461
4462 d_data = (struct nfs4_delegreturndata *)data;
4463
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004464 nfs4_setup_sequence(d_data->res.server,
4465 &d_data->args.seq_args,
4466 &d_data->res.seq_res,
4467 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004468}
4469#endif /* CONFIG_NFS_V4_1 */
4470
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004471static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004472#if defined(CONFIG_NFS_V4_1)
4473 .rpc_call_prepare = nfs4_delegreturn_prepare,
4474#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004475 .rpc_call_done = nfs4_delegreturn_done,
4476 .rpc_release = nfs4_delegreturn_release,
4477};
4478
Trond Myklebuste6f81072008-01-24 18:14:34 -05004479static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004480{
4481 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004482 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004483 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004484 struct rpc_message msg = {
4485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4486 .rpc_cred = cred,
4487 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004488 struct rpc_task_setup task_setup_data = {
4489 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004490 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004491 .callback_ops = &nfs4_delegreturn_ops,
4492 .flags = RPC_TASK_ASYNC,
4493 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004494 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004495
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004496 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004497 if (data == NULL)
4498 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004499 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004500 data->args.fhandle = &data->fh;
4501 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004502 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004503 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004504 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004505 data->res.fattr = &data->fattr;
4506 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004507 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004508 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004509 data->rpc_status = 0;
4510
Trond Myklebustc970aa82007-07-14 15:39:59 -04004511 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004512 msg.rpc_argp = &data->args;
4513 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004514 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004515 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004516 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004517 if (!issync)
4518 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004519 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004520 if (status != 0)
4521 goto out;
4522 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004523 if (status == 0)
4524 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4525 else
4526 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004527out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004528 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004529 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
4531
Trond Myklebuste6f81072008-01-24 18:14:34 -05004532int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533{
4534 struct nfs_server *server = NFS_SERVER(inode);
4535 struct nfs4_exception exception = { };
4536 int err;
4537 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004538 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 switch (err) {
4540 case -NFS4ERR_STALE_STATEID:
4541 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 case 0:
4543 return 0;
4544 }
4545 err = nfs4_handle_exception(server, err, &exception);
4546 } while (exception.retry);
4547 return err;
4548}
4549
4550#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4551#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4552
4553/*
4554 * sleep, with exponential backoff, and retry the LOCK operation.
4555 */
4556static unsigned long
4557nfs4_set_lock_task_retry(unsigned long timeout)
4558{
Jeff Laytond3103102011-12-01 22:44:39 +01004559 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 timeout <<= 1;
4561 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4562 return NFS4_LOCK_MAXTIMEOUT;
4563 return timeout;
4564}
4565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4567{
4568 struct inode *inode = state->inode;
4569 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004570 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004571 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004573 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004575 struct nfs_lockt_res res = {
4576 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 };
4578 struct rpc_message msg = {
4579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4580 .rpc_argp = &arg,
4581 .rpc_resp = &res,
4582 .rpc_cred = state->owner->so_cred,
4583 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 struct nfs4_lock_state *lsp;
4585 int status;
4586
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004587 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004588 status = nfs4_set_lock_state(state, request);
4589 if (status != 0)
4590 goto out;
4591 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004592 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004593 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004594 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004595 switch (status) {
4596 case 0:
4597 request->fl_type = F_UNLCK;
4598 break;
4599 case -NFS4ERR_DENIED:
4600 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004602 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004603out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 return status;
4605}
4606
4607static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4608{
4609 struct nfs4_exception exception = { };
4610 int err;
4611
4612 do {
4613 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4614 _nfs4_proc_getlk(state, cmd, request),
4615 &exception);
4616 } while (exception.retry);
4617 return err;
4618}
4619
4620static int do_vfs_lock(struct file *file, struct file_lock *fl)
4621{
4622 int res = 0;
4623 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4624 case FL_POSIX:
4625 res = posix_lock_file_wait(file, fl);
4626 break;
4627 case FL_FLOCK:
4628 res = flock_lock_file_wait(file, fl);
4629 break;
4630 default:
4631 BUG();
4632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 return res;
4634}
4635
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004636struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004637 struct nfs_locku_args arg;
4638 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004639 struct nfs4_lock_state *lsp;
4640 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004641 struct file_lock fl;
4642 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004643 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004644};
4645
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004646static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4647 struct nfs_open_context *ctx,
4648 struct nfs4_lock_state *lsp,
4649 struct nfs_seqid *seqid)
4650{
4651 struct nfs4_unlockdata *p;
4652 struct inode *inode = lsp->ls_state->inode;
4653
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004654 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004655 if (p == NULL)
4656 return NULL;
4657 p->arg.fh = NFS_FH(inode);
4658 p->arg.fl = &p->fl;
4659 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004660 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004661 p->arg.stateid = &lsp->ls_stateid;
4662 p->lsp = lsp;
4663 atomic_inc(&lsp->ls_count);
4664 /* Ensure we don't close file until we're done freeing locks! */
4665 p->ctx = get_nfs_open_context(ctx);
4666 memcpy(&p->fl, fl, sizeof(p->fl));
4667 p->server = NFS_SERVER(inode);
4668 return p;
4669}
4670
Trond Myklebust06f814a2006-01-03 09:55:07 +01004671static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004672{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004673 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004674 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004675 nfs4_put_lock_state(calldata->lsp);
4676 put_nfs_open_context(calldata->ctx);
4677 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004678}
4679
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004680static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004681{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004682 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004683
Trond Myklebust14516c32010-07-31 14:29:06 -04004684 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4685 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004686 switch (task->tk_status) {
4687 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004688 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4689 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004690 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004691 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004692 case -NFS4ERR_BAD_STATEID:
4693 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004694 case -NFS4ERR_STALE_STATEID:
4695 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004696 break;
4697 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004698 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004699 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004700 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004701 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004702}
4703
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004704static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004705{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004706 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004708 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004709 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004710 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004711 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004712 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004713 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004714 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004715 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004716 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004717 &calldata->res.seq_res,
4718 task) != 0)
4719 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004720 return;
4721out_no_action:
4722 task->tk_action = NULL;
4723out_wait:
4724 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725}
4726
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004727static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004728 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004729 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004730 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004731};
4732
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004733static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4734 struct nfs_open_context *ctx,
4735 struct nfs4_lock_state *lsp,
4736 struct nfs_seqid *seqid)
4737{
4738 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004739 struct rpc_message msg = {
4740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4741 .rpc_cred = ctx->cred,
4742 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004743 struct rpc_task_setup task_setup_data = {
4744 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004745 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004746 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004747 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004748 .flags = RPC_TASK_ASYNC,
4749 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004750
Frank Filz137d6ac2007-07-09 15:32:29 -07004751 /* Ensure this is an unlock - when canceling a lock, the
4752 * canceled lock is passed in, and it won't be an unlock.
4753 */
4754 fl->fl_type = F_UNLCK;
4755
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004756 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4757 if (data == NULL) {
4758 nfs_free_seqid(seqid);
4759 return ERR_PTR(-ENOMEM);
4760 }
4761
Trond Myklebust9d12b212012-01-17 22:04:25 -05004762 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004763 msg.rpc_argp = &data->arg;
4764 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004765 task_setup_data.callback_data = data;
4766 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004767}
4768
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4770{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004771 struct inode *inode = state->inode;
4772 struct nfs4_state_owner *sp = state->owner;
4773 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004774 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004775 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004776 struct rpc_task *task;
4777 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004778 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
Trond Myklebust9b073572006-06-29 16:38:34 -04004780 status = nfs4_set_lock_state(state, request);
4781 /* Unlock _before_ we do the RPC call */
4782 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004783 /* Exclude nfs_delegation_claim_locks() */
4784 mutex_lock(&sp->so_delegreturn_mutex);
4785 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004786 down_read(&nfsi->rwsem);
4787 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4788 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004789 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004790 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004791 }
4792 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004793 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004794 if (status != 0)
4795 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004796 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004797 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04004798 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
4799 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004800 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004801 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004802 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004803 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004804 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004805 status = PTR_ERR(task);
4806 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004807 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004808 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004809 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004810out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004811 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004812 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004815struct nfs4_lockdata {
4816 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004817 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004818 struct nfs4_lock_state *lsp;
4819 struct nfs_open_context *ctx;
4820 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004821 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004822 int rpc_status;
4823 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004824 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004825};
4826
4827static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004828 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4829 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004830{
4831 struct nfs4_lockdata *p;
4832 struct inode *inode = lsp->ls_state->inode;
4833 struct nfs_server *server = NFS_SERVER(inode);
4834
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004835 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004836 if (p == NULL)
4837 return NULL;
4838
4839 p->arg.fh = NFS_FH(inode);
4840 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004841 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004842 if (p->arg.open_seqid == NULL)
4843 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004844 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004845 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004846 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004847 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004848 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004849 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004850 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004851 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004852 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004853 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004854 atomic_inc(&lsp->ls_count);
4855 p->ctx = get_nfs_open_context(ctx);
4856 memcpy(&p->fl, fl, sizeof(p->fl));
4857 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004858out_free_seqid:
4859 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004860out_free:
4861 kfree(p);
4862 return NULL;
4863}
4864
4865static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4866{
4867 struct nfs4_lockdata *data = calldata;
4868 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Harvey Harrison3110ff82008-05-02 13:42:44 -07004870 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004871 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004872 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004873 /* Do we need to do an open_to_lock_owner? */
4874 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004875 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004876 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004877 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04004878 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004879 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004880 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004881 } else
4882 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004883 if (!nfs4_valid_open_stateid(state)) {
4884 data->rpc_status = -EBADF;
4885 task->tk_action = NULL;
4886 goto out_release_open_seqid;
4887 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004888 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004889 if (nfs4_setup_sequence(data->server,
4890 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004891 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004892 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004893 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004894out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004895 nfs_release_seqid(data->arg.open_seqid);
4896out_release_lock_seqid:
4897 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004898out_wait:
4899 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004900 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004901}
4902
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004903static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4904{
4905 struct nfs4_lockdata *data = calldata;
4906
Harvey Harrison3110ff82008-05-02 13:42:44 -07004907 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004908
Trond Myklebust14516c32010-07-31 14:29:06 -04004909 if (!nfs4_sequence_done(task, &data->res.seq_res))
4910 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004911
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004912 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004913 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004914 if (data->rpc_status == 0)
4915 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4916 else
4917 goto out;
4918 }
4919 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004920 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004921 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004922 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004923 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004924out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004925 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004926}
4927
4928static void nfs4_lock_release(void *calldata)
4929{
4930 struct nfs4_lockdata *data = calldata;
4931
Harvey Harrison3110ff82008-05-02 13:42:44 -07004932 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004933 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004934 if (data->cancelled != 0) {
4935 struct rpc_task *task;
4936 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4937 data->arg.lock_seqid);
4938 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004939 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004940 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004941 } else
4942 nfs_free_seqid(data->arg.lock_seqid);
4943 nfs4_put_lock_state(data->lsp);
4944 put_nfs_open_context(data->ctx);
4945 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004946 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004947}
4948
4949static const struct rpc_call_ops nfs4_lock_ops = {
4950 .rpc_call_prepare = nfs4_lock_prepare,
4951 .rpc_call_done = nfs4_lock_done,
4952 .rpc_release = nfs4_lock_release,
4953};
4954
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004955static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4956{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004957 switch (error) {
4958 case -NFS4ERR_ADMIN_REVOKED:
4959 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004960 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004961 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004962 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004963 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004964 break;
4965 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004966 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004967 case -NFS4ERR_EXPIRED:
4968 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004969 };
4970}
4971
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004972static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004973{
4974 struct nfs4_lockdata *data;
4975 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004976 struct rpc_message msg = {
4977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4978 .rpc_cred = state->owner->so_cred,
4979 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004980 struct rpc_task_setup task_setup_data = {
4981 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004982 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004983 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004984 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004985 .flags = RPC_TASK_ASYNC,
4986 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004987 int ret;
4988
Harvey Harrison3110ff82008-05-02 13:42:44 -07004989 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004990 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004991 fl->fl_u.nfs4_fl.owner,
4992 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004993 if (data == NULL)
4994 return -ENOMEM;
4995 if (IS_SETLKW(cmd))
4996 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004997 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004998 msg.rpc_argp = &data->arg;
4999 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005000 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005001 if (recovery_type > NFS_LOCK_NEW) {
5002 if (recovery_type == NFS_LOCK_RECLAIM)
5003 data->arg.reclaim = NFS_LOCK_RECLAIM;
5004 nfs4_set_sequence_privileged(&data->arg.seq_args);
5005 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005006 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005007 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005008 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005009 ret = nfs4_wait_for_completion_rpc_task(task);
5010 if (ret == 0) {
5011 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005012 if (ret)
5013 nfs4_handle_setlk_error(data->server, data->lsp,
5014 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005015 } else
5016 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005017 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005018 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005019 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020}
5021
5022static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5023{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005024 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005025 struct nfs4_exception exception = {
5026 .inode = state->inode,
5027 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005028 int err;
5029
5030 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005031 /* Cache the lock if possible... */
5032 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5033 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005034 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005035 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005036 break;
5037 nfs4_handle_exception(server, err, &exception);
5038 } while (exception.retry);
5039 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040}
5041
5042static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5043{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005044 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005045 struct nfs4_exception exception = {
5046 .inode = state->inode,
5047 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005048 int err;
5049
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005050 err = nfs4_set_lock_state(state, request);
5051 if (err != 0)
5052 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005053 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005054 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5055 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005056 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005057 switch (err) {
5058 default:
5059 goto out;
5060 case -NFS4ERR_GRACE:
5061 case -NFS4ERR_DELAY:
5062 nfs4_handle_exception(server, err, &exception);
5063 err = 0;
5064 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005065 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005066out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005067 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068}
5069
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005070#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005071/**
5072 * nfs41_check_expired_locks - possibly free a lock stateid
5073 *
5074 * @state: NFSv4 state for an inode
5075 *
5076 * Returns NFS_OK if recovery for this stateid is now finished.
5077 * Otherwise a negative NFS4ERR value is returned.
5078 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005079static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005080{
Chuck Levereb64cf92012-07-11 16:30:05 -04005081 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005082 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005083 struct nfs_server *server = NFS_SERVER(state->inode);
5084
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005085 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005086 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005087 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5088 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005089 /* Free the stateid unless the server
5090 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005091 if (status != -NFS4ERR_BAD_STATEID)
5092 nfs41_free_stateid(server,
5093 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005094 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005095 ret = status;
5096 }
5097 }
5098 };
5099
5100 return ret;
5101}
5102
5103static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5104{
5105 int status = NFS_OK;
5106
5107 if (test_bit(LK_STATE_IN_USE, &state->flags))
5108 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005109 if (status != NFS_OK)
5110 status = nfs4_lock_expired(state, request);
5111 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005112}
5113#endif
5114
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5116{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005117 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005118 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005119 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005120 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005121 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005123 if ((fl_flags & FL_POSIX) &&
5124 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5125 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005126 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005127 status = nfs4_set_lock_state(state, request);
5128 if (status != 0)
5129 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005130 request->fl_flags |= FL_ACCESS;
5131 status = do_vfs_lock(request->fl_file, request);
5132 if (status < 0)
5133 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005134 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005135 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005136 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005137 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005138 request->fl_flags = fl_flags & ~FL_SLEEP;
5139 status = do_vfs_lock(request->fl_file, request);
5140 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005141 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005142 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5143 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005144 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005145 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005146 goto out;
5147 down_read(&nfsi->rwsem);
5148 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5149 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005150 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005151 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005152 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005153 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005154 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005155 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5156 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005157out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005158 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005159out:
5160 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 return status;
5162}
5163
5164static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5165{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005166 struct nfs4_exception exception = {
5167 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005168 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005169 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 int err;
5171
5172 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005173 err = _nfs4_proc_setlk(state, cmd, request);
5174 if (err == -NFS4ERR_DENIED)
5175 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005177 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 } while (exception.retry);
5179 return err;
5180}
5181
5182static int
5183nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5184{
5185 struct nfs_open_context *ctx;
5186 struct nfs4_state *state;
5187 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5188 int status;
5189
5190 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005191 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 state = ctx->state;
5193
5194 if (request->fl_start < 0 || request->fl_end < 0)
5195 return -EINVAL;
5196
Trond Myklebustd9531262009-07-21 19:22:38 -04005197 if (IS_GETLK(cmd)) {
5198 if (state != NULL)
5199 return nfs4_proc_getlk(state, F_GETLK, request);
5200 return 0;
5201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
5203 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5204 return -EINVAL;
5205
Trond Myklebustd9531262009-07-21 19:22:38 -04005206 if (request->fl_type == F_UNLCK) {
5207 if (state != NULL)
5208 return nfs4_proc_unlck(state, cmd, request);
5209 return 0;
5210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
Trond Myklebustd9531262009-07-21 19:22:38 -04005212 if (state == NULL)
5213 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005214 /*
5215 * Don't rely on the VFS having checked the file open mode,
5216 * since it won't do this for flock() locks.
5217 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005218 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005219 case F_RDLCK:
5220 if (!(filp->f_mode & FMODE_READ))
5221 return -EBADF;
5222 break;
5223 case F_WRLCK:
5224 if (!(filp->f_mode & FMODE_WRITE))
5225 return -EBADF;
5226 }
5227
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 do {
5229 status = nfs4_proc_setlk(state, cmd, request);
5230 if ((status != -EAGAIN) || IS_SETLK(cmd))
5231 break;
5232 timeout = nfs4_set_lock_task_retry(timeout);
5233 status = -ERESTARTSYS;
5234 if (signalled())
5235 break;
5236 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 return status;
5238}
5239
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005240int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005241{
5242 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005243 int err;
5244
5245 err = nfs4_set_lock_state(state, fl);
5246 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005247 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005248 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005249 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005250}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005251
Trond Myklebustcf470c32012-03-07 13:49:12 -05005252struct nfs_release_lockowner_data {
5253 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005254 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005255 struct nfs_release_lockowner_args args;
5256};
5257
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005258static void nfs4_release_lockowner_release(void *calldata)
5259{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005260 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005261 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005262 kfree(calldata);
5263}
5264
Trond Myklebust17280172012-03-11 13:11:00 -04005265static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005266 .rpc_release = nfs4_release_lockowner_release,
5267};
5268
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005269static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005270{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005271 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005272 struct rpc_message msg = {
5273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5274 };
5275
5276 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005277 return -EINVAL;
5278 data = kmalloc(sizeof(*data), GFP_NOFS);
5279 if (!data)
5280 return -ENOMEM;
5281 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005282 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005283 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5284 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5285 data->args.lock_owner.s_dev = server->s_dev;
5286 msg.rpc_argp = &data->args;
5287 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5288 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005289}
5290
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005291#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5292
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005293static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5294 const void *buf, size_t buflen,
5295 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005296{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005297 if (strcmp(key, "") != 0)
5298 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005299
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005300 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005301}
5302
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005303static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5304 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005305{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005306 if (strcmp(key, "") != 0)
5307 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005308
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005309 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005310}
5311
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005312static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5313 size_t list_len, const char *name,
5314 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005315{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005316 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005317
J. Bruce Fields096455a2006-03-20 23:23:42 -05005318 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5319 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005320
5321 if (list && len <= list_len)
5322 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005323 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005324}
5325
Andy Adamson533eb462011-06-13 18:25:56 -04005326/*
5327 * nfs_fhget will use either the mounted_on_fileid or the fileid
5328 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005329static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5330{
Andy Adamson533eb462011-06-13 18:25:56 -04005331 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5332 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5333 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005334 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005335 return;
5336
5337 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005338 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005339 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5340 fattr->nlink = 2;
5341}
5342
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005343static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5344 const struct qstr *name,
5345 struct nfs4_fs_locations *fs_locations,
5346 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005347{
5348 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005349 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005350 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005351 };
5352 struct nfs4_fs_locations_arg args = {
5353 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005354 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005355 .page = page,
5356 .bitmask = bitmask,
5357 };
Benny Halevy22958462009-04-01 09:22:02 -04005358 struct nfs4_fs_locations_res res = {
5359 .fs_locations = fs_locations,
5360 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005361 struct rpc_message msg = {
5362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5363 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005364 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005365 };
5366 int status;
5367
Harvey Harrison3110ff82008-05-02 13:42:44 -07005368 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005369
5370 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5371 * is not supported */
5372 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5373 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5374 else
5375 bitmask[0] |= FATTR4_WORD0_FILEID;
5376
Trond Myklebustc228fd32007-01-13 02:28:11 -05005377 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005378 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005379 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005380 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005381 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005382 return status;
5383}
5384
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005385int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5386 const struct qstr *name,
5387 struct nfs4_fs_locations *fs_locations,
5388 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005389{
5390 struct nfs4_exception exception = { };
5391 int err;
5392 do {
5393 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005394 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005395 &exception);
5396 } while (exception.retry);
5397 return err;
5398}
5399
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005400static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5401{
5402 int status;
5403 struct nfs4_secinfo_arg args = {
5404 .dir_fh = NFS_FH(dir),
5405 .name = name,
5406 };
5407 struct nfs4_secinfo_res res = {
5408 .flavors = flavors,
5409 };
5410 struct rpc_message msg = {
5411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5412 .rpc_argp = &args,
5413 .rpc_resp = &res,
5414 };
5415
5416 dprintk("NFS call secinfo %s\n", name->name);
5417 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5418 dprintk("NFS reply secinfo: %d\n", status);
5419 return status;
5420}
5421
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005422int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5423 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005424{
5425 struct nfs4_exception exception = { };
5426 int err;
5427 do {
5428 err = nfs4_handle_exception(NFS_SERVER(dir),
5429 _nfs4_proc_secinfo(dir, name, flavors),
5430 &exception);
5431 } while (exception.retry);
5432 return err;
5433}
5434
Andy Adamson557134a2009-04-01 09:21:53 -04005435#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005436/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005437 * Check the exchange flags returned by the server for invalid flags, having
5438 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5439 * DS flags set.
5440 */
5441static int nfs4_check_cl_exchange_flags(u32 flags)
5442{
5443 if (flags & ~EXCHGID4_FLAG_MASK_R)
5444 goto out_inval;
5445 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5446 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5447 goto out_inval;
5448 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5449 goto out_inval;
5450 return NFS_OK;
5451out_inval:
5452 return -NFS4ERR_INVAL;
5453}
5454
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005455static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005456nfs41_same_server_scope(struct nfs41_server_scope *a,
5457 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005458{
5459 if (a->server_scope_sz == b->server_scope_sz &&
5460 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5461 return true;
5462
5463 return false;
5464}
5465
Andy Adamson357f54d2010-12-14 10:11:57 -05005466/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005467 * nfs4_proc_bind_conn_to_session()
5468 *
5469 * The 4.1 client currently uses the same TCP connection for the
5470 * fore and backchannel.
5471 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005472int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005473{
5474 int status;
5475 struct nfs41_bind_conn_to_session_res res;
5476 struct rpc_message msg = {
5477 .rpc_proc =
5478 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5479 .rpc_argp = clp,
5480 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005481 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005482 };
5483
5484 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005485
5486 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5487 if (unlikely(res.session == NULL)) {
5488 status = -ENOMEM;
5489 goto out;
5490 }
5491
5492 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5493 if (status == 0) {
5494 if (memcmp(res.session->sess_id.data,
5495 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5496 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5497 status = -EIO;
5498 goto out_session;
5499 }
5500 if (res.dir != NFS4_CDFS4_BOTH) {
5501 dprintk("NFS: %s: Unexpected direction from server\n",
5502 __func__);
5503 status = -EIO;
5504 goto out_session;
5505 }
5506 if (res.use_conn_in_rdma_mode) {
5507 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5508 __func__);
5509 status = -EIO;
5510 goto out_session;
5511 }
5512 }
5513out_session:
5514 kfree(res.session);
5515out:
5516 dprintk("<-- %s status= %d\n", __func__, status);
5517 return status;
5518}
5519
5520/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005521 * nfs4_proc_exchange_id()
5522 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005523 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5524 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005525 * Since the clientid has expired, all compounds using sessions
5526 * associated with the stale clientid will be returning
5527 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5528 * be in some phase of session reset.
5529 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005530int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005531{
5532 nfs4_verifier verifier;
5533 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005534 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005535 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005536 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005537 };
5538 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005539 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005540 };
5541 int status;
5542 struct rpc_message msg = {
5543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5544 .rpc_argp = &args,
5545 .rpc_resp = &res,
5546 .rpc_cred = cred,
5547 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005548
Chuck Leverf0920752012-05-21 22:45:41 -04005549 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005550 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5551 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005552 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5553 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5554 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005555
Chuck Leveracdeb692012-05-21 22:46:16 -04005556 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005557 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005558 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005559 status = -ENOMEM;
5560 goto out;
5561 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005562
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005563 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005564 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005565 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005566 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005567 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005568 }
5569
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005570 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005571 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005572 status = -ENOMEM;
5573 goto out_server_scope;
5574 }
5575
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005576 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005577 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005578 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005579
Chuck Lever177313f2012-05-21 22:44:58 -04005580 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005581 clp->cl_clientid = res.clientid;
5582 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5583 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5584 clp->cl_seqid = res.seqid;
5585
Chuck Leveracdeb692012-05-21 22:46:16 -04005586 kfree(clp->cl_serverowner);
5587 clp->cl_serverowner = res.server_owner;
5588 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005589
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005590 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005591 kfree(clp->cl_implid);
5592 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005593
Chuck Lever177313f2012-05-21 22:44:58 -04005594 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005595 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005596 res.server_scope)) {
5597 dprintk("%s: server_scope mismatch detected\n",
5598 __func__);
5599 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005600 kfree(clp->cl_serverscope);
5601 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005602 }
5603
Chuck Lever177313f2012-05-21 22:44:58 -04005604 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005605 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005606 goto out;
5607 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005608 } else
5609 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005610
Chuck Leveracdeb692012-05-21 22:46:16 -04005611out_server_owner:
5612 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005613out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005614 kfree(res.server_scope);
5615out:
Chuck Lever177313f2012-05-21 22:44:58 -04005616 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005617 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005618 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005619 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005620 clp->cl_implid->date.seconds,
5621 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005622 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005623 return status;
5624}
5625
Trond Myklebust66245532012-05-25 17:18:09 -04005626static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5627 struct rpc_cred *cred)
5628{
5629 struct rpc_message msg = {
5630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5631 .rpc_argp = clp,
5632 .rpc_cred = cred,
5633 };
5634 int status;
5635
5636 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5637 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005638 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005639 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5640 return status;
5641}
5642
5643static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5644 struct rpc_cred *cred)
5645{
5646 unsigned int loop;
5647 int ret;
5648
5649 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5650 ret = _nfs4_proc_destroy_clientid(clp, cred);
5651 switch (ret) {
5652 case -NFS4ERR_DELAY:
5653 case -NFS4ERR_CLIENTID_BUSY:
5654 ssleep(1);
5655 break;
5656 default:
5657 return ret;
5658 }
5659 }
5660 return 0;
5661}
5662
5663int nfs4_destroy_clientid(struct nfs_client *clp)
5664{
5665 struct rpc_cred *cred;
5666 int ret = 0;
5667
5668 if (clp->cl_mvops->minor_version < 1)
5669 goto out;
5670 if (clp->cl_exchange_flags == 0)
5671 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005672 if (clp->cl_preserve_clid)
5673 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005674 cred = nfs4_get_exchange_id_cred(clp);
5675 ret = nfs4_proc_destroy_clientid(clp, cred);
5676 if (cred)
5677 put_rpccred(cred);
5678 switch (ret) {
5679 case 0:
5680 case -NFS4ERR_STALE_CLIENTID:
5681 clp->cl_exchange_flags = 0;
5682 }
5683out:
5684 return ret;
5685}
5686
Andy Adamson2050f0c2009-04-01 09:22:30 -04005687struct nfs4_get_lease_time_data {
5688 struct nfs4_get_lease_time_args *args;
5689 struct nfs4_get_lease_time_res *res;
5690 struct nfs_client *clp;
5691};
5692
5693static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5694 void *calldata)
5695{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005696 struct nfs4_get_lease_time_data *data =
5697 (struct nfs4_get_lease_time_data *)calldata;
5698
5699 dprintk("--> %s\n", __func__);
5700 /* just setup sequence, do not trigger session recovery
5701 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005702 nfs41_setup_sequence(data->clp->cl_session,
5703 &data->args->la_seq_args,
5704 &data->res->lr_seq_res,
5705 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005706 dprintk("<-- %s\n", __func__);
5707}
5708
5709/*
5710 * Called from nfs4_state_manager thread for session setup, so don't recover
5711 * from sequence operation or clientid errors.
5712 */
5713static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5714{
5715 struct nfs4_get_lease_time_data *data =
5716 (struct nfs4_get_lease_time_data *)calldata;
5717
5718 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005719 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5720 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005721 switch (task->tk_status) {
5722 case -NFS4ERR_DELAY:
5723 case -NFS4ERR_GRACE:
5724 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5725 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5726 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005727 /* fall through */
5728 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005729 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005730 return;
5731 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005732 dprintk("<-- %s\n", __func__);
5733}
5734
Trond Myklebust17280172012-03-11 13:11:00 -04005735static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005736 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5737 .rpc_call_done = nfs4_get_lease_time_done,
5738};
5739
5740int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5741{
5742 struct rpc_task *task;
5743 struct nfs4_get_lease_time_args args;
5744 struct nfs4_get_lease_time_res res = {
5745 .lr_fsinfo = fsinfo,
5746 };
5747 struct nfs4_get_lease_time_data data = {
5748 .args = &args,
5749 .res = &res,
5750 .clp = clp,
5751 };
5752 struct rpc_message msg = {
5753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5754 .rpc_argp = &args,
5755 .rpc_resp = &res,
5756 };
5757 struct rpc_task_setup task_setup = {
5758 .rpc_client = clp->cl_rpcclient,
5759 .rpc_message = &msg,
5760 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005761 .callback_data = &data,
5762 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005763 };
5764 int status;
5765
Trond Myklebust9d12b212012-01-17 22:04:25 -05005766 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005767 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005768 dprintk("--> %s\n", __func__);
5769 task = rpc_run_task(&task_setup);
5770
5771 if (IS_ERR(task))
5772 status = PTR_ERR(task);
5773 else {
5774 status = task->tk_status;
5775 rpc_put_task(task);
5776 }
5777 dprintk("<-- %s return %d\n", __func__, status);
5778
5779 return status;
5780}
5781
Andy Adamsonfc931582009-04-01 09:22:31 -04005782/*
5783 * Initialize the values to be used by the client in CREATE_SESSION
5784 * If nfs4_init_session set the fore channel request and response sizes,
5785 * use them.
5786 *
5787 * Set the back channel max_resp_sz_cached to zero to force the client to
5788 * always set csa_cachethis to FALSE because the current implementation
5789 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5790 */
5791static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5792{
5793 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005794 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5795 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005796
5797 if (mxrqst_sz == 0)
5798 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5799 if (mxresp_sz == 0)
5800 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5801 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005802 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5803 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005804 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005805 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005806
5807 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005808 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005809 __func__,
5810 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005811 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005812
5813 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005814 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5815 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5816 args->bc_attrs.max_resp_sz_cached = 0;
5817 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5818 args->bc_attrs.max_reqs = 1;
5819
5820 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5821 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5822 __func__,
5823 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5824 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5825 args->bc_attrs.max_reqs);
5826}
5827
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005828static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005829{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005830 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5831 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5832
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005833 if (rcvd->max_resp_sz > sent->max_resp_sz)
5834 return -EINVAL;
5835 /*
5836 * Our requested max_ops is the minimum we need; we're not
5837 * prepared to break up compounds into smaller pieces than that.
5838 * So, no point even trying to continue if the server won't
5839 * cooperate:
5840 */
5841 if (rcvd->max_ops < sent->max_ops)
5842 return -EINVAL;
5843 if (rcvd->max_reqs == 0)
5844 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005845 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5846 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005847 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005848}
5849
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005850static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5851{
5852 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5853 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005854
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005855 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5856 return -EINVAL;
5857 if (rcvd->max_resp_sz < sent->max_resp_sz)
5858 return -EINVAL;
5859 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5860 return -EINVAL;
5861 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005862 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005863 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005864 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005865 return -EINVAL;
5866 return 0;
5867}
Andy Adamson8d353012009-04-01 09:22:32 -04005868
Andy Adamson8d353012009-04-01 09:22:32 -04005869static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5870 struct nfs4_session *session)
5871{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005872 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005873
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005874 ret = nfs4_verify_fore_channel_attrs(args, session);
5875 if (ret)
5876 return ret;
5877 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005878}
5879
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005880static int _nfs4_proc_create_session(struct nfs_client *clp,
5881 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005882{
5883 struct nfs4_session *session = clp->cl_session;
5884 struct nfs41_create_session_args args = {
5885 .client = clp,
5886 .cb_program = NFS4_CALLBACK,
5887 };
5888 struct nfs41_create_session_res res = {
5889 .client = clp,
5890 };
5891 struct rpc_message msg = {
5892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5893 .rpc_argp = &args,
5894 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005895 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005896 };
5897 int status;
5898
5899 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005900 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005901
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005902 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005903
Trond Myklebust43095d32012-11-20 11:13:12 -05005904 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005905 /* Verify the session's negotiated channel_attrs values */
5906 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005907 /* Increment the clientid slot sequence id */
5908 clp->cl_seqid++;
5909 }
5910
5911 return status;
5912}
5913
5914/*
5915 * Issues a CREATE_SESSION operation to the server.
5916 * It is the responsibility of the caller to verify the session is
5917 * expired before calling this routine.
5918 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005919int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005920{
5921 int status;
5922 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005923 struct nfs4_session *session = clp->cl_session;
5924
5925 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5926
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005927 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005928 if (status)
5929 goto out;
5930
Andy Adamsonaacd5532011-11-09 13:58:21 -05005931 /* Init or reset the session slot tables */
5932 status = nfs4_setup_session_slot_tables(session);
5933 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005934 if (status)
5935 goto out;
5936
5937 ptr = (unsigned *)&session->sess_id.data[0];
5938 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5939 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005940out:
5941 dprintk("<-- %s\n", __func__);
5942 return status;
5943}
5944
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005945/*
5946 * Issue the over-the-wire RPC DESTROY_SESSION.
5947 * The caller must serialize access to this routine.
5948 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005949int nfs4_proc_destroy_session(struct nfs4_session *session,
5950 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005951{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005952 struct rpc_message msg = {
5953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5954 .rpc_argp = session,
5955 .rpc_cred = cred,
5956 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005957 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005958
5959 dprintk("--> nfs4_proc_destroy_session\n");
5960
5961 /* session is still being setup */
5962 if (session->clp->cl_cons_state != NFS_CS_READY)
5963 return status;
5964
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005965 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005966
5967 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005968 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005969 "Session has been destroyed regardless...\n", status);
5970
5971 dprintk("<-- nfs4_proc_destroy_session\n");
5972 return status;
5973}
5974
Trond Myklebust7b38c362012-05-23 13:23:31 -04005975/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005976 * Renew the cl_session lease.
5977 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005978struct nfs4_sequence_data {
5979 struct nfs_client *clp;
5980 struct nfs4_sequence_args args;
5981 struct nfs4_sequence_res res;
5982};
5983
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005984static void nfs41_sequence_release(void *data)
5985{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005986 struct nfs4_sequence_data *calldata = data;
5987 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005988
Alexandros Batsakis71358402010-02-05 03:45:05 -08005989 if (atomic_read(&clp->cl_count) > 1)
5990 nfs4_schedule_state_renewal(clp);
5991 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005992 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005993}
5994
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005995static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5996{
5997 switch(task->tk_status) {
5998 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005999 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6000 return -EAGAIN;
6001 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006002 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006003 }
6004 return 0;
6005}
6006
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006007static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006008{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006009 struct nfs4_sequence_data *calldata = data;
6010 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006011
Trond Myklebust14516c32010-07-31 14:29:06 -04006012 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6013 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006014
6015 if (task->tk_status < 0) {
6016 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006017 if (atomic_read(&clp->cl_count) == 1)
6018 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006019
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006020 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6021 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006022 return;
6023 }
6024 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006025 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006026out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006027 dprintk("<-- %s\n", __func__);
6028}
6029
6030static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6031{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006032 struct nfs4_sequence_data *calldata = data;
6033 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006034 struct nfs4_sequence_args *args;
6035 struct nfs4_sequence_res *res;
6036
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006037 args = task->tk_msg.rpc_argp;
6038 res = task->tk_msg.rpc_resp;
6039
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006040 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006041}
6042
6043static const struct rpc_call_ops nfs41_sequence_ops = {
6044 .rpc_call_done = nfs41_sequence_call_done,
6045 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006046 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006047};
6048
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006049static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6050 struct rpc_cred *cred,
6051 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006052{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006053 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006054 struct rpc_message msg = {
6055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6056 .rpc_cred = cred,
6057 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006058 struct rpc_task_setup task_setup_data = {
6059 .rpc_client = clp->cl_rpcclient,
6060 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006061 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006062 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006063 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006064
Alexandros Batsakis71358402010-02-05 03:45:05 -08006065 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006066 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006067 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006068 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006069 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006070 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006071 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006072 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006073 if (is_privileged)
6074 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006075 msg.rpc_argp = &calldata->args;
6076 msg.rpc_resp = &calldata->res;
6077 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006078 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006079
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006080 return rpc_run_task(&task_setup_data);
6081}
6082
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006083static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006084{
6085 struct rpc_task *task;
6086 int ret = 0;
6087
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006088 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6089 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006090 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006091 if (IS_ERR(task))
6092 ret = PTR_ERR(task);
6093 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006094 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006095 dprintk("<-- %s status=%d\n", __func__, ret);
6096 return ret;
6097}
6098
6099static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6100{
6101 struct rpc_task *task;
6102 int ret;
6103
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006104 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006105 if (IS_ERR(task)) {
6106 ret = PTR_ERR(task);
6107 goto out;
6108 }
6109 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006110 if (!ret) {
6111 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6112
6113 if (task->tk_status == 0)
6114 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006115 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006116 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006117 rpc_put_task(task);
6118out:
6119 dprintk("<-- %s status=%d\n", __func__, ret);
6120 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006121}
6122
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006123struct nfs4_reclaim_complete_data {
6124 struct nfs_client *clp;
6125 struct nfs41_reclaim_complete_args arg;
6126 struct nfs41_reclaim_complete_res res;
6127};
6128
6129static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6130{
6131 struct nfs4_reclaim_complete_data *calldata = data;
6132
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006133 nfs41_setup_sequence(calldata->clp->cl_session,
6134 &calldata->arg.seq_args,
6135 &calldata->res.seq_res,
6136 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006137}
6138
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006139static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6140{
6141 switch(task->tk_status) {
6142 case 0:
6143 case -NFS4ERR_COMPLETE_ALREADY:
6144 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6145 break;
6146 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006147 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006148 /* fall through */
6149 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006150 return -EAGAIN;
6151 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006152 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006153 }
6154 return 0;
6155}
6156
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006157static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6158{
6159 struct nfs4_reclaim_complete_data *calldata = data;
6160 struct nfs_client *clp = calldata->clp;
6161 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6162
6163 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006164 if (!nfs41_sequence_done(task, res))
6165 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006166
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006167 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6168 rpc_restart_call_prepare(task);
6169 return;
6170 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006171 dprintk("<-- %s\n", __func__);
6172}
6173
6174static void nfs4_free_reclaim_complete_data(void *data)
6175{
6176 struct nfs4_reclaim_complete_data *calldata = data;
6177
6178 kfree(calldata);
6179}
6180
6181static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6182 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6183 .rpc_call_done = nfs4_reclaim_complete_done,
6184 .rpc_release = nfs4_free_reclaim_complete_data,
6185};
6186
6187/*
6188 * Issue a global reclaim complete.
6189 */
6190static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6191{
6192 struct nfs4_reclaim_complete_data *calldata;
6193 struct rpc_task *task;
6194 struct rpc_message msg = {
6195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6196 };
6197 struct rpc_task_setup task_setup_data = {
6198 .rpc_client = clp->cl_rpcclient,
6199 .rpc_message = &msg,
6200 .callback_ops = &nfs4_reclaim_complete_call_ops,
6201 .flags = RPC_TASK_ASYNC,
6202 };
6203 int status = -ENOMEM;
6204
6205 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006206 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006207 if (calldata == NULL)
6208 goto out;
6209 calldata->clp = clp;
6210 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006211
Trond Myklebust9d12b212012-01-17 22:04:25 -05006212 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006213 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006214 msg.rpc_argp = &calldata->arg;
6215 msg.rpc_resp = &calldata->res;
6216 task_setup_data.callback_data = calldata;
6217 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006218 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006219 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006220 goto out;
6221 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006222 status = nfs4_wait_for_completion_rpc_task(task);
6223 if (status == 0)
6224 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006225 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006226 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006227out:
6228 dprintk("<-- %s status=%d\n", __func__, status);
6229 return status;
6230}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006231
6232static void
6233nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6234{
6235 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006236 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006237 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006238
6239 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006240 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6241 * right now covering the LAYOUTGET we are about to send.
6242 * However, that is not so catastrophic, and there seems
6243 * to be no way to prevent it completely.
6244 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006245 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006246 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006247 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006248 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6249 NFS_I(lgp->args.inode)->layout,
6250 lgp->args.ctx->state)) {
6251 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006252 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006253}
6254
6255static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6256{
6257 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006258 struct inode *inode = lgp->args.inode;
6259 struct nfs_server *server = NFS_SERVER(inode);
6260 struct pnfs_layout_hdr *lo;
6261 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006262 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006263
6264 dprintk("--> %s\n", __func__);
6265
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006266 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006267 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006268
6269 switch (task->tk_status) {
6270 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006271 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006272 case -NFS4ERR_LAYOUTTRYLATER:
6273 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006274 timeo = rpc_get_timeout(task->tk_client);
6275 giveup = lgp->args.timestamp + timeo;
6276 if (time_after(giveup, jiffies))
6277 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006278 break;
6279 case -NFS4ERR_EXPIRED:
6280 case -NFS4ERR_BAD_STATEID:
6281 spin_lock(&inode->i_lock);
6282 lo = NFS_I(inode)->layout;
6283 if (!lo || list_empty(&lo->plh_segs)) {
6284 spin_unlock(&inode->i_lock);
6285 /* If the open stateid was bad, then recover it. */
6286 state = lgp->args.ctx->state;
6287 } else {
6288 LIST_HEAD(head);
6289
6290 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6291 spin_unlock(&inode->i_lock);
6292 /* Mark the bad layout state as invalid, then
6293 * retry using the open stateid. */
6294 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006295 }
6296 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006297 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6298 rpc_restart_call_prepare(task);
6299out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006300 dprintk("<-- %s\n", __func__);
6301}
6302
Idan Kedar85541162012-08-02 11:47:10 +03006303static size_t max_response_pages(struct nfs_server *server)
6304{
6305 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6306 return nfs_page_array_len(0, max_resp_sz);
6307}
6308
6309static void nfs4_free_pages(struct page **pages, size_t size)
6310{
6311 int i;
6312
6313 if (!pages)
6314 return;
6315
6316 for (i = 0; i < size; i++) {
6317 if (!pages[i])
6318 break;
6319 __free_page(pages[i]);
6320 }
6321 kfree(pages);
6322}
6323
6324static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6325{
6326 struct page **pages;
6327 int i;
6328
6329 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6330 if (!pages) {
6331 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6332 return NULL;
6333 }
6334
6335 for (i = 0; i < size; i++) {
6336 pages[i] = alloc_page(gfp_flags);
6337 if (!pages[i]) {
6338 dprintk("%s: failed to allocate page\n", __func__);
6339 nfs4_free_pages(pages, size);
6340 return NULL;
6341 }
6342 }
6343
6344 return pages;
6345}
6346
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006347static void nfs4_layoutget_release(void *calldata)
6348{
6349 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006350 struct inode *inode = lgp->args.inode;
6351 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006352 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006353
6354 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006355 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006356 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006357 put_nfs_open_context(lgp->args.ctx);
6358 kfree(calldata);
6359 dprintk("<-- %s\n", __func__);
6360}
6361
6362static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6363 .rpc_call_prepare = nfs4_layoutget_prepare,
6364 .rpc_call_done = nfs4_layoutget_done,
6365 .rpc_release = nfs4_layoutget_release,
6366};
6367
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006368struct pnfs_layout_segment *
6369nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006370{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006371 struct inode *inode = lgp->args.inode;
6372 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006373 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006374 struct rpc_task *task;
6375 struct rpc_message msg = {
6376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6377 .rpc_argp = &lgp->args,
6378 .rpc_resp = &lgp->res,
6379 };
6380 struct rpc_task_setup task_setup_data = {
6381 .rpc_client = server->client,
6382 .rpc_message = &msg,
6383 .callback_ops = &nfs4_layoutget_call_ops,
6384 .callback_data = lgp,
6385 .flags = RPC_TASK_ASYNC,
6386 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006387 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006388 int status = 0;
6389
6390 dprintk("--> %s\n", __func__);
6391
Idan Kedar85541162012-08-02 11:47:10 +03006392 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6393 if (!lgp->args.layout.pages) {
6394 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006395 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006396 }
6397 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006398 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006399
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006400 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006401 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006402 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006403
6404 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6405 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6406
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006407 task = rpc_run_task(&task_setup_data);
6408 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006409 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006410 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006411 if (status == 0)
6412 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006413 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6414 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006415 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006416 rpc_put_task(task);
6417 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006418 if (status)
6419 return ERR_PTR(status);
6420 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006421}
6422
Benny Halevycbe82602011-05-22 19:52:37 +03006423static void
6424nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6425{
6426 struct nfs4_layoutreturn *lrp = calldata;
6427
6428 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006429 nfs41_setup_sequence(lrp->clp->cl_session,
6430 &lrp->args.seq_args,
6431 &lrp->res.seq_res,
6432 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006433}
6434
6435static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6436{
6437 struct nfs4_layoutreturn *lrp = calldata;
6438 struct nfs_server *server;
6439
6440 dprintk("--> %s\n", __func__);
6441
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006442 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006443 return;
6444
6445 server = NFS_SERVER(lrp->args.inode);
6446 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006447 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006448 return;
6449 }
Benny Halevycbe82602011-05-22 19:52:37 +03006450 dprintk("<-- %s\n", __func__);
6451}
6452
6453static void nfs4_layoutreturn_release(void *calldata)
6454{
6455 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006456 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006457
6458 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006459 spin_lock(&lo->plh_inode->i_lock);
6460 if (lrp->res.lrs_present)
6461 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6462 lo->plh_block_lgets--;
6463 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006464 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006465 kfree(calldata);
6466 dprintk("<-- %s\n", __func__);
6467}
6468
6469static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6470 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6471 .rpc_call_done = nfs4_layoutreturn_done,
6472 .rpc_release = nfs4_layoutreturn_release,
6473};
6474
6475int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6476{
6477 struct rpc_task *task;
6478 struct rpc_message msg = {
6479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6480 .rpc_argp = &lrp->args,
6481 .rpc_resp = &lrp->res,
6482 };
6483 struct rpc_task_setup task_setup_data = {
6484 .rpc_client = lrp->clp->cl_rpcclient,
6485 .rpc_message = &msg,
6486 .callback_ops = &nfs4_layoutreturn_call_ops,
6487 .callback_data = lrp,
6488 };
6489 int status;
6490
6491 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006492 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006493 task = rpc_run_task(&task_setup_data);
6494 if (IS_ERR(task))
6495 return PTR_ERR(task);
6496 status = task->tk_status;
6497 dprintk("<-- %s status=%d\n", __func__, status);
6498 rpc_put_task(task);
6499 return status;
6500}
6501
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006502/*
6503 * Retrieve the list of Data Server devices from the MDS.
6504 */
6505static int _nfs4_getdevicelist(struct nfs_server *server,
6506 const struct nfs_fh *fh,
6507 struct pnfs_devicelist *devlist)
6508{
6509 struct nfs4_getdevicelist_args args = {
6510 .fh = fh,
6511 .layoutclass = server->pnfs_curr_ld->id,
6512 };
6513 struct nfs4_getdevicelist_res res = {
6514 .devlist = devlist,
6515 };
6516 struct rpc_message msg = {
6517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6518 .rpc_argp = &args,
6519 .rpc_resp = &res,
6520 };
6521 int status;
6522
6523 dprintk("--> %s\n", __func__);
6524 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6525 &res.seq_res, 0);
6526 dprintk("<-- %s status=%d\n", __func__, status);
6527 return status;
6528}
6529
6530int nfs4_proc_getdevicelist(struct nfs_server *server,
6531 const struct nfs_fh *fh,
6532 struct pnfs_devicelist *devlist)
6533{
6534 struct nfs4_exception exception = { };
6535 int err;
6536
6537 do {
6538 err = nfs4_handle_exception(server,
6539 _nfs4_getdevicelist(server, fh, devlist),
6540 &exception);
6541 } while (exception.retry);
6542
6543 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6544 err, devlist->num_devs);
6545
6546 return err;
6547}
6548EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6549
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006550static int
6551_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6552{
6553 struct nfs4_getdeviceinfo_args args = {
6554 .pdev = pdev,
6555 };
6556 struct nfs4_getdeviceinfo_res res = {
6557 .pdev = pdev,
6558 };
6559 struct rpc_message msg = {
6560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6561 .rpc_argp = &args,
6562 .rpc_resp = &res,
6563 };
6564 int status;
6565
6566 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006567 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006568 dprintk("<-- %s status=%d\n", __func__, status);
6569
6570 return status;
6571}
6572
6573int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6574{
6575 struct nfs4_exception exception = { };
6576 int err;
6577
6578 do {
6579 err = nfs4_handle_exception(server,
6580 _nfs4_proc_getdeviceinfo(server, pdev),
6581 &exception);
6582 } while (exception.retry);
6583 return err;
6584}
6585EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6586
Andy Adamson863a3c62011-03-23 13:27:54 +00006587static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6588{
6589 struct nfs4_layoutcommit_data *data = calldata;
6590 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006591 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006592
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006593 nfs41_setup_sequence(session,
6594 &data->args.seq_args,
6595 &data->res.seq_res,
6596 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006597}
6598
6599static void
6600nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6601{
6602 struct nfs4_layoutcommit_data *data = calldata;
6603 struct nfs_server *server = NFS_SERVER(data->args.inode);
6604
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006605 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006606 return;
6607
6608 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006609 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6610 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6611 case -NFS4ERR_BADLAYOUT: /* no layout */
6612 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006613 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006614 break;
6615 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006616 nfs_post_op_update_inode_force_wcc(data->args.inode,
6617 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006618 break;
6619 default:
6620 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6621 rpc_restart_call_prepare(task);
6622 return;
6623 }
6624 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006625}
6626
6627static void nfs4_layoutcommit_release(void *calldata)
6628{
6629 struct nfs4_layoutcommit_data *data = calldata;
6630
Andy Adamsondb29c082011-07-30 20:52:38 -04006631 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006632 put_rpccred(data->cred);
6633 kfree(data);
6634}
6635
6636static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6637 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6638 .rpc_call_done = nfs4_layoutcommit_done,
6639 .rpc_release = nfs4_layoutcommit_release,
6640};
6641
6642int
Andy Adamsonef311532011-03-12 02:58:10 -05006643nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006644{
6645 struct rpc_message msg = {
6646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6647 .rpc_argp = &data->args,
6648 .rpc_resp = &data->res,
6649 .rpc_cred = data->cred,
6650 };
6651 struct rpc_task_setup task_setup_data = {
6652 .task = &data->task,
6653 .rpc_client = NFS_CLIENT(data->args.inode),
6654 .rpc_message = &msg,
6655 .callback_ops = &nfs4_layoutcommit_ops,
6656 .callback_data = data,
6657 .flags = RPC_TASK_ASYNC,
6658 };
6659 struct rpc_task *task;
6660 int status = 0;
6661
6662 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6663 "lbw: %llu inode %lu\n",
6664 data->task.tk_pid, sync,
6665 data->args.lastbytewritten,
6666 data->args.inode->i_ino);
6667
Trond Myklebust9d12b212012-01-17 22:04:25 -05006668 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006669 task = rpc_run_task(&task_setup_data);
6670 if (IS_ERR(task))
6671 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006672 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006673 goto out;
6674 status = nfs4_wait_for_completion_rpc_task(task);
6675 if (status != 0)
6676 goto out;
6677 status = task->tk_status;
6678out:
6679 dprintk("%s: status %d\n", __func__, status);
6680 rpc_put_task(task);
6681 return status;
6682}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006683
6684static int
6685_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6686 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6687{
6688 struct nfs41_secinfo_no_name_args args = {
6689 .style = SECINFO_STYLE_CURRENT_FH,
6690 };
6691 struct nfs4_secinfo_res res = {
6692 .flavors = flavors,
6693 };
6694 struct rpc_message msg = {
6695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6696 .rpc_argp = &args,
6697 .rpc_resp = &res,
6698 };
6699 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6700}
6701
6702static int
6703nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6704 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6705{
6706 struct nfs4_exception exception = { };
6707 int err;
6708 do {
6709 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6710 switch (err) {
6711 case 0:
6712 case -NFS4ERR_WRONGSEC:
6713 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006714 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006715 default:
6716 err = nfs4_handle_exception(server, err, &exception);
6717 }
6718 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006719out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006720 return err;
6721}
6722
6723static int
6724nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6725 struct nfs_fsinfo *info)
6726{
6727 int err;
6728 struct page *page;
6729 rpc_authflavor_t flavor;
6730 struct nfs4_secinfo_flavors *flavors;
6731
6732 page = alloc_page(GFP_KERNEL);
6733 if (!page) {
6734 err = -ENOMEM;
6735 goto out;
6736 }
6737
6738 flavors = page_address(page);
6739 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6740
6741 /*
6742 * Fall back on "guess and check" method if
6743 * the server doesn't support SECINFO_NO_NAME
6744 */
6745 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6746 err = nfs4_find_root_sec(server, fhandle, info);
6747 goto out_freepage;
6748 }
6749 if (err)
6750 goto out_freepage;
6751
6752 flavor = nfs_find_best_sec(flavors);
6753 if (err == 0)
6754 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6755
6756out_freepage:
6757 put_page(page);
6758 if (err == -EACCES)
6759 return -EPERM;
6760out:
6761 return err;
6762}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006763
6764static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006765{
6766 int status;
6767 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006768 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006769 };
6770 struct nfs41_test_stateid_res res;
6771 struct rpc_message msg = {
6772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6773 .rpc_argp = &args,
6774 .rpc_resp = &res,
6775 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006776
Chuck Lever38527b12012-07-11 16:30:23 -04006777 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006778 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006779 nfs4_set_sequence_privileged(&args.seq_args);
6780 status = nfs4_call_sync_sequence(server->client, server, &msg,
6781 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006782 if (status != NFS_OK) {
6783 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006784 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006785 }
6786 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006787 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006788}
6789
Chuck Lever38527b12012-07-11 16:30:23 -04006790/**
6791 * nfs41_test_stateid - perform a TEST_STATEID operation
6792 *
6793 * @server: server / transport on which to perform the operation
6794 * @stateid: state ID to test
6795 *
6796 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6797 * Otherwise a negative NFS4ERR value is returned if the operation
6798 * failed or the state ID is not currently valid.
6799 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006800static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006801{
6802 struct nfs4_exception exception = { };
6803 int err;
6804 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006805 err = _nfs41_test_stateid(server, stateid);
6806 if (err != -NFS4ERR_DELAY)
6807 break;
6808 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006809 } while (exception.retry);
6810 return err;
6811}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006812
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006813struct nfs_free_stateid_data {
6814 struct nfs_server *server;
6815 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006816 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006817};
6818
6819static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
6820{
6821 struct nfs_free_stateid_data *data = calldata;
6822 nfs41_setup_sequence(nfs4_get_session(data->server),
6823 &data->args.seq_args,
6824 &data->res.seq_res,
6825 task);
6826}
6827
6828static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
6829{
6830 struct nfs_free_stateid_data *data = calldata;
6831
6832 nfs41_sequence_done(task, &data->res.seq_res);
6833
6834 switch (task->tk_status) {
6835 case -NFS4ERR_DELAY:
6836 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
6837 rpc_restart_call_prepare(task);
6838 }
6839}
6840
6841static void nfs41_free_stateid_release(void *calldata)
6842{
6843 kfree(calldata);
6844}
6845
6846const struct rpc_call_ops nfs41_free_stateid_ops = {
6847 .rpc_call_prepare = nfs41_free_stateid_prepare,
6848 .rpc_call_done = nfs41_free_stateid_done,
6849 .rpc_release = nfs41_free_stateid_release,
6850};
6851
6852static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
6853 nfs4_stateid *stateid,
6854 bool privileged)
6855{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006856 struct rpc_message msg = {
6857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006858 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006859 struct rpc_task_setup task_setup = {
6860 .rpc_client = server->client,
6861 .rpc_message = &msg,
6862 .callback_ops = &nfs41_free_stateid_ops,
6863 .flags = RPC_TASK_ASYNC,
6864 };
6865 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006866
Chuck Lever38527b12012-07-11 16:30:23 -04006867 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006868 data = kmalloc(sizeof(*data), GFP_NOFS);
6869 if (!data)
6870 return ERR_PTR(-ENOMEM);
6871 data->server = server;
6872 nfs4_stateid_copy(&data->args.stateid, stateid);
6873
6874 task_setup.callback_data = data;
6875
6876 msg.rpc_argp = &data->args;
6877 msg.rpc_resp = &data->res;
6878 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6879 if (privileged)
6880 nfs4_set_sequence_privileged(&data->args.seq_args);
6881
6882 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006883}
6884
Chuck Lever38527b12012-07-11 16:30:23 -04006885/**
6886 * nfs41_free_stateid - perform a FREE_STATEID operation
6887 *
6888 * @server: server / transport on which to perform the operation
6889 * @stateid: state ID to release
6890 *
6891 * Returns NFS_OK if the server freed "stateid". Otherwise a
6892 * negative NFS4ERR value is returned.
6893 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006894static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006895{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006896 struct rpc_task *task;
6897 int ret;
6898
6899 task = _nfs41_free_stateid(server, stateid, true);
6900 if (IS_ERR(task))
6901 return PTR_ERR(task);
6902 ret = rpc_wait_for_completion_task(task);
6903 if (!ret)
6904 ret = task->tk_status;
6905 rpc_put_task(task);
6906 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006907}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006908
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04006909static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
6910{
6911 struct rpc_task *task;
6912
6913 task = _nfs41_free_stateid(server, &lsp->ls_stateid, false);
6914 nfs4_free_lock_state(server, lsp);
6915 if (IS_ERR(task))
6916 return PTR_ERR(task);
6917 rpc_put_task(task);
6918 return 0;
6919}
6920
Trond Myklebust36281ca2012-03-04 18:13:56 -05006921static bool nfs41_match_stateid(const nfs4_stateid *s1,
6922 const nfs4_stateid *s2)
6923{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006924 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006925 return false;
6926
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006927 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006928 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006929 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006930 return true;
6931
6932 return false;
6933}
6934
Andy Adamson557134a2009-04-01 09:21:53 -04006935#endif /* CONFIG_NFS_V4_1 */
6936
Trond Myklebust36281ca2012-03-04 18:13:56 -05006937static bool nfs4_match_stateid(const nfs4_stateid *s1,
6938 const nfs4_stateid *s2)
6939{
Trond Myklebustf597c532012-03-04 18:13:56 -05006940 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006941}
6942
6943
Trond Myklebust17280172012-03-11 13:11:00 -04006944static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006945 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006946 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 .recover_open = nfs4_open_reclaim,
6948 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006949 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006950 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006951 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952};
6953
Andy Adamson591d71c2009-04-01 09:22:47 -04006954#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006955static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006956 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6957 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6958 .recover_open = nfs4_open_reclaim,
6959 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006960 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006961 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006962 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006963 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006964};
6965#endif /* CONFIG_NFS_V4_1 */
6966
Trond Myklebust17280172012-03-11 13:11:00 -04006967static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006968 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006969 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 .recover_open = nfs4_open_expired,
6971 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006972 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006973 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974};
6975
Andy Adamson591d71c2009-04-01 09:22:47 -04006976#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006977static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006978 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6979 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006980 .recover_open = nfs41_open_expired,
6981 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006982 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006983 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006984};
6985#endif /* CONFIG_NFS_V4_1 */
6986
Trond Myklebust17280172012-03-11 13:11:00 -04006987static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006988 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006989 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006990 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006991};
6992
6993#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006994static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006995 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006996 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006997 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006998};
6999#endif
7000
Trond Myklebust97dc1352010-06-16 09:52:26 -04007001static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7002 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007003 .init_caps = NFS_CAP_READDIRPLUS
7004 | NFS_CAP_ATOMIC_OPEN
7005 | NFS_CAP_CHANGE_ATTR
7006 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007007 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007008 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007009 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007010 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007011 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7012 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7013 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007014};
7015
7016#if defined(CONFIG_NFS_V4_1)
7017static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7018 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007019 .init_caps = NFS_CAP_READDIRPLUS
7020 | NFS_CAP_ATOMIC_OPEN
7021 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007022 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007023 | NFS_CAP_STATEID_NFSV41
7024 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007025 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007026 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007027 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007028 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007029 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7030 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7031 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007032};
7033#endif
7034
Steve Dickson42c2c422013-05-22 12:50:38 -04007035#if defined(CONFIG_NFS_V4_2)
7036static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7037 .minor_version = 2,
7038 .call_sync = nfs4_call_sync_sequence,
7039 .match_stateid = nfs41_match_stateid,
7040 .find_root_sec = nfs41_find_root_sec,
7041 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7042 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7043 .state_renewal_ops = &nfs41_state_renewal_ops,
7044};
7045#endif
7046
Trond Myklebust97dc1352010-06-16 09:52:26 -04007047const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7048 [0] = &nfs_v4_0_minor_ops,
7049#if defined(CONFIG_NFS_V4_1)
7050 [1] = &nfs_v4_1_minor_ops,
7051#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007052#if defined(CONFIG_NFS_V4_2)
7053 [2] = &nfs_v4_2_minor_ops,
7054#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007055};
7056
Bryan Schumaker73a79702012-07-16 16:39:12 -04007057const struct inode_operations nfs4_dir_inode_operations = {
7058 .create = nfs_create,
7059 .lookup = nfs_lookup,
7060 .atomic_open = nfs_atomic_open,
7061 .link = nfs_link,
7062 .unlink = nfs_unlink,
7063 .symlink = nfs_symlink,
7064 .mkdir = nfs_mkdir,
7065 .rmdir = nfs_rmdir,
7066 .mknod = nfs_mknod,
7067 .rename = nfs_rename,
7068 .permission = nfs_permission,
7069 .getattr = nfs_getattr,
7070 .setattr = nfs_setattr,
7071 .getxattr = generic_getxattr,
7072 .setxattr = generic_setxattr,
7073 .listxattr = generic_listxattr,
7074 .removexattr = generic_removexattr,
7075};
7076
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007077static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007078 .permission = nfs_permission,
7079 .getattr = nfs_getattr,
7080 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007081 .getxattr = generic_getxattr,
7082 .setxattr = generic_setxattr,
7083 .listxattr = generic_listxattr,
7084 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007085};
7086
David Howells509de812006-08-22 20:06:11 -04007087const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 .version = 4, /* protocol version */
7089 .dentry_ops = &nfs4_dentry_operations,
7090 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007091 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007092 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007094 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007095 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 .getattr = nfs4_proc_getattr,
7097 .setattr = nfs4_proc_setattr,
7098 .lookup = nfs4_proc_lookup,
7099 .access = nfs4_proc_access,
7100 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 .create = nfs4_proc_create,
7102 .remove = nfs4_proc_remove,
7103 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007104 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 .unlink_done = nfs4_proc_unlink_done,
7106 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007107 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007108 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007109 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110 .link = nfs4_proc_link,
7111 .symlink = nfs4_proc_symlink,
7112 .mkdir = nfs4_proc_mkdir,
7113 .rmdir = nfs4_proc_remove,
7114 .readdir = nfs4_proc_readdir,
7115 .mknod = nfs4_proc_mknod,
7116 .statfs = nfs4_proc_statfs,
7117 .fsinfo = nfs4_proc_fsinfo,
7118 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007119 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 .decode_dirent = nfs4_decode_dirent,
7121 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007122 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007123 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007124 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007126 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007127 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007128 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007130 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007131 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007133 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007134 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007135 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007136 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007137 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007138 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007139 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007140 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007141 .create_server = nfs4_create_server,
7142 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143};
7144
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007145static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7146 .prefix = XATTR_NAME_NFSV4_ACL,
7147 .list = nfs4_xattr_list_nfs4_acl,
7148 .get = nfs4_xattr_get_nfs4_acl,
7149 .set = nfs4_xattr_set_nfs4_acl,
7150};
7151
7152const struct xattr_handler *nfs4_xattr_handlers[] = {
7153 &nfs4_xattr_nfs4_acl_handler,
7154 NULL
7155};
7156
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157/*
7158 * Local variables:
7159 * c-basic-offset: 8
7160 * End:
7161 */