blob: 0ad025eb523bc6b651d82497c29a0d27ebdf3dac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -050096 case -NFS4ERR_LAYOUTTRYLATER:
97 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -040098 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000099 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400104 case -NFS4ERR_SHARE_DENIED:
105 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400106 case -NFS4ERR_MINOR_VERS_MISMATCH:
107 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400108 case -NFS4ERR_ACCESS:
109 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400110 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700112 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400115 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/*
119 * This is our standard bitmap for GETATTR requests.
120 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400121const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 FATTR4_WORD0_TYPE
123 | FATTR4_WORD0_CHANGE
124 | FATTR4_WORD0_SIZE
125 | FATTR4_WORD0_FSID
126 | FATTR4_WORD0_FILEID,
127 FATTR4_WORD1_MODE
128 | FATTR4_WORD1_NUMLINKS
129 | FATTR4_WORD1_OWNER
130 | FATTR4_WORD1_OWNER_GROUP
131 | FATTR4_WORD1_RAWDEV
132 | FATTR4_WORD1_SPACE_USED
133 | FATTR4_WORD1_TIME_ACCESS
134 | FATTR4_WORD1_TIME_METADATA
135 | FATTR4_WORD1_TIME_MODIFY
136};
137
Trond Myklebust1549210f2012-06-05 09:16:47 -0400138static const u32 nfs4_pnfs_open_bitmap[3] = {
139 FATTR4_WORD0_TYPE
140 | FATTR4_WORD0_CHANGE
141 | FATTR4_WORD0_SIZE
142 | FATTR4_WORD0_FSID
143 | FATTR4_WORD0_FILEID,
144 FATTR4_WORD1_MODE
145 | FATTR4_WORD1_NUMLINKS
146 | FATTR4_WORD1_OWNER
147 | FATTR4_WORD1_OWNER_GROUP
148 | FATTR4_WORD1_RAWDEV
149 | FATTR4_WORD1_SPACE_USED
150 | FATTR4_WORD1_TIME_ACCESS
151 | FATTR4_WORD1_TIME_METADATA
152 | FATTR4_WORD1_TIME_MODIFY,
153 FATTR4_WORD2_MDSTHRESHOLD
154};
155
Andy Adamsone23008e2012-10-02 21:07:32 -0400156static const u32 nfs4_open_noattr_bitmap[3] = {
157 FATTR4_WORD0_TYPE
158 | FATTR4_WORD0_CHANGE
159 | FATTR4_WORD0_FILEID,
160};
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162const u32 nfs4_statfs_bitmap[2] = {
163 FATTR4_WORD0_FILES_AVAIL
164 | FATTR4_WORD0_FILES_FREE
165 | FATTR4_WORD0_FILES_TOTAL,
166 FATTR4_WORD1_SPACE_AVAIL
167 | FATTR4_WORD1_SPACE_FREE
168 | FATTR4_WORD1_SPACE_TOTAL
169};
170
Trond Myklebust4ce79712005-06-22 17:16:21 +0000171const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 FATTR4_WORD0_MAXLINK
173 | FATTR4_WORD0_MAXNAME,
174 0
175};
176
Fred Isamandae100c2011-07-30 20:52:37 -0400177const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 | FATTR4_WORD0_MAXREAD
179 | FATTR4_WORD0_MAXWRITE
180 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700181 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400182 | FATTR4_WORD1_FS_LAYOUT_TYPES,
183 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
Manoj Naik830b8e32006-06-09 09:34:25 -0400186const u32 nfs4_fs_locations_bitmap[2] = {
187 FATTR4_WORD0_TYPE
188 | FATTR4_WORD0_CHANGE
189 | FATTR4_WORD0_SIZE
190 | FATTR4_WORD0_FSID
191 | FATTR4_WORD0_FILEID
192 | FATTR4_WORD0_FS_LOCATIONS,
193 FATTR4_WORD1_MODE
194 | FATTR4_WORD1_NUMLINKS
195 | FATTR4_WORD1_OWNER
196 | FATTR4_WORD1_OWNER_GROUP
197 | FATTR4_WORD1_RAWDEV
198 | FATTR4_WORD1_SPACE_USED
199 | FATTR4_WORD1_TIME_ACCESS
200 | FATTR4_WORD1_TIME_METADATA
201 | FATTR4_WORD1_TIME_MODIFY
202 | FATTR4_WORD1_MOUNTED_ON_FILEID
203};
204
Al Virobc4785c2006-10-19 23:28:51 -0700205static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct nfs4_readdir_arg *readdir)
207{
Al Viro0dbb4c62006-10-19 23:28:49 -0700208 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000211 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
213 return;
214 }
215
216 readdir->cookie = 0;
217 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
218 if (cookie == 2)
219 return;
220
221 /*
222 * NFSv4 servers do not return entries for '.' and '..'
223 * Therefore, we fake these entries here. We let '.'
224 * have cookie 0 and '..' have cookie 1. Note that
225 * when talking to the server, we always send cookie 0
226 * instead of 1 or 2.
227 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800228 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (cookie == 0) {
231 *p++ = xdr_one; /* next */
232 *p++ = xdr_zero; /* cookie, first word */
233 *p++ = xdr_one; /* cookie, second word */
234 *p++ = xdr_one; /* entry len */
235 memcpy(p, ".\0\0\0", 4); /* entry */
236 p++;
237 *p++ = xdr_one; /* bitmap length */
238 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
239 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400240 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
243 *p++ = xdr_one; /* next */
244 *p++ = xdr_zero; /* cookie, first word */
245 *p++ = xdr_two; /* cookie, second word */
246 *p++ = xdr_two; /* entry len */
247 memcpy(p, "..\0\0", 4); /* entry */
248 p++;
249 *p++ = xdr_one; /* bitmap length */
250 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
251 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400252 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 readdir->pgbase = (char *)p - (char *)start;
255 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800256 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Trond Myklebust65de8722008-12-23 15:21:44 -0500259static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
260{
261 int res = 0;
262
263 might_sleep();
264
265 if (*timeout <= 0)
266 *timeout = NFS4_POLL_RETRY_MIN;
267 if (*timeout > NFS4_POLL_RETRY_MAX)
268 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100269 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500270 if (fatal_signal_pending(current))
271 res = -ERESTARTSYS;
272 *timeout <<= 1;
273 return res;
274}
275
276/* This is the error handling routine for processes that are allowed
277 * to sleep.
278 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800279static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500280{
281 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500282 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500283 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500284 int ret = errorcode;
285
286 exception->retry = 0;
287 switch(errorcode) {
288 case 0:
289 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500290 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400291 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400292 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500293 exception->retry = 1;
294 return 0;
295 }
296 if (state == NULL)
297 break;
298 nfs4_schedule_stateid_recovery(server, state);
299 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500300 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500301 case -NFS4ERR_ADMIN_REVOKED:
302 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500303 if (state == NULL)
304 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400305 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500306 nfs4_schedule_stateid_recovery(server, state);
307 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400308 case -NFS4ERR_EXPIRED:
309 if (state != NULL)
310 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500311 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500312 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500313 nfs4_schedule_lease_recovery(clp);
314 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500315#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400316 case -NFS4ERR_BADSESSION:
317 case -NFS4ERR_BADSLOT:
318 case -NFS4ERR_BAD_HIGH_SLOT:
319 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
320 case -NFS4ERR_DEADSESSION:
321 case -NFS4ERR_SEQ_FALSE_RETRY:
322 case -NFS4ERR_SEQ_MISORDERED:
323 dprintk("%s ERROR: %d Reset session\n", __func__,
324 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400325 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400326 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500327#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500328 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500329 if (exception->timeout > HZ) {
330 /* We have retried a decent amount, time to
331 * fail
332 */
333 ret = -EBUSY;
334 break;
335 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500336 case -NFS4ERR_GRACE:
337 case -NFS4ERR_DELAY:
338 ret = nfs4_delay(server->client, &exception->timeout);
339 if (ret != 0)
340 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400341 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500342 case -NFS4ERR_OLD_STATEID:
343 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800344 break;
345 case -NFS4ERR_BADOWNER:
346 /* The following works around a Linux server bug! */
347 case -NFS4ERR_BADNAME:
348 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
349 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
350 exception->retry = 1;
351 printk(KERN_WARNING "NFS: v4 server %s "
352 "does not accept raw "
353 "uid/gids. "
354 "Reenabling the idmapper.\n",
355 server->nfs_client->cl_hostname);
356 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500357 }
358 /* We failed to handle the error */
359 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500360wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500361 ret = nfs4_wait_clnt_recover(clp);
362 if (ret == 0)
363 exception->retry = 1;
364 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500365}
366
367
Trond Myklebust452e9352010-07-31 14:29:06 -0400368static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 spin_lock(&clp->cl_lock);
371 if (time_before(clp->cl_last_renewal,timestamp))
372 clp->cl_last_renewal = timestamp;
373 spin_unlock(&clp->cl_lock);
374}
375
Trond Myklebust452e9352010-07-31 14:29:06 -0400376static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
377{
378 do_renew_lease(server->nfs_client, timestamp);
379}
380
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400381#if defined(CONFIG_NFS_V4_1)
382
Trond Myklebustd185a332010-06-16 09:52:25 -0400383static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400384{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500385 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400386 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500387 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400388
Benny Halevydfb4f3092010-09-24 09:17:01 -0400389 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400390 /* just wake up the next guy waiting since
391 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500392 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500393 return;
Andy Adamson13615872009-04-01 09:22:17 -0400394 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500395 tbl = res->sr_slot->table;
396 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500397
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500398 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500399 /* Be nice to the server: try to ensure that the last transmitted
400 * value for highest_user_slotid <= target_highest_slotid
401 */
402 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
403 send_new_highest_used_slotid = true;
404
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500405 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
406 send_new_highest_used_slotid = false;
407 goto out_unlock;
408 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500409 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500410
411 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
412 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500413out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500414 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400415 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500416 if (send_new_highest_used_slotid)
417 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400418}
419
Trond Myklebust14516c32010-07-31 14:29:06 -0400420static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400421{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500422 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500423 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400424 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500425 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500426 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400427
Bryan Schumaker468f8612011-04-18 15:57:32 -0400428 /* don't increment the sequence number if the task wasn't sent */
429 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400430 goto out;
431
Trond Myklebust933602e2012-11-16 12:12:38 -0500432 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500433 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500434
Trond Myklebustac20d162012-12-15 15:36:07 -0500435 if (slot->interrupted) {
436 slot->interrupted = 0;
437 interrupted = true;
438 }
439
Andy Adamson691daf32009-12-04 15:55:39 -0500440 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400441 switch (res->sr_status) {
442 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500444 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500445 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500446 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500447 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500448 if (res->sr_status_flags != 0)
449 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500450 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400451 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500452 case 1:
453 /*
454 * sr_status remains 1 if an RPC level error occurred.
455 * The server may or may not have processed the sequence
456 * operation..
457 * Mark the slot as having hosted an interrupted RPC call.
458 */
459 slot->interrupted = 1;
460 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400461 case -NFS4ERR_DELAY:
462 /* The server detected a resend of the RPC call and
463 * returned NFS4ERR_DELAY as per Section 2.10.6.2
464 * of RFC5661.
465 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500466 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400467 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500468 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500469 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400470 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500471 case -NFS4ERR_BADSLOT:
472 /*
473 * The slot id we used was probably retired. Try again
474 * using a different slot id.
475 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500476 goto retry_nowait;
477 case -NFS4ERR_SEQ_MISORDERED:
478 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500479 * Was the last operation on this sequence interrupted?
480 * If so, retry after bumping the sequence number.
481 */
482 if (interrupted) {
483 ++slot->seq_nr;
484 goto retry_nowait;
485 }
486 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500487 * Could this slot have been previously retired?
488 * If so, then the server may be expecting seq_nr = 1!
489 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500490 if (slot->seq_nr != 1) {
491 slot->seq_nr = 1;
492 goto retry_nowait;
493 }
494 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500495 case -NFS4ERR_SEQ_FALSE_RETRY:
496 ++slot->seq_nr;
497 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400498 default:
499 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500500 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400501 }
502out:
503 /* The session may be reset by one of the error handlers. */
504 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400505 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500506 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500507retry_nowait:
508 if (rpc_restart_call_prepare(task)) {
509 task->tk_status = 0;
510 ret = 0;
511 }
512 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400513out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400514 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400515 goto out;
516 rpc_delay(task, NFS4_POLL_RETRY_MAX);
517 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400518}
519
Trond Myklebust14516c32010-07-31 14:29:06 -0400520static int nfs4_sequence_done(struct rpc_task *task,
521 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400522{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500523 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400524 return 1;
525 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400526}
527
Trond Myklebust9d12b212012-01-17 22:04:25 -0500528static void nfs41_init_sequence(struct nfs4_sequence_args *args,
529 struct nfs4_sequence_res *res, int cache_reply)
530{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500531 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500532 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400533 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500534 if (cache_reply)
535 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500536 res->sr_slot = NULL;
537}
538
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400539static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
540{
541 args->sa_privileged = 1;
542}
543
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000544int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400545 struct nfs4_sequence_args *args,
546 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400547 struct rpc_task *task)
548{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400549 struct nfs4_slot *slot;
550 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400551
552 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400553 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400554 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400555 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400556
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400557 tbl = &session->fc_slot_table;
558
Trond Myklebust69d206b2012-11-22 13:21:02 -0500559 task->tk_timeout = 0;
560
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400561 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400562 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400563 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500564 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500565 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400566 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400567 }
568
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500569 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500570 if (IS_ERR(slot)) {
571 /* If out of memory, try again in 1/4 second */
572 if (slot == ERR_PTR(-ENOMEM))
573 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400575 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400576 }
577 spin_unlock(&tbl->slot_tbl_lock);
578
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500579 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400580
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500581 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
582 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583
Benny Halevydfb4f3092010-09-24 09:17:01 -0400584 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500585 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400586 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400587 /*
588 * sr_status is only set in decode_sequence, and so will remain
589 * set to 1 if an rpc level failure occurs.
590 */
591 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400592out_success:
593 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400594 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400595out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400596 /* Privileged tasks are queued with top priority */
597 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400598 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
599 NULL, RPC_PRIORITY_PRIVILEGED);
600 else
601 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400602 spin_unlock(&tbl->slot_tbl_lock);
603 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000605EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606
Trond Myklebust035168a2010-06-16 09:52:26 -0400607int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 struct nfs4_sequence_args *args,
609 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610 struct rpc_task *task)
611{
Trond Myklebust035168a2010-06-16 09:52:26 -0400612 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613 int ret = 0;
614
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400615 if (session == NULL) {
616 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400618 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400619
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500620 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400621 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500622 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400623
Trond Myklebust9d12b212012-01-17 22:04:25 -0500624 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625out:
626 dprintk("<-- %s status=%d\n", __func__, ret);
627 return ret;
628}
629
630struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400631 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 struct nfs4_sequence_args *seq_args;
633 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634};
635
636static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
637{
638 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400639 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640
Trond Myklebust035168a2010-06-16 09:52:26 -0400641 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
642
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400643 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644}
645
Andy Adamson69ab40c2009-04-01 09:22:19 -0400646static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
647{
648 struct nfs41_call_sync_data *data = calldata;
649
Trond Myklebust14516c32010-07-31 14:29:06 -0400650 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400651}
652
Trond Myklebust17280172012-03-11 13:11:00 -0400653static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400655 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656};
657
Bryan Schumaker7c513052011-03-24 17:12:24 +0000658static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
659 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400660 struct rpc_message *msg,
661 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400662 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663{
664 int ret;
665 struct rpc_task *task;
666 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400667 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400668 .seq_args = args,
669 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400670 };
671 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000672 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673 .rpc_message = msg,
674 .callback_ops = &nfs41_call_sync_ops,
675 .callback_data = &data
676 };
677
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678 task = rpc_run_task(&task_setup);
679 if (IS_ERR(task))
680 ret = PTR_ERR(task);
681 else {
682 ret = task->tk_status;
683 rpc_put_task(task);
684 }
685 return ret;
686}
687
Trond Myklebustdf896452010-06-16 09:52:26 -0400688#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400689static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500690void nfs41_init_sequence(struct nfs4_sequence_args *args,
691 struct nfs4_sequence_res *res, int cache_reply)
692{
693}
694
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400695static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
696{
697}
698
699
Trond Myklebust14516c32010-07-31 14:29:06 -0400700static int nfs4_sequence_done(struct rpc_task *task,
701 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400702{
Trond Myklebust14516c32010-07-31 14:29:06 -0400703 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400704}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400705#endif /* CONFIG_NFS_V4_1 */
706
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400707static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000708int _nfs4_call_sync(struct rpc_clnt *clnt,
709 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400710 struct rpc_message *msg,
711 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400712 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400713{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000714 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400715}
716
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400717static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000718int nfs4_call_sync(struct rpc_clnt *clnt,
719 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000720 struct rpc_message *msg,
721 struct nfs4_sequence_args *args,
722 struct nfs4_sequence_res *res,
723 int cache_reply)
724{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400725 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000726 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400727 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000728}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400729
Trond Myklebust38478b22006-05-25 01:40:57 -0400730static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Trond Myklebust38478b22006-05-25 01:40:57 -0400732 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Trond Myklebust38478b22006-05-25 01:40:57 -0400734 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400735 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700736 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400737 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700738 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000739 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400740 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100743struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400744 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100745 struct nfs_openargs o_arg;
746 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100747 struct nfs_open_confirmargs c_arg;
748 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500749 struct nfs4_string owner_name;
750 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100751 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400753 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400755 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100757 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400758 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100759 int rpc_status;
760 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761};
762
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400763
764static void nfs4_init_opendata_res(struct nfs4_opendata *p)
765{
766 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400767 p->o_res.seqid = p->o_arg.seqid;
768 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400769 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400770 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400771 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500772 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400773}
774
Al Viro82a2c1b2011-06-22 18:30:55 -0400775static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500776 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400777 const struct iattr *attrs,
778 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779{
Al Viro82a2c1b2011-06-22 18:30:55 -0400780 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100781 struct inode *dir = parent->d_inode;
782 struct nfs_server *server = NFS_SERVER(dir);
783 struct nfs4_opendata *p;
784
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400785 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786 if (p == NULL)
787 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400788 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100789 if (p->o_arg.seqid == NULL)
790 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400791 nfs_sb_active(dentry->d_sb);
792 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 p->dir = parent;
794 p->owner = sp;
795 atomic_inc(&sp->so_count);
796 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500797 p->o_arg.open_flags = flags;
798 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700799 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
800 * will return permission denied for all bits until close */
801 if (!(flags & O_EXCL)) {
802 /* ask server to check for all possible rights as results
803 * are cached */
804 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
805 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700806 }
David Howells7539bba2006-08-22 20:06:09 -0400807 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400808 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
809 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400810 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100811 p->o_arg.server = server;
812 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400813 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500815 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500816 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400817
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 p->o_arg.u.attrs = &p->attrs;
819 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500820
821 verf[0] = jiffies;
822 verf[1] = current->pid;
823 memcpy(p->o_arg.u.verifier.data, verf,
824 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100826 p->c_arg.fh = &p->o_res.fh;
827 p->c_arg.stateid = &p->o_res.stateid;
828 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400829 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400830 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831 return p;
832err_free:
833 kfree(p);
834err:
835 dput(parent);
836 return NULL;
837}
838
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400839static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100840{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400841 struct nfs4_opendata *p = container_of(kref,
842 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400843 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400844
845 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400846 if (p->state != NULL)
847 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400848 nfs4_put_state_owner(p->owner);
849 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400850 dput(p->dentry);
851 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500852 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400853 kfree(p);
854}
855
856static void nfs4_opendata_put(struct nfs4_opendata *p)
857{
858 if (p != NULL)
859 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860}
861
Trond Myklebust06f814a2006-01-03 09:55:07 +0100862static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
863{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100864 int ret;
865
Trond Myklebust06f814a2006-01-03 09:55:07 +0100866 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100867 return ret;
868}
869
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400871{
872 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873
Trond Myklebust536e43d2012-01-17 22:04:26 -0500874 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875 goto out;
876 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400877 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500878 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
879 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400880 break;
881 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500882 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
883 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400884 break;
885 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500886 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
887 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400888 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400890 return ret;
891}
892
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400894{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500895 if (delegation == NULL)
896 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500899 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500901 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
902 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500903 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400904 return 1;
905}
906
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100908{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100910 case FMODE_WRITE:
911 state->n_wronly++;
912 break;
913 case FMODE_READ:
914 state->n_rdonly++;
915 break;
916 case FMODE_READ|FMODE_WRITE:
917 state->n_rdwr++;
918 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100920}
921
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400923{
924 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500925 nfs4_stateid_copy(&state->stateid, stateid);
926 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400928 case FMODE_READ:
929 set_bit(NFS_O_RDONLY_STATE, &state->flags);
930 break;
931 case FMODE_WRITE:
932 set_bit(NFS_O_WRONLY_STATE, &state->flags);
933 break;
934 case FMODE_READ|FMODE_WRITE:
935 set_bit(NFS_O_RDWR_STATE, &state->flags);
936 }
937}
938
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400940{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400941 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400943 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400944}
945
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946static 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 -0700947{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400948 /*
949 * Protect the call to nfs4_state_set_mode_locked and
950 * serialise the stateid update
951 */
952 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400953 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500954 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400955 set_bit(NFS_DELEGATED_STATE, &state->flags);
956 }
957 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400959 write_sequnlock(&state->seqlock);
960 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700962 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965static 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 -0500966{
967 struct nfs_inode *nfsi = NFS_I(state->inode);
968 struct nfs_delegation *deleg_cur;
969 int ret = 0;
970
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500972
973 rcu_read_lock();
974 deleg_cur = rcu_dereference(nfsi->delegation);
975 if (deleg_cur == NULL)
976 goto no_delegation;
977
978 spin_lock(&deleg_cur->lock);
979 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -0500980 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500982 goto no_delegation_unlock;
983
984 if (delegation == NULL)
985 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500986 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500987 goto no_delegation_unlock;
988
Trond Myklebustb7391f42008-12-23 15:21:52 -0500989 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500991 ret = 1;
992no_delegation_unlock:
993 spin_unlock(&deleg_cur->lock);
994no_delegation:
995 rcu_read_unlock();
996
997 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500999 ret = 1;
1000 }
1001
1002 return ret;
1003}
1004
1005
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001006static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001007{
1008 struct nfs_delegation *delegation;
1009
1010 rcu_read_lock();
1011 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013 rcu_read_unlock();
1014 return;
1015 }
1016 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001017 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001018}
1019
Trond Myklebust6ee41262007-07-08 14:11:36 -04001020static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021{
1022 struct nfs4_state *state = opendata->state;
1023 struct nfs_inode *nfsi = NFS_I(state->inode);
1024 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001025 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027 nfs4_stateid stateid;
1028 int ret = -EAGAIN;
1029
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033 if (can_open_cached(state, fmode, open_mode)) {
1034 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001035 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001036 goto out_return_state;
1037 }
1038 spin_unlock(&state->owner->so_lock);
1039 }
Trond Myklebust34310432008-12-23 15:21:38 -05001040 rcu_read_lock();
1041 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001042 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001043 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001044 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001045 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001046 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001047 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001049 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustaf22f942007-08-10 17:45:10 -04001050 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001051 if (ret != 0)
1052 goto out;
1053 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001054
1055 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001057 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001059out:
1060 return ERR_PTR(ret);
1061out_return_state:
1062 atomic_inc(&state->count);
1063 return state;
1064}
1065
Andy Adamsone23008e2012-10-02 21:07:32 -04001066static void
1067nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1068{
1069 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1070 struct nfs_delegation *delegation;
1071 int delegation_flags = 0;
1072
1073 rcu_read_lock();
1074 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1075 if (delegation)
1076 delegation_flags = delegation->flags;
1077 rcu_read_unlock();
1078 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1079 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1080 "returning a delegation for "
1081 "OPEN(CLAIM_DELEGATE_CUR)\n",
1082 clp->cl_hostname);
1083 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1084 nfs_inode_set_delegation(state->inode,
1085 data->owner->so_cred,
1086 &data->o_res);
1087 else
1088 nfs_inode_reclaim_delegation(state->inode,
1089 data->owner->so_cred,
1090 &data->o_res);
1091}
1092
1093/*
1094 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1095 * and update the nfs4_state.
1096 */
1097static struct nfs4_state *
1098_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1099{
1100 struct inode *inode = data->state->inode;
1101 struct nfs4_state *state = data->state;
1102 int ret;
1103
1104 if (!data->rpc_done) {
1105 ret = data->rpc_status;
1106 goto err;
1107 }
1108
1109 ret = -ESTALE;
1110 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1111 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1112 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1113 goto err;
1114
1115 ret = -ENOMEM;
1116 state = nfs4_get_open_state(inode, data->owner);
1117 if (state == NULL)
1118 goto err;
1119
1120 ret = nfs_refresh_inode(inode, &data->f_attr);
1121 if (ret)
1122 goto err;
1123
1124 if (data->o_res.delegation_type != 0)
1125 nfs4_opendata_check_deleg(data, state);
1126 update_open_stateid(state, &data->o_res.stateid, NULL,
1127 data->o_arg.fmode);
1128
1129 return state;
1130err:
1131 return ERR_PTR(ret);
1132
1133}
1134
1135static struct nfs4_state *
1136_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001137{
1138 struct inode *inode;
1139 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001140 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001141
Trond Myklebustaac00a82007-07-05 19:02:21 -04001142 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001143 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001144 goto out;
1145 }
1146
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001147 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001148 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001149 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001150 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001151 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001152 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001153 goto err;
1154 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001155 state = nfs4_get_open_state(inode, data->owner);
1156 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001157 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001158 if (data->o_res.delegation_type != 0)
1159 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001160 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001161 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001162 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001163out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001164 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001165 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001166err_put_inode:
1167 iput(inode);
1168err:
1169 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001170}
1171
Andy Adamsone23008e2012-10-02 21:07:32 -04001172static struct nfs4_state *
1173nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1174{
1175 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1176 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1177 return _nfs4_opendata_to_nfs4_state(data);
1178}
1179
Trond Myklebust864472e2006-01-03 09:55:15 +01001180static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1181{
1182 struct nfs_inode *nfsi = NFS_I(state->inode);
1183 struct nfs_open_context *ctx;
1184
1185 spin_lock(&state->inode->i_lock);
1186 list_for_each_entry(ctx, &nfsi->open_files, list) {
1187 if (ctx->state != state)
1188 continue;
1189 get_nfs_open_context(ctx);
1190 spin_unlock(&state->inode->i_lock);
1191 return ctx;
1192 }
1193 spin_unlock(&state->inode->i_lock);
1194 return ERR_PTR(-ENOENT);
1195}
1196
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001197static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1198{
1199 struct nfs4_opendata *opendata;
1200
Al Viro3d4ff432011-06-22 18:40:12 -04001201 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001202 if (opendata == NULL)
1203 return ERR_PTR(-ENOMEM);
1204 opendata->state = state;
1205 atomic_inc(&state->count);
1206 return opendata;
1207}
1208
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001209static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001210{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001211 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 int ret;
1213
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001214 opendata->o_arg.open_flags = 0;
1215 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001216 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1217 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1218 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001219 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 if (ret != 0)
1221 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001223 if (IS_ERR(newstate))
1224 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001225 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001226 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 return 0;
1228}
1229
1230static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1231{
Trond Myklebust864472e2006-01-03 09:55:15 +01001232 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 int ret;
1234
1235 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001236 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 smp_rmb();
1238 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001239 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001240 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001241 if (ret != 0)
1242 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001243 if (newstate != state)
1244 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001245 }
1246 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001247 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001248 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001249 if (ret != 0)
1250 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001251 if (newstate != state)
1252 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 }
1254 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001255 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001256 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001257 if (ret != 0)
1258 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001259 if (newstate != state)
1260 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001261 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001262 /*
1263 * We may have performed cached opens for all three recoveries.
1264 * Check if we need to update the current stateid.
1265 */
1266 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001267 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001268 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001269 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001270 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001271 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001272 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001273 return 0;
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276/*
1277 * OPEN_RECLAIM:
1278 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001280static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001282 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001284 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int status;
1286
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001287 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1288 if (IS_ERR(opendata))
1289 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001290 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1291 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001292 rcu_read_lock();
1293 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001294 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001295 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001296 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 opendata->o_arg.u.delegation_type = delegation_type;
1298 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001299 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return status;
1301}
1302
Trond Myklebust539cd032007-06-05 11:46:42 -04001303static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct nfs_server *server = NFS_SERVER(state->inode);
1306 struct nfs4_exception exception = { };
1307 int err;
1308 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001309 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001310 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001311 break;
1312 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 } while (exception.retry);
1314 return err;
1315}
1316
Trond Myklebust864472e2006-01-03 09:55:15 +01001317static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1318{
1319 struct nfs_open_context *ctx;
1320 int ret;
1321
1322 ctx = nfs4_state_find_open_context(state);
1323 if (IS_ERR(ctx))
1324 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001325 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001326 put_nfs_open_context(ctx);
1327 return ret;
1328}
1329
Trond Myklebust13437e12007-07-06 15:10:43 -04001330static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001332 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001333 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001335 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1336 if (IS_ERR(opendata))
1337 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001338 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001339 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001340 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001341 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Trond Myklebust13437e12007-07-06 15:10:43 -04001345int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001348 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int err;
1350 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001351 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 switch (err) {
1353 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001354 case -ENOENT:
1355 case -ESTALE:
1356 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001357 case -NFS4ERR_BADSESSION:
1358 case -NFS4ERR_BADSLOT:
1359 case -NFS4ERR_BAD_HIGH_SLOT:
1360 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1361 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05001362 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04001363 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05001364 err = -EAGAIN;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 case -NFS4ERR_STALE_CLIENTID:
1367 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05001368 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 case -NFS4ERR_EXPIRED:
1370 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001371 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05001372 err = -EAGAIN;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001373 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001374 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001375 case -NFS4ERR_ADMIN_REVOKED:
1376 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001377 nfs_inode_find_state_and_recover(state->inode,
1378 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001379 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001380 case -ENOMEM:
1381 err = 0;
1382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Trond Myklebust37380e42013-02-05 12:35:16 -05001384 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 err = nfs4_handle_exception(server, err, &exception);
1386 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001387out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return err;
1389}
1390
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001391static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1392{
1393 struct nfs4_opendata *data = calldata;
1394
1395 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001396 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001397 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001398 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001399 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001400 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001401 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001402}
1403
1404static void nfs4_open_confirm_release(void *calldata)
1405{
1406 struct nfs4_opendata *data = calldata;
1407 struct nfs4_state *state = NULL;
1408
1409 /* If this request hasn't been cancelled, do nothing */
1410 if (data->cancelled == 0)
1411 goto out_free;
1412 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001413 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001414 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001415 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001416 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001417 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001418out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001419 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001420}
1421
1422static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001423 .rpc_call_done = nfs4_open_confirm_done,
1424 .rpc_release = nfs4_open_confirm_release,
1425};
1426
1427/*
1428 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1429 */
1430static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1431{
1432 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1433 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001434 struct rpc_message msg = {
1435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1436 .rpc_argp = &data->c_arg,
1437 .rpc_resp = &data->c_res,
1438 .rpc_cred = data->owner->so_cred,
1439 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001440 struct rpc_task_setup task_setup_data = {
1441 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001442 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001443 .callback_ops = &nfs4_open_confirm_ops,
1444 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001445 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001446 .flags = RPC_TASK_ASYNC,
1447 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 int status;
1449
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001450 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001451 data->rpc_done = 0;
1452 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001453 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001454 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001455 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001456 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001457 status = nfs4_wait_for_completion_rpc_task(task);
1458 if (status != 0) {
1459 data->cancelled = 1;
1460 smp_wmb();
1461 } else
1462 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001463 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return status;
1465}
1466
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001467static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469 struct nfs4_opendata *data = calldata;
1470 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001471
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001473 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001474 /*
1475 * Check if we still need to send an OPEN call, or if we can use
1476 * a delegation instead.
1477 */
1478 if (data->state != NULL) {
1479 struct nfs_delegation *delegation;
1480
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001481 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001482 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001483 rcu_read_lock();
1484 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001485 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1486 can_open_delegated(delegation, data->o_arg.fmode))
1487 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001488 rcu_read_unlock();
1489 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001490 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001491 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001492 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001493 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001494 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001495 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1496 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001497 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001498 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001499 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001500 &data->o_res.seq_res,
1501 task) != 0)
1502 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001503 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001504unlock_no_action:
1505 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001506out_no_action:
1507 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001508out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001509 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001510}
1511
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001512static void nfs4_open_done(struct rpc_task *task, void *calldata)
1513{
1514 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001517
Trond Myklebust14516c32010-07-31 14:29:06 -04001518 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1519 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001520
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001522 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1523 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001524 case S_IFREG:
1525 break;
1526 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001528 break;
1529 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001531 break;
1532 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001534 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001535 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001536 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001537 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1538 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001539 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001540 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001541}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001542
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543static void nfs4_open_release(void *calldata)
1544{
1545 struct nfs4_opendata *data = calldata;
1546 struct nfs4_state *state = NULL;
1547
1548 /* If this request hasn't been cancelled, do nothing */
1549 if (data->cancelled == 0)
1550 goto out_free;
1551 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001552 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001553 goto out_free;
1554 /* In case we need an open_confirm, no cleanup! */
1555 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1556 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001558 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001559 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001560out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001561 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562}
1563
1564static const struct rpc_call_ops nfs4_open_ops = {
1565 .rpc_call_prepare = nfs4_open_prepare,
1566 .rpc_call_done = nfs4_open_done,
1567 .rpc_release = nfs4_open_release,
1568};
1569
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001570static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001571{
1572 struct inode *dir = data->dir->d_inode;
1573 struct nfs_server *server = NFS_SERVER(dir);
1574 struct nfs_openargs *o_arg = &data->o_arg;
1575 struct nfs_openres *o_res = &data->o_res;
1576 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001577 struct rpc_message msg = {
1578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1579 .rpc_argp = o_arg,
1580 .rpc_resp = o_res,
1581 .rpc_cred = data->owner->so_cred,
1582 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001583 struct rpc_task_setup task_setup_data = {
1584 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001585 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001586 .callback_ops = &nfs4_open_ops,
1587 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001588 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001589 .flags = RPC_TASK_ASYNC,
1590 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001591 int status;
1592
Trond Myklebust9d12b212012-01-17 22:04:25 -05001593 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001594 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001595 data->rpc_done = 0;
1596 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001597 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001598 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001599 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001600 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001601 if (IS_ERR(task))
1602 return PTR_ERR(task);
1603 status = nfs4_wait_for_completion_rpc_task(task);
1604 if (status != 0) {
1605 data->cancelled = 1;
1606 smp_wmb();
1607 } else
1608 status = data->rpc_status;
1609 rpc_put_task(task);
1610
1611 return status;
1612}
1613
1614static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1615{
1616 struct inode *dir = data->dir->d_inode;
1617 struct nfs_openres *o_res = &data->o_res;
1618 int status;
1619
1620 status = nfs4_run_open_task(data, 1);
1621 if (status != 0 || !data->rpc_done)
1622 return status;
1623
Trond Myklebust6926afd2012-01-07 13:22:46 -05001624 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1625
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001626 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1627 status = _nfs4_proc_open_confirm(data);
1628 if (status != 0)
1629 return status;
1630 }
1631
1632 return status;
1633}
1634
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001635static int nfs4_opendata_access(struct rpc_cred *cred,
1636 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001637 struct nfs4_state *state, fmode_t fmode,
1638 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001639{
1640 struct nfs_access_entry cache;
1641 u32 mask;
1642
1643 /* access call failed or for some reason the server doesn't
1644 * support any access modes -- defer access call until later */
1645 if (opendata->o_res.access_supported == 0)
1646 return 0;
1647
1648 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001649 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001650 * write mode bits, but POSIX allows the creating process to write.
1651 * use openflags to check for exec, because fmode won't
1652 * always have FMODE_EXEC set when file open for exec. */
1653 if (openflags & __FMODE_EXEC) {
1654 /* ONLY check for exec rights */
1655 mask = MAY_EXEC;
1656 } else if (fmode & FMODE_READ)
1657 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001658
1659 cache.cred = cred;
1660 cache.jiffies = jiffies;
1661 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1662 nfs_access_add_cache(state->inode, &cache);
1663
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001664 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001665 return 0;
1666
1667 /* even though OPEN succeeded, access is denied. Close the file */
1668 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001669 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001670}
1671
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001672/*
1673 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1674 */
1675static int _nfs4_proc_open(struct nfs4_opendata *data)
1676{
1677 struct inode *dir = data->dir->d_inode;
1678 struct nfs_server *server = NFS_SERVER(dir);
1679 struct nfs_openargs *o_arg = &data->o_arg;
1680 struct nfs_openres *o_res = &data->o_res;
1681 int status;
1682
1683 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001684 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001685 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001686 if (status != 0) {
1687 if (status == -NFS4ERR_BADNAME &&
1688 !(o_arg->open_flags & O_CREAT))
1689 return -ENOENT;
1690 return status;
1691 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001692
Trond Myklebust6926afd2012-01-07 13:22:46 -05001693 nfs_fattr_map_and_free_names(server, &data->f_attr);
1694
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001695 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001696 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001697 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1698 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001700 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001702 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001705 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Andy Adamsond83217c2011-03-01 01:34:17 +00001709static int nfs4_recover_expired_lease(struct nfs_server *server)
1710{
1711 return nfs4_client_recover_expired_lease(server->nfs_client);
1712}
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/*
1715 * OPEN_EXPIRED:
1716 * reclaim state on the server after a network partition.
1717 * Assumes caller holds the appropriate lock
1718 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001719static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001721 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001722 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001724 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1725 if (IS_ERR(opendata))
1726 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001727 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001728 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001729 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001730 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001731 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001734static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001735{
Trond Myklebust539cd032007-06-05 11:46:42 -04001736 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001737 struct nfs4_exception exception = { };
1738 int err;
1739
1740 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001741 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001742 switch (err) {
1743 default:
1744 goto out;
1745 case -NFS4ERR_GRACE:
1746 case -NFS4ERR_DELAY:
1747 nfs4_handle_exception(server, err, &exception);
1748 err = 0;
1749 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001750 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001751out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001752 return err;
1753}
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1756{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001758 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Trond Myklebust864472e2006-01-03 09:55:15 +01001760 ctx = nfs4_state_find_open_context(state);
1761 if (IS_ERR(ctx))
1762 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001763 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001764 put_nfs_open_context(ctx);
1765 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001768#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001769static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001770{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001771 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001772 nfs4_stateid *stateid = &state->stateid;
1773 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001774
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001775 /* If a state reset has been done, test_stateid is unneeded */
1776 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1777 return;
1778
1779 status = nfs41_test_stateid(server, stateid);
1780 if (status != NFS_OK) {
1781 /* Free the stateid unless the server explicitly
1782 * informs us the stateid is unrecognized. */
1783 if (status != -NFS4ERR_BAD_STATEID)
1784 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001785 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001786
Bryan Schumaker69388672012-09-26 15:25:52 -04001787 write_seqlock(&state->seqlock);
1788 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1789 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001790 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1791 }
1792}
1793
1794/**
1795 * nfs41_check_open_stateid - possibly free an open stateid
1796 *
1797 * @state: NFSv4 state for an inode
1798 *
1799 * Returns NFS_OK if recovery for this stateid is now finished.
1800 * Otherwise a negative NFS4ERR value is returned.
1801 */
1802static int nfs41_check_open_stateid(struct nfs4_state *state)
1803{
1804 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001805 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001806 int status;
1807
1808 /* If a state reset has been done, test_stateid is unneeded */
1809 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1810 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1811 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1812 return -NFS4ERR_BAD_STATEID;
1813
1814 status = nfs41_test_stateid(server, stateid);
1815 if (status != NFS_OK) {
1816 /* Free the stateid unless the server explicitly
1817 * informs us the stateid is unrecognized. */
1818 if (status != -NFS4ERR_BAD_STATEID)
1819 nfs41_free_stateid(server, stateid);
1820
1821 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1822 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1823 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001824 }
1825 return status;
1826}
1827
1828static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1829{
Chuck Levereb64cf92012-07-11 16:30:05 -04001830 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001831
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001832 nfs41_clear_delegation_stateid(state);
1833 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001834 if (status != NFS_OK)
1835 status = nfs4_open_expired(sp, state);
1836 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001837}
1838#endif
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001841 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1842 * fields corresponding to attributes that were used to store the verifier.
1843 * Make sure we clobber those fields in the later setattr call
1844 */
1845static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1846{
1847 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1848 !(sattr->ia_valid & ATTR_ATIME_SET))
1849 sattr->ia_valid |= ATTR_ATIME;
1850
1851 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1852 !(sattr->ia_valid & ATTR_MTIME_SET))
1853 sattr->ia_valid |= ATTR_MTIME;
1854}
1855
Trond Myklebustc21443c2013-02-07 14:26:21 -05001856static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1857 fmode_t fmode,
1858 int flags,
1859 struct nfs4_state **res)
1860{
1861 struct nfs4_state_owner *sp = opendata->owner;
1862 struct nfs_server *server = sp->so_server;
1863 struct nfs4_state *state;
1864 unsigned int seq;
1865 int ret;
1866
1867 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1868
1869 ret = _nfs4_proc_open(opendata);
1870 if (ret != 0)
1871 goto out;
1872
1873 state = nfs4_opendata_to_nfs4_state(opendata);
1874 ret = PTR_ERR(state);
1875 if (IS_ERR(state))
1876 goto out;
1877 if (server->caps & NFS_CAP_POSIX_LOCK)
1878 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1879
1880 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1881 if (ret != 0)
1882 goto out;
1883
1884 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
1885 nfs4_schedule_stateid_recovery(server, state);
1886 nfs4_wait_clnt_recover(server->nfs_client);
1887 }
1888 *res = state;
1889out:
1890 return ret;
1891}
1892
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001893/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001894 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 */
Andy Adamson82be4172012-05-23 05:02:35 -04001896static int _nfs4_do_open(struct inode *dir,
1897 struct dentry *dentry,
1898 fmode_t fmode,
1899 int flags,
1900 struct iattr *sattr,
1901 struct rpc_cred *cred,
1902 struct nfs4_state **res,
1903 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 struct nfs4_state_owner *sp;
1906 struct nfs4_state *state = NULL;
1907 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001908 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001909 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001913 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1914 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1916 goto out_err;
1917 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001918 status = nfs4_recover_expired_lease(server);
1919 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001920 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001921 if (dentry->d_inode != NULL)
1922 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001923 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001924 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001925 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001926 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Andy Adamson82be4172012-05-23 05:02:35 -04001928 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1929 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1930 if (!opendata->f_attr.mdsthreshold)
1931 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001932 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001933 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001934 if (dentry->d_inode != NULL)
1935 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001936
Trond Myklebustc21443c2013-02-07 14:26:21 -05001937 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001938 if (status != 0)
1939 goto err_opendata_put;
1940
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001941 if (opendata->o_arg.open_flags & O_EXCL) {
1942 nfs4_exclusive_attrset(opendata, sattr);
1943
1944 nfs_fattr_init(opendata->o_res.f_attr);
1945 status = nfs4_do_setattr(state->inode, cred,
1946 opendata->o_res.f_attr, sattr,
1947 state);
1948 if (status == 0)
1949 nfs_setattr_update_inode(state->inode, sattr);
1950 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1951 }
Andy Adamson82be4172012-05-23 05:02:35 -04001952
1953 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1954 *ctx_th = opendata->f_attr.mdsthreshold;
1955 else
1956 kfree(opendata->f_attr.mdsthreshold);
1957 opendata->f_attr.mdsthreshold = NULL;
1958
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001959 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 *res = state;
1962 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001963err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001964 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001965 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001966err_put_state_owner:
1967 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 *res = NULL;
1970 return status;
1971}
1972
1973
Andy Adamson82be4172012-05-23 05:02:35 -04001974static struct nfs4_state *nfs4_do_open(struct inode *dir,
1975 struct dentry *dentry,
1976 fmode_t fmode,
1977 int flags,
1978 struct iattr *sattr,
1979 struct rpc_cred *cred,
1980 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct nfs4_exception exception = { };
1983 struct nfs4_state *res;
1984 int status;
1985
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001986 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001988 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1989 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (status == 0)
1991 break;
1992 /* NOTE: BAD_SEQID means the server and client disagree about the
1993 * book-keeping w.r.t. state-changing operations
1994 * (OPEN/CLOSE/LOCK/LOCKU...)
1995 * It is actually a sign of a bug on the client or on the server.
1996 *
1997 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001998 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 * have unhashed the old state_owner for us, and that we can
2000 * therefore safely retry using a new one. We should still warn
2001 * the user though...
2002 */
2003 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002004 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002005 " returned a bad sequence-id error!\n",
2006 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 exception.retry = 1;
2008 continue;
2009 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002010 /*
2011 * BAD_STATEID on OPEN means that the server cancelled our
2012 * state before it received the OPEN_CONFIRM.
2013 * Recover by retrying the request as per the discussion
2014 * on Page 181 of RFC3530.
2015 */
2016 if (status == -NFS4ERR_BAD_STATEID) {
2017 exception.retry = 1;
2018 continue;
2019 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002020 if (status == -EAGAIN) {
2021 /* We must have found a delegation */
2022 exception.retry = 1;
2023 continue;
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2026 status, &exception));
2027 } while (exception.retry);
2028 return res;
2029}
2030
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002031static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2032 struct nfs_fattr *fattr, struct iattr *sattr,
2033 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002035 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002037 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 .iap = sattr,
2039 .server = server,
2040 .bitmask = server->attr_bitmask,
2041 };
2042 struct nfs_setattrres res = {
2043 .fattr = fattr,
2044 .server = server,
2045 };
2046 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2048 .rpc_argp = &arg,
2049 .rpc_resp = &res,
2050 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002052 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002053 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Trond Myklebust0e574af2005-10-27 22:12:38 -04002055 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Trond Myklebust4fc87962012-03-08 17:42:01 -05002057 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002058 struct nfs_lockowner lockowner = {
2059 .l_owner = current->files,
2060 .l_pid = current->tgid,
2061 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002062 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002063 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002064 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2065 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002066 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002067 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002068 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Bryan Schumaker7c513052011-03-24 17:12:24 +00002070 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002071 if (status == 0 && state != NULL)
2072 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002073 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002076static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2077 struct nfs_fattr *fattr, struct iattr *sattr,
2078 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002080 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002081 struct nfs4_exception exception = {
2082 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002083 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002084 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 int err;
2086 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002087 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2088 switch (err) {
2089 case -NFS4ERR_OPENMODE:
2090 if (state && !(state->state & FMODE_WRITE)) {
2091 err = -EBADF;
2092 if (sattr->ia_valid & ATTR_OPEN)
2093 err = -EACCES;
2094 goto out;
2095 }
2096 }
2097 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002099out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return err;
2101}
2102
2103struct nfs4_closedata {
2104 struct inode *inode;
2105 struct nfs4_state *state;
2106 struct nfs_closeargs arg;
2107 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002108 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002109 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002110 bool roc;
2111 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112};
2113
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002114static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002115{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002116 struct nfs4_closedata *calldata = data;
2117 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002118 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002119
Fred Isamanf7e89172011-01-06 11:36:32 +00002120 if (calldata->roc)
2121 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002122 nfs4_put_open_state(calldata->state);
2123 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002124 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002125 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002126 kfree(calldata);
2127}
2128
Trond Myklebust88069f72009-12-08 08:33:16 -05002129static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2130 fmode_t fmode)
2131{
2132 spin_lock(&state->owner->so_lock);
2133 if (!(fmode & FMODE_READ))
2134 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2135 if (!(fmode & FMODE_WRITE))
2136 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2137 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2138 spin_unlock(&state->owner->so_lock);
2139}
2140
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002141static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002143 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 struct nfs_server *server = NFS_SERVER(calldata->inode);
2146
Chuck Levera3ca5652012-03-01 17:00:40 -05002147 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002148 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2149 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /* hmm. we are done with the inode, and in the process of freeing
2151 * the state_owner. we keep this around to process errors
2152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 switch (task->tk_status) {
2154 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002155 if (calldata->roc)
2156 pnfs_roc_set_barrier(state->inode,
2157 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002158 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002159 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002160 nfs4_close_clear_stateid_flags(state,
2161 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 break;
2163 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002164 case -NFS4ERR_OLD_STATEID:
2165 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002167 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002170 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2171 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002173 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002174 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002175 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002178static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002180 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002181 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002182 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002183 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002184
Chuck Levera3ca5652012-03-01 17:00:40 -05002185 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002186 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002187 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002188
Trond Myklebust88069f72009-12-08 08:33:16 -05002189 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2190 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002191 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002192 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002193 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002194 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002195 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2196 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2197 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002198 }
2199 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002200 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2201 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2202 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002203 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002204 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002205 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002206
2207 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002208 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002209 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002210 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002211
Fred Isamanf7e89172011-01-06 11:36:32 +00002212 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002213 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002214 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002215 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002216 goto out_wait;
Fred Isamanf7e89172011-01-06 11:36:32 +00002217 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002218
Trond Myklebust516a6af2005-10-27 22:12:41 -04002219 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002220 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002221 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002222 &calldata->arg.seq_args,
2223 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002224 task) != 0)
2225 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002226 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002227 return;
2228out_no_action:
2229 task->tk_action = NULL;
2230out_wait:
2231 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002234static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002235 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002236 .rpc_call_done = nfs4_close_done,
2237 .rpc_release = nfs4_free_closedata,
2238};
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240/*
2241 * It is possible for data to be read/written from a mem-mapped file
2242 * after the sys_close call (which hits the vfs layer as a flush).
2243 * This means that we can't safely call nfsv4 close on a file until
2244 * the inode is cleared. This in turn means that we are not good
2245 * NFSv4 citizens - we do not indicate to the server to update the file's
2246 * share state even when we are done with one of the three share
2247 * stateid's in the inode.
2248 *
2249 * NOTE: Caller must be holding the sp->so_owner semaphore!
2250 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002251int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002253 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002255 struct nfs4_state_owner *sp = state->owner;
2256 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002257 struct rpc_message msg = {
2258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2259 .rpc_cred = state->owner->so_cred,
2260 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002261 struct rpc_task_setup task_setup_data = {
2262 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002263 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002264 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002265 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002266 .flags = RPC_TASK_ASYNC,
2267 };
Trond Myklebust95121352005-10-18 14:20:12 -07002268 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002270 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002272 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002273 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002274 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002276 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002277 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002279 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002280 if (calldata->arg.seqid == NULL)
2281 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002282 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002283 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002284 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002285 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002286 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002287 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002288 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002289
Trond Myklebust1174dd12010-12-21 10:52:24 -05002290 msg.rpc_argp = &calldata->arg;
2291 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002292 task_setup_data.callback_data = calldata;
2293 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002294 if (IS_ERR(task))
2295 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002296 status = 0;
2297 if (wait)
2298 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002299 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002300 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002301out_free_calldata:
2302 kfree(calldata);
2303out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002304 nfs4_put_open_state(state);
2305 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002306 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Trond Myklebust2b484292010-09-17 10:56:51 -04002309static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002310nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 struct nfs4_state *state;
2313
Trond Myklebust565277f2007-10-15 18:17:53 -04002314 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002315 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2316 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002317 if (IS_ERR(state))
2318 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002319 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002320 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
Trond Myklebust1185a552009-12-03 15:54:02 -05002323static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002324{
2325 if (ctx->state == NULL)
2326 return;
2327 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002328 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002329 else
Al Viro643168c2011-06-22 18:20:23 -04002330 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002331}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2334{
Benny Halevy43652ad2009-04-01 09:21:54 -04002335 struct nfs4_server_caps_arg args = {
2336 .fhandle = fhandle,
2337 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 struct nfs4_server_caps_res res = {};
2339 struct rpc_message msg = {
2340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002341 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 .rpc_resp = &res,
2343 };
2344 int status;
2345
Bryan Schumaker7c513052011-03-24 17:12:24 +00002346 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (status == 0) {
2348 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002349 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2350 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2351 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2352 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2353 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2355 server->caps |= NFS_CAP_ACLS;
2356 if (res.has_links != 0)
2357 server->caps |= NFS_CAP_HARDLINKS;
2358 if (res.has_symlinks != 0)
2359 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002360 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2361 server->caps |= NFS_CAP_FILEID;
2362 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2363 server->caps |= NFS_CAP_MODE;
2364 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2365 server->caps |= NFS_CAP_NLINK;
2366 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2367 server->caps |= NFS_CAP_OWNER;
2368 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2369 server->caps |= NFS_CAP_OWNER_GROUP;
2370 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2371 server->caps |= NFS_CAP_ATIME;
2372 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2373 server->caps |= NFS_CAP_CTIME;
2374 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2375 server->caps |= NFS_CAP_MTIME;
2376
Trond Myklebusta65318b2009-03-11 14:10:28 -04002377 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2378 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2379 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002381 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return status;
2385}
2386
Trond Myklebust55a97592006-06-09 09:34:19 -04002387int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
2389 struct nfs4_exception exception = { };
2390 int err;
2391 do {
2392 err = nfs4_handle_exception(server,
2393 _nfs4_server_capabilities(server, fhandle),
2394 &exception);
2395 } while (exception.retry);
2396 return err;
2397}
2398
2399static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2400 struct nfs_fsinfo *info)
2401{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 struct nfs4_lookup_root_arg args = {
2403 .bitmask = nfs4_fattr_bitmap,
2404 };
2405 struct nfs4_lookup_res res = {
2406 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002407 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 .fh = fhandle,
2409 };
2410 struct rpc_message msg = {
2411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2412 .rpc_argp = &args,
2413 .rpc_resp = &res,
2414 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002415
Trond Myklebust0e574af2005-10-27 22:12:38 -04002416 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002417 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
2420static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2421 struct nfs_fsinfo *info)
2422{
2423 struct nfs4_exception exception = { };
2424 int err;
2425 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002426 err = _nfs4_lookup_root(server, fhandle, info);
2427 switch (err) {
2428 case 0:
2429 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002430 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002431 default:
2432 err = nfs4_handle_exception(server, err, &exception);
2433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002435out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return err;
2437}
2438
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002439static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2440 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2441{
2442 struct rpc_auth *auth;
2443 int ret;
2444
2445 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002446 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002447 ret = -EIO;
2448 goto out;
2449 }
2450 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002451out:
2452 return ret;
2453}
2454
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002455static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002456 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002458 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002459 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002461 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002462 if (len < 0)
2463 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002464
2465 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002466 /* AUTH_UNIX is the default flavor if none was specified,
2467 * thus has already been tried. */
2468 if (flav_array[i] == RPC_AUTH_UNIX)
2469 continue;
2470
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002471 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002472 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002473 continue;
2474 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002475 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002476 /*
2477 * -EACCESS could mean that the user doesn't have correct permissions
2478 * to access the mount. It could also mean that we tried to mount
2479 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2480 * existing mount programs don't handle -EACCES very well so it should
2481 * be mapped to -EPERM instead.
2482 */
2483 if (status == -EACCES)
2484 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002485 return status;
2486}
2487
2488/*
2489 * get the file handle for the "/" directory on the server
2490 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002491int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2492 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002493{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002494 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002495 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002496 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2497 /*
2498 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2499 * by nfs4_map_errors() as this function exits.
2500 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002501 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (status == 0)
2503 status = nfs4_server_capabilities(server, fhandle);
2504 if (status == 0)
2505 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002506 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
Bryan Schumakerbae36242012-05-10 15:07:31 -04002509static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2510 struct nfs_fsinfo *info)
2511{
2512 int error;
2513 struct nfs_fattr *fattr = info->fattr;
2514
2515 error = nfs4_server_capabilities(server, mntfh);
2516 if (error < 0) {
2517 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2518 return error;
2519 }
2520
2521 error = nfs4_proc_getattr(server, mntfh, fattr);
2522 if (error < 0) {
2523 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2524 return error;
2525 }
2526
2527 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2528 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2529 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2530
2531 return error;
2532}
2533
Manoj Naik6b97fd32006-06-09 09:34:29 -04002534/*
2535 * Get locations and (maybe) other attributes of a referral.
2536 * Note that we'll actually follow the referral later when
2537 * we detect fsid mismatch in inode revalidation
2538 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002539static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2540 const struct qstr *name, struct nfs_fattr *fattr,
2541 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002542{
2543 int status = -ENOMEM;
2544 struct page *page = NULL;
2545 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002546
2547 page = alloc_page(GFP_KERNEL);
2548 if (page == NULL)
2549 goto out;
2550 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2551 if (locations == NULL)
2552 goto out;
2553
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002554 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002555 if (status != 0)
2556 goto out;
2557 /* Make sure server returned a different fsid for the referral */
2558 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002559 dprintk("%s: server did not return a different fsid for"
2560 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002561 status = -EIO;
2562 goto out;
2563 }
Andy Adamson533eb462011-06-13 18:25:56 -04002564 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2565 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002566
Andy Adamson533eb462011-06-13 18:25:56 -04002567 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002568 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002569 memset(fhandle, 0, sizeof(struct nfs_fh));
2570out:
2571 if (page)
2572 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002573 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002574 return status;
2575}
2576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2578{
2579 struct nfs4_getattr_arg args = {
2580 .fh = fhandle,
2581 .bitmask = server->attr_bitmask,
2582 };
2583 struct nfs4_getattr_res res = {
2584 .fattr = fattr,
2585 .server = server,
2586 };
2587 struct rpc_message msg = {
2588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2589 .rpc_argp = &args,
2590 .rpc_resp = &res,
2591 };
2592
Trond Myklebust0e574af2005-10-27 22:12:38 -04002593 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002594 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
2597static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2598{
2599 struct nfs4_exception exception = { };
2600 int err;
2601 do {
2602 err = nfs4_handle_exception(server,
2603 _nfs4_proc_getattr(server, fhandle, fattr),
2604 &exception);
2605 } while (exception.retry);
2606 return err;
2607}
2608
2609/*
2610 * The file is not closed if it is opened due to the a request to change
2611 * the size of the file. The open call will not be needed once the
2612 * VFS layer lookup-intents are implemented.
2613 *
2614 * Close is called when the inode is destroyed.
2615 * If we haven't opened the file for O_WRONLY, we
2616 * need to in the size_change case to obtain a stateid.
2617 *
2618 * Got race?
2619 * Because OPEN is always done by name in nfsv4, it is
2620 * possible that we opened a different file by the same
2621 * name. We can recognize this race condition, but we
2622 * can't do anything about it besides returning an error.
2623 *
2624 * This will be fixed with VFS changes (lookup-intent).
2625 */
2626static int
2627nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2628 struct iattr *sattr)
2629{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002630 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002631 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002632 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 int status;
2634
Benny Halevy8a1636c2010-07-14 15:43:57 -04002635 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002636 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002637
Trond Myklebust0e574af2005-10-27 22:12:38 -04002638 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Andy Adamson26699402012-05-30 16:12:24 -04002640 /* Deal with open(O_TRUNC) */
2641 if (sattr->ia_valid & ATTR_OPEN)
2642 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2643
2644 /* Optimization: if the end result is no change, don't RPC */
2645 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2646 return 0;
2647
Trond Myklebustd5308382005-11-04 15:33:38 -05002648 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002649 if (sattr->ia_valid & ATTR_FILE) {
2650 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002651
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002652 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002653 if (ctx) {
2654 cred = ctx->cred;
2655 state = ctx->state;
2656 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002657 }
2658
2659 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002660 if (status == 0)
2661 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return status;
2663}
2664
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002665static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2666 const struct qstr *name, struct nfs_fh *fhandle,
2667 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002668{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002669 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002670 int status;
2671 struct nfs4_lookup_arg args = {
2672 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002673 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002674 .name = name,
2675 };
2676 struct nfs4_lookup_res res = {
2677 .server = server,
2678 .fattr = fattr,
2679 .fh = fhandle,
2680 };
2681 struct rpc_message msg = {
2682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2683 .rpc_argp = &args,
2684 .rpc_resp = &res,
2685 };
2686
2687 nfs_fattr_init(fattr);
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002690 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 dprintk("NFS reply lookup: %d\n", status);
2692 return status;
2693}
2694
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002695static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002696{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002697 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002698 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002699 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2700 fattr->nlink = 2;
2701}
2702
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002703static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2704 struct qstr *name, struct nfs_fh *fhandle,
2705 struct nfs_fattr *fattr)
2706{
2707 struct nfs4_exception exception = { };
2708 struct rpc_clnt *client = *clnt;
2709 int err;
2710 do {
2711 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2712 switch (err) {
2713 case -NFS4ERR_BADNAME:
2714 err = -ENOENT;
2715 goto out;
2716 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002717 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002718 goto out;
2719 case -NFS4ERR_WRONGSEC:
2720 err = -EPERM;
2721 if (client != *clnt)
2722 goto out;
2723
2724 client = nfs4_create_sec_client(client, dir, name);
2725 if (IS_ERR(client))
2726 return PTR_ERR(client);
2727
2728 exception.retry = 1;
2729 break;
2730 default:
2731 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2732 }
2733 } while (exception.retry);
2734
2735out:
2736 if (err == 0)
2737 *clnt = client;
2738 else if (client != *clnt)
2739 rpc_shutdown_client(client);
2740
2741 return err;
2742}
2743
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002744static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002745 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002747 int status;
2748 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002749
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002750 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2751 if (client != NFS_CLIENT(dir)) {
2752 rpc_shutdown_client(client);
2753 nfs_fixup_secinfo_attributes(fattr);
2754 }
2755 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756}
2757
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002758struct rpc_clnt *
2759nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2760 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2761{
2762 int status;
2763 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2764
2765 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2766 if (status < 0) {
2767 rpc_shutdown_client(client);
2768 return ERR_PTR(status);
2769 }
2770 return client;
2771}
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2774{
Trond Myklebust76b32992007-08-10 17:45:11 -04002775 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 struct nfs4_accessargs args = {
2777 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002778 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002780 struct nfs4_accessres res = {
2781 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002782 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 struct rpc_message msg = {
2784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2785 .rpc_argp = &args,
2786 .rpc_resp = &res,
2787 .rpc_cred = entry->cred,
2788 };
2789 int mode = entry->mask;
2790 int status;
2791
2792 /*
2793 * Determine which access bits we want to ask for...
2794 */
2795 if (mode & MAY_READ)
2796 args.access |= NFS4_ACCESS_READ;
2797 if (S_ISDIR(inode->i_mode)) {
2798 if (mode & MAY_WRITE)
2799 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2800 if (mode & MAY_EXEC)
2801 args.access |= NFS4_ACCESS_LOOKUP;
2802 } else {
2803 if (mode & MAY_WRITE)
2804 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2805 if (mode & MAY_EXEC)
2806 args.access |= NFS4_ACCESS_EXECUTE;
2807 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002808
2809 res.fattr = nfs_alloc_fattr();
2810 if (res.fattr == NULL)
2811 return -ENOMEM;
2812
Bryan Schumaker7c513052011-03-24 17:12:24 +00002813 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002815 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002816 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002818 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return status;
2820}
2821
2822static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2823{
2824 struct nfs4_exception exception = { };
2825 int err;
2826 do {
2827 err = nfs4_handle_exception(NFS_SERVER(inode),
2828 _nfs4_proc_access(inode, entry),
2829 &exception);
2830 } while (exception.retry);
2831 return err;
2832}
2833
2834/*
2835 * TODO: For the time being, we don't try to get any attributes
2836 * along with any of the zero-copy operations READ, READDIR,
2837 * READLINK, WRITE.
2838 *
2839 * In the case of the first three, we want to put the GETATTR
2840 * after the read-type operation -- this is because it is hard
2841 * to predict the length of a GETATTR response in v4, and thus
2842 * align the READ data correctly. This means that the GETATTR
2843 * may end up partially falling into the page cache, and we should
2844 * shift it into the 'tail' of the xdr_buf before processing.
2845 * To do this efficiently, we need to know the total length
2846 * of data received, which doesn't seem to be available outside
2847 * of the RPC layer.
2848 *
2849 * In the case of WRITE, we also want to put the GETATTR after
2850 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002851 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 *
2853 * Both of these changes to the XDR layer would in fact be quite
2854 * minor, but I decided to leave them for a subsequent patch.
2855 */
2856static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2857 unsigned int pgbase, unsigned int pglen)
2858{
2859 struct nfs4_readlink args = {
2860 .fh = NFS_FH(inode),
2861 .pgbase = pgbase,
2862 .pglen = pglen,
2863 .pages = &page,
2864 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002865 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 struct rpc_message msg = {
2867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2868 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002869 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 };
2871
Bryan Schumaker7c513052011-03-24 17:12:24 +00002872 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 -07002873}
2874
2875static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2876 unsigned int pgbase, unsigned int pglen)
2877{
2878 struct nfs4_exception exception = { };
2879 int err;
2880 do {
2881 err = nfs4_handle_exception(NFS_SERVER(inode),
2882 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2883 &exception);
2884 } while (exception.retry);
2885 return err;
2886}
2887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002889 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891static int
2892nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002893 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002895 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 int status = 0;
2898
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002899 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2900 if (IS_ERR(ctx))
2901 return PTR_ERR(ctx);
2902
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002903 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002904 state = nfs4_do_open(dir, dentry, ctx->mode,
2905 flags, sattr, ctx->cred,
2906 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002907 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (IS_ERR(state)) {
2909 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002910 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002912 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002913 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002914 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002916 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return status;
2918}
2919
2920static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2921{
Trond Myklebust16e42952005-10-27 22:12:44 -04002922 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002923 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002925 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002927 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002928 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002929 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2932 .rpc_argp = &args,
2933 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002935 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002936
Bryan Schumaker7c513052011-03-24 17:12:24 +00002937 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002938 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002939 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return status;
2941}
2942
2943static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2944{
2945 struct nfs4_exception exception = { };
2946 int err;
2947 do {
2948 err = nfs4_handle_exception(NFS_SERVER(dir),
2949 _nfs4_proc_remove(dir, name),
2950 &exception);
2951 } while (exception.retry);
2952 return err;
2953}
2954
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002955static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002957 struct nfs_server *server = NFS_SERVER(dir);
2958 struct nfs_removeargs *args = msg->rpc_argp;
2959 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002961 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002963 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002966static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2967{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002968 nfs4_setup_sequence(NFS_SERVER(data->dir),
2969 &data->args.seq_args,
2970 &data->res.seq_res,
2971 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002974static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002976 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2977
Trond Myklebust14516c32010-07-31 14:29:06 -04002978 if (!nfs4_sequence_done(task, &res->seq_res))
2979 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002980 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002981 return 0;
2982 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002983 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
Jeff Laytond3d41522010-09-17 17:31:57 -04002986static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2987{
2988 struct nfs_server *server = NFS_SERVER(dir);
2989 struct nfs_renameargs *arg = msg->rpc_argp;
2990 struct nfs_renameres *res = msg->rpc_resp;
2991
2992 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002993 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002994 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002995}
2996
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002997static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2998{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002999 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3000 &data->args.seq_args,
3001 &data->res.seq_res,
3002 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003003}
3004
3005static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3006 struct inode *new_dir)
3007{
3008 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3009
3010 if (!nfs4_sequence_done(task, &res->seq_res))
3011 return 0;
3012 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3013 return 0;
3014
3015 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003016 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003017 return 1;
3018}
3019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3021 struct inode *new_dir, struct qstr *new_name)
3022{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003023 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003024 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 .old_dir = NFS_FH(old_dir),
3026 .new_dir = NFS_FH(new_dir),
3027 .old_name = old_name,
3028 .new_name = new_name,
3029 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003030 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003031 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003032 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 struct rpc_message msg = {
3034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3035 .rpc_argp = &arg,
3036 .rpc_resp = &res,
3037 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003038 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Bryan Schumaker7c513052011-03-24 17:12:24 +00003040 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 if (!status) {
3042 update_changeattr(old_dir, &res.old_cinfo);
3043 update_changeattr(new_dir, &res.new_cinfo);
3044 }
3045 return status;
3046}
3047
3048static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3049 struct inode *new_dir, struct qstr *new_name)
3050{
3051 struct nfs4_exception exception = { };
3052 int err;
3053 do {
3054 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3055 _nfs4_proc_rename(old_dir, old_name,
3056 new_dir, new_name),
3057 &exception);
3058 } while (exception.retry);
3059 return err;
3060}
3061
3062static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3063{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003064 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 struct nfs4_link_arg arg = {
3066 .fh = NFS_FH(inode),
3067 .dir_fh = NFS_FH(dir),
3068 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003069 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003071 struct nfs4_link_res res = {
3072 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003073 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 struct rpc_message msg = {
3075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3076 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003077 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003079 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Trond Myklebust136f2622010-04-16 16:22:49 -04003081 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003082 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003083 goto out;
3084
Bryan Schumaker7c513052011-03-24 17:12:24 +00003085 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003086 if (!status) {
3087 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003088 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003089 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003090out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003091 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return status;
3093}
3094
3095static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3096{
3097 struct nfs4_exception exception = { };
3098 int err;
3099 do {
3100 err = nfs4_handle_exception(NFS_SERVER(inode),
3101 _nfs4_proc_link(inode, dir, name),
3102 &exception);
3103 } while (exception.retry);
3104 return err;
3105}
3106
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003107struct nfs4_createdata {
3108 struct rpc_message msg;
3109 struct nfs4_create_arg arg;
3110 struct nfs4_create_res res;
3111 struct nfs_fh fh;
3112 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003113};
3114
3115static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3116 struct qstr *name, struct iattr *sattr, u32 ftype)
3117{
3118 struct nfs4_createdata *data;
3119
3120 data = kzalloc(sizeof(*data), GFP_KERNEL);
3121 if (data != NULL) {
3122 struct nfs_server *server = NFS_SERVER(dir);
3123
3124 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3125 data->msg.rpc_argp = &data->arg;
3126 data->msg.rpc_resp = &data->res;
3127 data->arg.dir_fh = NFS_FH(dir);
3128 data->arg.server = server;
3129 data->arg.name = name;
3130 data->arg.attrs = sattr;
3131 data->arg.ftype = ftype;
3132 data->arg.bitmask = server->attr_bitmask;
3133 data->res.server = server;
3134 data->res.fh = &data->fh;
3135 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003136 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003137 }
3138 return data;
3139}
3140
3141static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3142{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003143 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003144 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003145 if (status == 0) {
3146 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003147 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3148 }
3149 return status;
3150}
3151
3152static void nfs4_free_createdata(struct nfs4_createdata *data)
3153{
3154 kfree(data);
3155}
3156
Chuck Lever4f390c12006-08-22 20:06:22 -04003157static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003158 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003160 struct nfs4_createdata *data;
3161 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Chuck Lever94a6d752006-08-22 20:06:23 -04003163 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003164 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003165
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003166 status = -ENOMEM;
3167 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3168 if (data == NULL)
3169 goto out;
3170
3171 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3172 data->arg.u.symlink.pages = &page;
3173 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003175 status = nfs4_do_create(dir, dentry, data);
3176
3177 nfs4_free_createdata(data);
3178out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 return status;
3180}
3181
Chuck Lever4f390c12006-08-22 20:06:22 -04003182static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003183 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184{
3185 struct nfs4_exception exception = { };
3186 int err;
3187 do {
3188 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003189 _nfs4_proc_symlink(dir, dentry, page,
3190 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 &exception);
3192 } while (exception.retry);
3193 return err;
3194}
3195
3196static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3197 struct iattr *sattr)
3198{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003199 struct nfs4_createdata *data;
3200 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003202 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3203 if (data == NULL)
3204 goto out;
3205
3206 status = nfs4_do_create(dir, dentry, data);
3207
3208 nfs4_free_createdata(data);
3209out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return status;
3211}
3212
3213static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3214 struct iattr *sattr)
3215{
3216 struct nfs4_exception exception = { };
3217 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003218
3219 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 do {
3221 err = nfs4_handle_exception(NFS_SERVER(dir),
3222 _nfs4_proc_mkdir(dir, dentry, sattr),
3223 &exception);
3224 } while (exception.retry);
3225 return err;
3226}
3227
3228static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003229 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
3231 struct inode *dir = dentry->d_inode;
3232 struct nfs4_readdir_arg args = {
3233 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003234 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 .pgbase = 0,
3236 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003237 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003238 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 };
3240 struct nfs4_readdir_res res;
3241 struct rpc_message msg = {
3242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3243 .rpc_argp = &args,
3244 .rpc_resp = &res,
3245 .rpc_cred = cred,
3246 };
3247 int status;
3248
Harvey Harrison3110ff82008-05-02 13:42:44 -07003249 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003250 dentry->d_parent->d_name.name,
3251 dentry->d_name.name,
3252 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003253 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003255 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 -05003256 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003257 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003258 status += args.pgbase;
3259 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003260
3261 nfs_invalidate_atime(dir);
3262
Harvey Harrison3110ff82008-05-02 13:42:44 -07003263 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return status;
3265}
3266
3267static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003268 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 struct nfs4_exception exception = { };
3271 int err;
3272 do {
3273 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3274 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003275 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 &exception);
3277 } while (exception.retry);
3278 return err;
3279}
3280
3281static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3282 struct iattr *sattr, dev_t rdev)
3283{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003284 struct nfs4_createdata *data;
3285 int mode = sattr->ia_mode;
3286 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003288 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3289 if (data == NULL)
3290 goto out;
3291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003293 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003295 data->arg.ftype = NF4BLK;
3296 data->arg.u.device.specdata1 = MAJOR(rdev);
3297 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
3299 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003300 data->arg.ftype = NF4CHR;
3301 data->arg.u.device.specdata1 = MAJOR(rdev);
3302 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003303 } else if (!S_ISSOCK(mode)) {
3304 status = -EINVAL;
3305 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003308 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003309out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003310 nfs4_free_createdata(data);
3311out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 return status;
3313}
3314
3315static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3316 struct iattr *sattr, dev_t rdev)
3317{
3318 struct nfs4_exception exception = { };
3319 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003320
3321 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 do {
3323 err = nfs4_handle_exception(NFS_SERVER(dir),
3324 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3325 &exception);
3326 } while (exception.retry);
3327 return err;
3328}
3329
3330static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3331 struct nfs_fsstat *fsstat)
3332{
3333 struct nfs4_statfs_arg args = {
3334 .fh = fhandle,
3335 .bitmask = server->attr_bitmask,
3336 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003337 struct nfs4_statfs_res res = {
3338 .fsstat = fsstat,
3339 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 struct rpc_message msg = {
3341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3342 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003343 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 };
3345
Trond Myklebust0e574af2005-10-27 22:12:38 -04003346 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003347 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348}
3349
3350static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3351{
3352 struct nfs4_exception exception = { };
3353 int err;
3354 do {
3355 err = nfs4_handle_exception(server,
3356 _nfs4_proc_statfs(server, fhandle, fsstat),
3357 &exception);
3358 } while (exception.retry);
3359 return err;
3360}
3361
3362static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3363 struct nfs_fsinfo *fsinfo)
3364{
3365 struct nfs4_fsinfo_arg args = {
3366 .fh = fhandle,
3367 .bitmask = server->attr_bitmask,
3368 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003369 struct nfs4_fsinfo_res res = {
3370 .fsinfo = fsinfo,
3371 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 struct rpc_message msg = {
3373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3374 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003375 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 };
3377
Bryan Schumaker7c513052011-03-24 17:12:24 +00003378 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379}
3380
3381static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3382{
3383 struct nfs4_exception exception = { };
3384 int err;
3385
3386 do {
3387 err = nfs4_handle_exception(server,
3388 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3389 &exception);
3390 } while (exception.retry);
3391 return err;
3392}
3393
3394static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3395{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003396 int error;
3397
Trond Myklebust0e574af2005-10-27 22:12:38 -04003398 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003399 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003400 if (error == 0) {
3401 /* block layout checks this! */
3402 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003403 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003404 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003405
3406 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
3409static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3410 struct nfs_pathconf *pathconf)
3411{
3412 struct nfs4_pathconf_arg args = {
3413 .fh = fhandle,
3414 .bitmask = server->attr_bitmask,
3415 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003416 struct nfs4_pathconf_res res = {
3417 .pathconf = pathconf,
3418 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 struct rpc_message msg = {
3420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3421 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003422 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 };
3424
3425 /* None of the pathconf attributes are mandatory to implement */
3426 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3427 memset(pathconf, 0, sizeof(*pathconf));
3428 return 0;
3429 }
3430
Trond Myklebust0e574af2005-10-27 22:12:38 -04003431 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003432 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433}
3434
3435static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3436 struct nfs_pathconf *pathconf)
3437{
3438 struct nfs4_exception exception = { };
3439 int err;
3440
3441 do {
3442 err = nfs4_handle_exception(server,
3443 _nfs4_proc_pathconf(server, fhandle, pathconf),
3444 &exception);
3445 } while (exception.retry);
3446 return err;
3447}
3448
Benny Halevyd20581a2011-05-22 19:52:03 +03003449void __nfs4_read_done_cb(struct nfs_read_data *data)
3450{
Fred Isamancd841602012-04-20 14:47:44 -04003451 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003452}
3453
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003454static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
Fred Isamancd841602012-04-20 14:47:44 -04003456 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003458 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003459 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003460 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003462
Benny Halevyd20581a2011-05-22 19:52:03 +03003463 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003465 renew_lease(server, data->timestamp);
3466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
3468
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003469static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3470{
3471
3472 dprintk("--> %s\n", __func__);
3473
3474 if (!nfs4_sequence_done(task, &data->res.seq_res))
3475 return -EAGAIN;
3476
Benny Halevyd20581a2011-05-22 19:52:03 +03003477 return data->read_done_cb ? data->read_done_cb(task, data) :
3478 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003479}
3480
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003481static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003484 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003485 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003486 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
3488
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003489static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3490{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003491 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3492 &data->args.seq_args,
3493 &data->res.seq_res,
3494 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
Fred Isamanb029bc92011-03-03 15:13:42 +00003497static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
Fred Isamancd841602012-04-20 14:47:44 -04003499 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003501 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003502 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003503 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003505 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003507 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003508 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Fred Isamanb029bc92011-03-03 15:13:42 +00003512static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3513{
3514 if (!nfs4_sequence_done(task, &data->res.seq_res))
3515 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003516 return data->write_done_cb ? data->write_done_cb(task, data) :
3517 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003518}
3519
Trond Myklebust5a37f852012-04-28 14:55:16 -04003520static
3521bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003522{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003523 const struct nfs_pgio_header *hdr = data->header;
3524
3525 /* Don't request attributes for pNFS or O_DIRECT writes */
3526 if (data->ds_clp != NULL || hdr->dreq != NULL)
3527 return false;
3528 /* Otherwise, request attributes if and only if we don't hold
3529 * a delegation
3530 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003531 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003532}
Fred Isamana69aef12011-03-03 15:13:47 +00003533
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003534static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
Fred Isamancd841602012-04-20 14:47:44 -04003536 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003537
Trond Myklebust5a37f852012-04-28 14:55:16 -04003538 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003539 data->args.bitmask = NULL;
3540 data->res.fattr = NULL;
3541 } else
3542 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003543
Fred Isamanb029bc92011-03-03 15:13:42 +00003544 if (!data->write_done_cb)
3545 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003546 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 data->timestamp = jiffies;
3548
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003549 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003550 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551}
3552
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003553static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3554{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003555 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3556 &data->args.seq_args,
3557 &data->res.seq_res,
3558 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559}
3560
Fred Isaman0b7c0152012-04-20 14:47:39 -04003561static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3562{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003563 nfs4_setup_sequence(NFS_SERVER(data->inode),
3564 &data->args.seq_args,
3565 &data->res.seq_res,
3566 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567}
3568
Fred Isaman0b7c0152012-04-20 14:47:39 -04003569static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003572
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003573 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003574 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003575 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578}
3579
Fred Isaman0b7c0152012-04-20 14:47:39 -04003580static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003581{
3582 if (!nfs4_sequence_done(task, &data->res.seq_res))
3583 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003584 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003585}
3586
Fred Isaman0b7c0152012-04-20 14:47:39 -04003587static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003589 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003590
Fred Isaman0b7c0152012-04-20 14:47:39 -04003591 if (data->commit_done_cb == NULL)
3592 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003593 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003594 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003595 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596}
3597
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003598struct nfs4_renewdata {
3599 struct nfs_client *client;
3600 unsigned long timestamp;
3601};
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603/*
3604 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3605 * standalone procedure for queueing an asynchronous RENEW.
3606 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003607static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003608{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003609 struct nfs4_renewdata *data = calldata;
3610 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003611
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003612 if (atomic_read(&clp->cl_count) > 1)
3613 nfs4_schedule_state_renewal(clp);
3614 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003615 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003616}
3617
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003618static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003620 struct nfs4_renewdata *data = calldata;
3621 struct nfs_client *clp = data->client;
3622 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
3624 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003625 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003626 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3627 return;
3628 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003629 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003630 return;
3631 }
3632 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003634 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635}
3636
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003637static const struct rpc_call_ops nfs4_renew_ops = {
3638 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003639 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003640};
3641
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003642static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643{
3644 struct rpc_message msg = {
3645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3646 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003647 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003649 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003651 if (renew_flags == 0)
3652 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003653 if (!atomic_inc_not_zero(&clp->cl_count))
3654 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003655 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003656 if (data == NULL)
3657 return -ENOMEM;
3658 data->client = clp;
3659 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003661 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003664static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
3666 struct rpc_message msg = {
3667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3668 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003669 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 };
3671 unsigned long now = jiffies;
3672 int status;
3673
3674 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3675 if (status < 0)
3676 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003677 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return 0;
3679}
3680
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003681static inline int nfs4_server_supports_acls(struct nfs_server *server)
3682{
3683 return (server->caps & NFS_CAP_ACLS)
3684 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3685 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3686}
3687
Trond Myklebust21f498c2012-08-24 10:59:25 -04003688/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3689 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003690 * the stack.
3691 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003692#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003693
Neil Hormane9e3d722011-03-04 19:26:03 -05003694static int buf_to_pages_noslab(const void *buf, size_t buflen,
3695 struct page **pages, unsigned int *pgbase)
3696{
3697 struct page *newpage, **spages;
3698 int rc = 0;
3699 size_t len;
3700 spages = pages;
3701
3702 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003703 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003704 newpage = alloc_page(GFP_KERNEL);
3705
3706 if (newpage == NULL)
3707 goto unwind;
3708 memcpy(page_address(newpage), buf, len);
3709 buf += len;
3710 buflen -= len;
3711 *pages++ = newpage;
3712 rc++;
3713 } while (buflen != 0);
3714
3715 return rc;
3716
3717unwind:
3718 for(; rc > 0; rc--)
3719 __free_page(spages[rc-1]);
3720 return -ENOMEM;
3721}
3722
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003723struct nfs4_cached_acl {
3724 int cached;
3725 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003726 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003727};
3728
3729static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003730{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003731 struct nfs_inode *nfsi = NFS_I(inode);
3732
3733 spin_lock(&inode->i_lock);
3734 kfree(nfsi->nfs4_acl);
3735 nfsi->nfs4_acl = acl;
3736 spin_unlock(&inode->i_lock);
3737}
3738
3739static void nfs4_zap_acl_attr(struct inode *inode)
3740{
3741 nfs4_set_cached_acl(inode, NULL);
3742}
3743
3744static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3745{
3746 struct nfs_inode *nfsi = NFS_I(inode);
3747 struct nfs4_cached_acl *acl;
3748 int ret = -ENOENT;
3749
3750 spin_lock(&inode->i_lock);
3751 acl = nfsi->nfs4_acl;
3752 if (acl == NULL)
3753 goto out;
3754 if (buf == NULL) /* user is just asking for length */
3755 goto out_len;
3756 if (acl->cached == 0)
3757 goto out;
3758 ret = -ERANGE; /* see getxattr(2) man page */
3759 if (acl->len > buflen)
3760 goto out;
3761 memcpy(buf, acl->data, acl->len);
3762out_len:
3763 ret = acl->len;
3764out:
3765 spin_unlock(&inode->i_lock);
3766 return ret;
3767}
3768
Sachin Prabhu5794d212012-04-17 14:36:40 +01003769static 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 +00003770{
3771 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003772 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003773
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003774 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003775 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003776 if (acl == NULL)
3777 goto out;
3778 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003779 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003780 } else {
3781 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3782 if (acl == NULL)
3783 goto out;
3784 acl->cached = 0;
3785 }
3786 acl->len = acl_len;
3787out:
3788 nfs4_set_cached_acl(inode, acl);
3789}
3790
Andy Adamsonbf118a32011-12-07 11:55:27 -05003791/*
3792 * The getxattr API returns the required buffer length when called with a
3793 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3794 * the required buf. On a NULL buf, we send a page of data to the server
3795 * guessing that the ACL request can be serviced by a page. If so, we cache
3796 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3797 * the cache. If not so, we throw away the page, and cache the required
3798 * length. The next getxattr call will then produce another round trip to
3799 * the server, this time with the input buf of the required size.
3800 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003801static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003802{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003803 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003804 struct nfs_getaclargs args = {
3805 .fh = NFS_FH(inode),
3806 .acl_pages = pages,
3807 .acl_len = buflen,
3808 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003809 struct nfs_getaclres res = {
3810 .acl_len = buflen,
3811 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003812 struct rpc_message msg = {
3813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3814 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003815 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003816 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003817 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3818 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003819
Andy Adamsonbf118a32011-12-07 11:55:27 -05003820 /* As long as we're doing a round trip to the server anyway,
3821 * let's be prepared for a page of acl data. */
3822 if (npages == 0)
3823 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003824 if (npages > ARRAY_SIZE(pages))
3825 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003826
Andy Adamsonbf118a32011-12-07 11:55:27 -05003827 for (i = 0; i < npages; i++) {
3828 pages[i] = alloc_page(GFP_KERNEL);
3829 if (!pages[i])
3830 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003831 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003832
3833 /* for decoding across pages */
3834 res.acl_scratch = alloc_page(GFP_KERNEL);
3835 if (!res.acl_scratch)
3836 goto out_free;
3837
Andy Adamsonbf118a32011-12-07 11:55:27 -05003838 args.acl_len = npages * PAGE_SIZE;
3839 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003840
Peng Taode040be2012-01-10 22:42:47 +08003841 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003842 __func__, buf, buflen, npages, args.acl_len);
3843 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3844 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003845 if (ret)
3846 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003847
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003848 /* Handle the case where the passed-in buffer is too short */
3849 if (res.acl_flags & NFS4_ACL_TRUNC) {
3850 /* Did the user only issue a request for the acl length? */
3851 if (buf == NULL)
3852 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003853 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003854 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003855 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003856 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003857 if (buf) {
3858 if (res.acl_len > buflen) {
3859 ret = -ERANGE;
3860 goto out_free;
3861 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003862 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003863 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003864out_ok:
3865 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003866out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003867 for (i = 0; i < npages; i++)
3868 if (pages[i])
3869 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003870 if (res.acl_scratch)
3871 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003872 return ret;
3873}
3874
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003875static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3876{
3877 struct nfs4_exception exception = { };
3878 ssize_t ret;
3879 do {
3880 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3881 if (ret >= 0)
3882 break;
3883 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3884 } while (exception.retry);
3885 return ret;
3886}
3887
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003888static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3889{
3890 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003891 int ret;
3892
3893 if (!nfs4_server_supports_acls(server))
3894 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003895 ret = nfs_revalidate_inode(server, inode);
3896 if (ret < 0)
3897 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003898 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3899 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003900 ret = nfs4_read_cached_acl(inode, buf, buflen);
3901 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003902 /* -ENOENT is returned if there is no ACL or if there is an ACL
3903 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003904 return ret;
3905 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003906}
3907
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003908static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003909{
3910 struct nfs_server *server = NFS_SERVER(inode);
3911 struct page *pages[NFS4ACL_MAXPAGES];
3912 struct nfs_setaclargs arg = {
3913 .fh = NFS_FH(inode),
3914 .acl_pages = pages,
3915 .acl_len = buflen,
3916 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003917 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003918 struct rpc_message msg = {
3919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3920 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003921 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003922 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003923 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003924 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003925
3926 if (!nfs4_server_supports_acls(server))
3927 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003928 if (npages > ARRAY_SIZE(pages))
3929 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003930 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3931 if (i < 0)
3932 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003933 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003934 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003935
3936 /*
3937 * Free each page after tx, so the only ref left is
3938 * held by the network stack
3939 */
3940 for (; i > 0; i--)
3941 put_page(pages[i-1]);
3942
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003943 /*
3944 * Acl update can result in inode attribute update.
3945 * so mark the attribute cache invalid.
3946 */
3947 spin_lock(&inode->i_lock);
3948 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3949 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003950 nfs_access_zap_cache(inode);
3951 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003952 return ret;
3953}
3954
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003955static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3956{
3957 struct nfs4_exception exception = { };
3958 int err;
3959 do {
3960 err = nfs4_handle_exception(NFS_SERVER(inode),
3961 __nfs4_proc_set_acl(inode, buf, buflen),
3962 &exception);
3963 } while (exception.retry);
3964 return err;
3965}
3966
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003968nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003970 struct nfs_client *clp = server->nfs_client;
3971
3972 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return 0;
3974 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003975 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003976 case -NFS4ERR_ADMIN_REVOKED:
3977 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003978 if (state == NULL)
3979 break;
3980 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003981 case -NFS4ERR_OPENMODE:
3982 if (state == NULL)
3983 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003984 nfs4_schedule_stateid_recovery(server, state);
3985 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003986 case -NFS4ERR_EXPIRED:
3987 if (state != NULL)
3988 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003990 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003991 nfs4_schedule_lease_recovery(clp);
3992 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003993#if defined(CONFIG_NFS_V4_1)
3994 case -NFS4ERR_BADSESSION:
3995 case -NFS4ERR_BADSLOT:
3996 case -NFS4ERR_BAD_HIGH_SLOT:
3997 case -NFS4ERR_DEADSESSION:
3998 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3999 case -NFS4ERR_SEQ_FALSE_RETRY:
4000 case -NFS4ERR_SEQ_MISORDERED:
4001 dprintk("%s ERROR %d, Reset session\n", __func__,
4002 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004003 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004004 task->tk_status = 0;
4005 return -EAGAIN;
4006#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004008 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004009 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4011 task->tk_status = 0;
4012 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004013 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 case -NFS4ERR_OLD_STATEID:
4015 task->tk_status = 0;
4016 return -EAGAIN;
4017 }
4018 task->tk_status = nfs4_map_errors(task->tk_status);
4019 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004020wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004021 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004022 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4023 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4024 task->tk_status = 0;
4025 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026}
4027
Chuck Leverf0920752012-05-21 22:45:41 -04004028static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4029 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004030{
4031 __be32 verf[2];
4032
Chuck Lever2c820d92012-05-21 22:45:33 -04004033 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4034 /* An impossible timestamp guarantees this value
4035 * will never match a generated boot time. */
4036 verf[0] = 0;
4037 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4038 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004039 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4040 verf[0] = (__be32)nn->boot_time.tv_sec;
4041 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004042 }
Chuck Levercd937102012-03-02 17:14:31 -05004043 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4044}
4045
Chuck Levere984a552012-09-14 17:24:21 -04004046static unsigned int
4047nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4048 char *buf, size_t len)
4049{
4050 unsigned int result;
4051
4052 rcu_read_lock();
4053 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4054 clp->cl_ipaddr,
4055 rpc_peeraddr2str(clp->cl_rpcclient,
4056 RPC_DISPLAY_ADDR),
4057 rpc_peeraddr2str(clp->cl_rpcclient,
4058 RPC_DISPLAY_PROTO));
4059 rcu_read_unlock();
4060 return result;
4061}
4062
4063static unsigned int
4064nfs4_init_uniform_client_string(const struct nfs_client *clp,
4065 char *buf, size_t len)
4066{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004067 char *nodename = clp->cl_rpcclient->cl_nodename;
4068
4069 if (nfs4_client_id_uniquifier[0] != '\0')
4070 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004071 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4072 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004073 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004074}
4075
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004076/**
4077 * nfs4_proc_setclientid - Negotiate client ID
4078 * @clp: state data structure
4079 * @program: RPC program for NFSv4 callback service
4080 * @port: IP port number for NFS4 callback service
4081 * @cred: RPC credential to use for this call
4082 * @res: where to place the result
4083 *
4084 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4085 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004086int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4087 unsigned short port, struct rpc_cred *cred,
4088 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089{
4090 nfs4_verifier sc_verifier;
4091 struct nfs4_setclientid setclientid = {
4092 .sc_verifier = &sc_verifier,
4093 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004094 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 };
4096 struct rpc_message msg = {
4097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4098 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004099 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004100 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004102 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
Chuck Leverde734832012-07-11 16:30:50 -04004104 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004105 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004106 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4107 setclientid.sc_name_len =
4108 nfs4_init_uniform_client_string(clp,
4109 setclientid.sc_name,
4110 sizeof(setclientid.sc_name));
4111 else
4112 setclientid.sc_name_len =
4113 nfs4_init_nonuniform_client_string(clp,
4114 setclientid.sc_name,
4115 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004116 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004117 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004118 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004119 sizeof(setclientid.sc_netid),
4120 rpc_peeraddr2str(clp->cl_rpcclient,
4121 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004122 rcu_read_unlock();
4123 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004124 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 clp->cl_ipaddr, port >> 8, port & 255);
4126
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004127 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4128 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4129 setclientid.sc_name_len, setclientid.sc_name);
4130 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4131 dprintk("NFS reply setclientid: %d\n", status);
4132 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004135/**
4136 * nfs4_proc_setclientid_confirm - Confirm client ID
4137 * @clp: state data structure
4138 * @res: result of a previous SETCLIENTID
4139 * @cred: RPC credential to use for this call
4140 *
4141 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4142 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004143int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004144 struct nfs4_setclientid_res *arg,
4145 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
4147 struct nfs_fsinfo fsinfo;
4148 struct rpc_message msg = {
4149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004150 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004152 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 };
4154 unsigned long now;
4155 int status;
4156
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004157 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4158 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4159 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004161 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 if (status == 0) {
4163 spin_lock(&clp->cl_lock);
4164 clp->cl_lease_time = fsinfo.lease_time * HZ;
4165 clp->cl_last_renewal = now;
4166 spin_unlock(&clp->cl_lock);
4167 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004168 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 return status;
4170}
4171
Trond Myklebustfe650402006-01-03 09:55:18 +01004172struct nfs4_delegreturndata {
4173 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004174 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004175 struct nfs_fh fh;
4176 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004177 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004178 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004179 int rpc_status;
4180};
4181
Trond Myklebustfe650402006-01-03 09:55:18 +01004182static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4183{
4184 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004185
Trond Myklebust14516c32010-07-31 14:29:06 -04004186 if (!nfs4_sequence_done(task, &data->res.seq_res))
4187 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004188
Ricardo Labiaga79708862009-12-07 09:23:21 -05004189 switch (task->tk_status) {
4190 case -NFS4ERR_STALE_STATEID:
4191 case -NFS4ERR_EXPIRED:
4192 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004193 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004194 break;
4195 default:
4196 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4197 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004198 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004199 return;
4200 }
4201 }
4202 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004203}
4204
4205static void nfs4_delegreturn_release(void *calldata)
4206{
Trond Myklebustfe650402006-01-03 09:55:18 +01004207 kfree(calldata);
4208}
4209
Andy Adamson938e1012009-04-01 09:22:28 -04004210#if defined(CONFIG_NFS_V4_1)
4211static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4212{
4213 struct nfs4_delegreturndata *d_data;
4214
4215 d_data = (struct nfs4_delegreturndata *)data;
4216
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004217 nfs4_setup_sequence(d_data->res.server,
4218 &d_data->args.seq_args,
4219 &d_data->res.seq_res,
4220 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004221}
4222#endif /* CONFIG_NFS_V4_1 */
4223
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004224static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004225#if defined(CONFIG_NFS_V4_1)
4226 .rpc_call_prepare = nfs4_delegreturn_prepare,
4227#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004228 .rpc_call_done = nfs4_delegreturn_done,
4229 .rpc_release = nfs4_delegreturn_release,
4230};
4231
Trond Myklebuste6f81072008-01-24 18:14:34 -05004232static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004233{
4234 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004235 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004236 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004237 struct rpc_message msg = {
4238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4239 .rpc_cred = cred,
4240 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004241 struct rpc_task_setup task_setup_data = {
4242 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004243 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004244 .callback_ops = &nfs4_delegreturn_ops,
4245 .flags = RPC_TASK_ASYNC,
4246 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004247 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004248
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004249 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004250 if (data == NULL)
4251 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004252 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004253 data->args.fhandle = &data->fh;
4254 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004255 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004256 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004257 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004258 data->res.fattr = &data->fattr;
4259 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004260 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004261 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004262 data->rpc_status = 0;
4263
Trond Myklebustc970aa82007-07-14 15:39:59 -04004264 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004265 msg.rpc_argp = &data->args;
4266 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004267 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004268 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004269 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004270 if (!issync)
4271 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004272 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004273 if (status != 0)
4274 goto out;
4275 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004276 if (status == 0)
4277 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4278 else
4279 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004280out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004281 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004282 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283}
4284
Trond Myklebuste6f81072008-01-24 18:14:34 -05004285int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
4287 struct nfs_server *server = NFS_SERVER(inode);
4288 struct nfs4_exception exception = { };
4289 int err;
4290 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004291 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 switch (err) {
4293 case -NFS4ERR_STALE_STATEID:
4294 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 case 0:
4296 return 0;
4297 }
4298 err = nfs4_handle_exception(server, err, &exception);
4299 } while (exception.retry);
4300 return err;
4301}
4302
4303#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4304#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4305
4306/*
4307 * sleep, with exponential backoff, and retry the LOCK operation.
4308 */
4309static unsigned long
4310nfs4_set_lock_task_retry(unsigned long timeout)
4311{
Jeff Laytond3103102011-12-01 22:44:39 +01004312 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 timeout <<= 1;
4314 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4315 return NFS4_LOCK_MAXTIMEOUT;
4316 return timeout;
4317}
4318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4320{
4321 struct inode *inode = state->inode;
4322 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004323 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004324 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004326 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004328 struct nfs_lockt_res res = {
4329 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 };
4331 struct rpc_message msg = {
4332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4333 .rpc_argp = &arg,
4334 .rpc_resp = &res,
4335 .rpc_cred = state->owner->so_cred,
4336 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 struct nfs4_lock_state *lsp;
4338 int status;
4339
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004340 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004341 status = nfs4_set_lock_state(state, request);
4342 if (status != 0)
4343 goto out;
4344 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004345 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004346 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004347 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004348 switch (status) {
4349 case 0:
4350 request->fl_type = F_UNLCK;
4351 break;
4352 case -NFS4ERR_DENIED:
4353 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004355 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004356out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 return status;
4358}
4359
4360static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4361{
4362 struct nfs4_exception exception = { };
4363 int err;
4364
4365 do {
4366 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4367 _nfs4_proc_getlk(state, cmd, request),
4368 &exception);
4369 } while (exception.retry);
4370 return err;
4371}
4372
4373static int do_vfs_lock(struct file *file, struct file_lock *fl)
4374{
4375 int res = 0;
4376 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4377 case FL_POSIX:
4378 res = posix_lock_file_wait(file, fl);
4379 break;
4380 case FL_FLOCK:
4381 res = flock_lock_file_wait(file, fl);
4382 break;
4383 default:
4384 BUG();
4385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 return res;
4387}
4388
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004389struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004390 struct nfs_locku_args arg;
4391 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004392 struct nfs4_lock_state *lsp;
4393 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004394 struct file_lock fl;
4395 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004396 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004397};
4398
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004399static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4400 struct nfs_open_context *ctx,
4401 struct nfs4_lock_state *lsp,
4402 struct nfs_seqid *seqid)
4403{
4404 struct nfs4_unlockdata *p;
4405 struct inode *inode = lsp->ls_state->inode;
4406
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004407 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004408 if (p == NULL)
4409 return NULL;
4410 p->arg.fh = NFS_FH(inode);
4411 p->arg.fl = &p->fl;
4412 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004413 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004414 p->arg.stateid = &lsp->ls_stateid;
4415 p->lsp = lsp;
4416 atomic_inc(&lsp->ls_count);
4417 /* Ensure we don't close file until we're done freeing locks! */
4418 p->ctx = get_nfs_open_context(ctx);
4419 memcpy(&p->fl, fl, sizeof(p->fl));
4420 p->server = NFS_SERVER(inode);
4421 return p;
4422}
4423
Trond Myklebust06f814a2006-01-03 09:55:07 +01004424static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004425{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004426 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004427 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004428 nfs4_put_lock_state(calldata->lsp);
4429 put_nfs_open_context(calldata->ctx);
4430 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004431}
4432
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004433static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004434{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004435 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004436
Trond Myklebust14516c32010-07-31 14:29:06 -04004437 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4438 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004439 switch (task->tk_status) {
4440 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004441 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4442 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004443 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004444 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004445 case -NFS4ERR_BAD_STATEID:
4446 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004447 case -NFS4ERR_STALE_STATEID:
4448 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004449 break;
4450 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004451 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004452 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004453 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004454 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004455}
4456
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004457static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004458{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004459 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004461 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004462 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004463 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004464 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004465 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004466 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004467 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004468 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004469 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004470 &calldata->res.seq_res,
4471 task) != 0)
4472 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004473 return;
4474out_no_action:
4475 task->tk_action = NULL;
4476out_wait:
4477 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478}
4479
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004480static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004481 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004482 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004483 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004484};
4485
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004486static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4487 struct nfs_open_context *ctx,
4488 struct nfs4_lock_state *lsp,
4489 struct nfs_seqid *seqid)
4490{
4491 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004492 struct rpc_message msg = {
4493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4494 .rpc_cred = ctx->cred,
4495 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004496 struct rpc_task_setup task_setup_data = {
4497 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004498 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004499 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004500 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004501 .flags = RPC_TASK_ASYNC,
4502 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004503
Frank Filz137d6ac2007-07-09 15:32:29 -07004504 /* Ensure this is an unlock - when canceling a lock, the
4505 * canceled lock is passed in, and it won't be an unlock.
4506 */
4507 fl->fl_type = F_UNLCK;
4508
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4510 if (data == NULL) {
4511 nfs_free_seqid(seqid);
4512 return ERR_PTR(-ENOMEM);
4513 }
4514
Trond Myklebust9d12b212012-01-17 22:04:25 -05004515 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004516 msg.rpc_argp = &data->arg;
4517 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004518 task_setup_data.callback_data = data;
4519 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520}
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4523{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004524 struct inode *inode = state->inode;
4525 struct nfs4_state_owner *sp = state->owner;
4526 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004527 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004528 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004529 struct rpc_task *task;
4530 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004531 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Trond Myklebust9b073572006-06-29 16:38:34 -04004533 status = nfs4_set_lock_state(state, request);
4534 /* Unlock _before_ we do the RPC call */
4535 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004536 /* Exclude nfs_delegation_claim_locks() */
4537 mutex_lock(&sp->so_delegreturn_mutex);
4538 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004539 down_read(&nfsi->rwsem);
4540 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4541 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004542 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004543 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004544 }
4545 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004546 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004547 if (status != 0)
4548 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004549 /* Is this a delegated lock? */
4550 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004551 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004552 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004553 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004554 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004555 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004556 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004557 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004558 status = PTR_ERR(task);
4559 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004560 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004561 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004562 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004563out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004564 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004565 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566}
4567
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004568struct nfs4_lockdata {
4569 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004570 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004571 struct nfs4_lock_state *lsp;
4572 struct nfs_open_context *ctx;
4573 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004574 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004575 int rpc_status;
4576 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004577 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004578};
4579
4580static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004581 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4582 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004583{
4584 struct nfs4_lockdata *p;
4585 struct inode *inode = lsp->ls_state->inode;
4586 struct nfs_server *server = NFS_SERVER(inode);
4587
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004588 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004589 if (p == NULL)
4590 return NULL;
4591
4592 p->arg.fh = NFS_FH(inode);
4593 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004594 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004595 if (p->arg.open_seqid == NULL)
4596 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004597 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004598 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004599 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004600 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004601 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004602 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004603 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004604 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004605 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004606 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004607 atomic_inc(&lsp->ls_count);
4608 p->ctx = get_nfs_open_context(ctx);
4609 memcpy(&p->fl, fl, sizeof(p->fl));
4610 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004611out_free_seqid:
4612 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004613out_free:
4614 kfree(p);
4615 return NULL;
4616}
4617
4618static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4619{
4620 struct nfs4_lockdata *data = calldata;
4621 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Harvey Harrison3110ff82008-05-02 13:42:44 -07004623 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004624 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004625 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004626 /* Do we need to do an open_to_lock_owner? */
4627 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004628 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004629 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004630 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004631 data->arg.open_stateid = &state->stateid;
4632 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004633 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004634 } else
4635 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004636 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004637 if (nfs4_setup_sequence(data->server,
4638 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004639 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004640 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004641 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004642 nfs_release_seqid(data->arg.open_seqid);
4643out_release_lock_seqid:
4644 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004645out_wait:
4646 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004647 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004648}
4649
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004650static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4651{
4652 struct nfs4_lockdata *data = calldata;
4653
Harvey Harrison3110ff82008-05-02 13:42:44 -07004654 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004655
Trond Myklebust14516c32010-07-31 14:29:06 -04004656 if (!nfs4_sequence_done(task, &data->res.seq_res))
4657 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004658
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004659 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004660 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004661 if (data->rpc_status == 0)
4662 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4663 else
4664 goto out;
4665 }
4666 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004667 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004668 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004669 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004670 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004671out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004672 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004673}
4674
4675static void nfs4_lock_release(void *calldata)
4676{
4677 struct nfs4_lockdata *data = calldata;
4678
Harvey Harrison3110ff82008-05-02 13:42:44 -07004679 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004680 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004681 if (data->cancelled != 0) {
4682 struct rpc_task *task;
4683 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4684 data->arg.lock_seqid);
4685 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004686 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004687 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004688 } else
4689 nfs_free_seqid(data->arg.lock_seqid);
4690 nfs4_put_lock_state(data->lsp);
4691 put_nfs_open_context(data->ctx);
4692 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004693 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004694}
4695
4696static const struct rpc_call_ops nfs4_lock_ops = {
4697 .rpc_call_prepare = nfs4_lock_prepare,
4698 .rpc_call_done = nfs4_lock_done,
4699 .rpc_release = nfs4_lock_release,
4700};
4701
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004702static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4703{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004704 switch (error) {
4705 case -NFS4ERR_ADMIN_REVOKED:
4706 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004707 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004708 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004709 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004710 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004711 break;
4712 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004713 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004714 case -NFS4ERR_EXPIRED:
4715 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004716 };
4717}
4718
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004719static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004720{
4721 struct nfs4_lockdata *data;
4722 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004723 struct rpc_message msg = {
4724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4725 .rpc_cred = state->owner->so_cred,
4726 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004727 struct rpc_task_setup task_setup_data = {
4728 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004729 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004730 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004731 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004732 .flags = RPC_TASK_ASYNC,
4733 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004734 int ret;
4735
Harvey Harrison3110ff82008-05-02 13:42:44 -07004736 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004737 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004738 fl->fl_u.nfs4_fl.owner,
4739 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004740 if (data == NULL)
4741 return -ENOMEM;
4742 if (IS_SETLKW(cmd))
4743 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004744 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004745 msg.rpc_argp = &data->arg;
4746 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004747 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004748 if (recovery_type > NFS_LOCK_NEW) {
4749 if (recovery_type == NFS_LOCK_RECLAIM)
4750 data->arg.reclaim = NFS_LOCK_RECLAIM;
4751 nfs4_set_sequence_privileged(&data->arg.seq_args);
4752 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004753 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004754 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004755 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004756 ret = nfs4_wait_for_completion_rpc_task(task);
4757 if (ret == 0) {
4758 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004759 if (ret)
4760 nfs4_handle_setlk_error(data->server, data->lsp,
4761 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004762 } else
4763 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004764 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004765 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004766 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767}
4768
4769static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4770{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004771 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004772 struct nfs4_exception exception = {
4773 .inode = state->inode,
4774 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004775 int err;
4776
4777 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004778 /* Cache the lock if possible... */
4779 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4780 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004781 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004782 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004783 break;
4784 nfs4_handle_exception(server, err, &exception);
4785 } while (exception.retry);
4786 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787}
4788
4789static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4790{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004791 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004792 struct nfs4_exception exception = {
4793 .inode = state->inode,
4794 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004795 int err;
4796
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004797 err = nfs4_set_lock_state(state, request);
4798 if (err != 0)
4799 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004800 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004801 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4802 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004803 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004804 switch (err) {
4805 default:
4806 goto out;
4807 case -NFS4ERR_GRACE:
4808 case -NFS4ERR_DELAY:
4809 nfs4_handle_exception(server, err, &exception);
4810 err = 0;
4811 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004812 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004813out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004814 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815}
4816
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004817#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004818/**
4819 * nfs41_check_expired_locks - possibly free a lock stateid
4820 *
4821 * @state: NFSv4 state for an inode
4822 *
4823 * Returns NFS_OK if recovery for this stateid is now finished.
4824 * Otherwise a negative NFS4ERR value is returned.
4825 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004826static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004827{
Chuck Levereb64cf92012-07-11 16:30:05 -04004828 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004829 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004830 struct nfs_server *server = NFS_SERVER(state->inode);
4831
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004832 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004833 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004834 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4835 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004836 /* Free the stateid unless the server
4837 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004838 if (status != -NFS4ERR_BAD_STATEID)
4839 nfs41_free_stateid(server,
4840 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004841 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004842 ret = status;
4843 }
4844 }
4845 };
4846
4847 return ret;
4848}
4849
4850static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4851{
4852 int status = NFS_OK;
4853
4854 if (test_bit(LK_STATE_IN_USE, &state->flags))
4855 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004856 if (status != NFS_OK)
4857 status = nfs4_lock_expired(state, request);
4858 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004859}
4860#endif
4861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4863{
Trond Myklebust9a99af492013-02-04 20:17:49 -05004864 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004865 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004866 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004867 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004868 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004870 if ((fl_flags & FL_POSIX) &&
4871 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4872 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004873 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004874 status = nfs4_set_lock_state(state, request);
4875 if (status != 0)
4876 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004877 request->fl_flags |= FL_ACCESS;
4878 status = do_vfs_lock(request->fl_file, request);
4879 if (status < 0)
4880 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004881 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004882 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004883 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004884 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004885 request->fl_flags = fl_flags & ~FL_SLEEP;
4886 status = do_vfs_lock(request->fl_file, request);
4887 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004888 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05004889 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4890 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004891 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004892 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05004893 goto out;
4894 down_read(&nfsi->rwsem);
4895 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4896 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004897 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004898 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004899 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004900 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004901 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004902 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4903 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004904out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004905 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004906out:
4907 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 return status;
4909}
4910
4911static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4912{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004913 struct nfs4_exception exception = {
4914 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004915 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004916 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 int err;
4918
4919 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004920 err = _nfs4_proc_setlk(state, cmd, request);
4921 if (err == -NFS4ERR_DENIED)
4922 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004924 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 } while (exception.retry);
4926 return err;
4927}
4928
4929static int
4930nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4931{
4932 struct nfs_open_context *ctx;
4933 struct nfs4_state *state;
4934 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4935 int status;
4936
4937 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004938 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 state = ctx->state;
4940
4941 if (request->fl_start < 0 || request->fl_end < 0)
4942 return -EINVAL;
4943
Trond Myklebustd9531262009-07-21 19:22:38 -04004944 if (IS_GETLK(cmd)) {
4945 if (state != NULL)
4946 return nfs4_proc_getlk(state, F_GETLK, request);
4947 return 0;
4948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
4950 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4951 return -EINVAL;
4952
Trond Myklebustd9531262009-07-21 19:22:38 -04004953 if (request->fl_type == F_UNLCK) {
4954 if (state != NULL)
4955 return nfs4_proc_unlck(state, cmd, request);
4956 return 0;
4957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Trond Myklebustd9531262009-07-21 19:22:38 -04004959 if (state == NULL)
4960 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004961 /*
4962 * Don't rely on the VFS having checked the file open mode,
4963 * since it won't do this for flock() locks.
4964 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004965 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004966 case F_RDLCK:
4967 if (!(filp->f_mode & FMODE_READ))
4968 return -EBADF;
4969 break;
4970 case F_WRLCK:
4971 if (!(filp->f_mode & FMODE_WRITE))
4972 return -EBADF;
4973 }
4974
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 do {
4976 status = nfs4_proc_setlk(state, cmd, request);
4977 if ((status != -EAGAIN) || IS_SETLK(cmd))
4978 break;
4979 timeout = nfs4_set_lock_task_retry(timeout);
4980 status = -ERESTARTSYS;
4981 if (signalled())
4982 break;
4983 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 return status;
4985}
4986
Trond Myklebust888e6942005-11-04 15:38:11 -05004987int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4988{
4989 struct nfs_server *server = NFS_SERVER(state->inode);
4990 struct nfs4_exception exception = { };
4991 int err;
4992
4993 err = nfs4_set_lock_state(state, fl);
4994 if (err != 0)
4995 goto out;
4996 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004997 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004998 switch (err) {
4999 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005000 printk(KERN_ERR "NFS: %s: unhandled error "
5001 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005002 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005003 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005004 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005005 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005006 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05005007 set_bit(NFS_DELEGATED_STATE, &state->flags);
5008 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005009 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05005010 err = -EAGAIN;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005011 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005012 case -NFS4ERR_BADSESSION:
5013 case -NFS4ERR_BADSLOT:
5014 case -NFS4ERR_BAD_HIGH_SLOT:
5015 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5016 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05005017 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04005018 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05005019 err = -EAGAIN;
Trond Myklebustd5122202009-06-17 13:22:58 -07005020 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005021 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005022 case -NFS4ERR_ADMIN_REVOKED:
5023 case -NFS4ERR_BAD_STATEID:
5024 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005025 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005026 err = 0;
5027 goto out;
5028 case -ENOMEM:
5029 case -NFS4ERR_DENIED:
5030 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5031 err = 0;
5032 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005033 }
Trond Myklebust37380e42013-02-05 12:35:16 -05005034 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust888e6942005-11-04 15:38:11 -05005035 err = nfs4_handle_exception(server, err, &exception);
5036 } while (exception.retry);
5037out:
5038 return err;
5039}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005040
Trond Myklebustcf470c32012-03-07 13:49:12 -05005041struct nfs_release_lockowner_data {
5042 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005043 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005044 struct nfs_release_lockowner_args args;
5045};
5046
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005047static void nfs4_release_lockowner_release(void *calldata)
5048{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005049 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005050 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005051 kfree(calldata);
5052}
5053
Trond Myklebust17280172012-03-11 13:11:00 -04005054static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005055 .rpc_release = nfs4_release_lockowner_release,
5056};
5057
Trond Myklebustcf470c32012-03-07 13:49:12 -05005058int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005059{
5060 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005061 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005062 struct rpc_message msg = {
5063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5064 };
5065
5066 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005067 return -EINVAL;
5068 data = kmalloc(sizeof(*data), GFP_NOFS);
5069 if (!data)
5070 return -ENOMEM;
5071 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005072 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005073 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5074 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5075 data->args.lock_owner.s_dev = server->s_dev;
5076 msg.rpc_argp = &data->args;
5077 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5078 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005079}
5080
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005081#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5082
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005083static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5084 const void *buf, size_t buflen,
5085 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005086{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005087 if (strcmp(key, "") != 0)
5088 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005089
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005090 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005091}
5092
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005093static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5094 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005095{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005096 if (strcmp(key, "") != 0)
5097 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005098
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005099 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005100}
5101
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005102static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5103 size_t list_len, const char *name,
5104 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005105{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005106 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005107
J. Bruce Fields096455a2006-03-20 23:23:42 -05005108 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5109 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005110
5111 if (list && len <= list_len)
5112 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005113 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005114}
5115
Andy Adamson533eb462011-06-13 18:25:56 -04005116/*
5117 * nfs_fhget will use either the mounted_on_fileid or the fileid
5118 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005119static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5120{
Andy Adamson533eb462011-06-13 18:25:56 -04005121 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5122 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5123 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005124 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005125 return;
5126
5127 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005128 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005129 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5130 fattr->nlink = 2;
5131}
5132
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005133static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5134 const struct qstr *name,
5135 struct nfs4_fs_locations *fs_locations,
5136 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005137{
5138 struct nfs_server *server = NFS_SERVER(dir);
5139 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005140 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005141 };
5142 struct nfs4_fs_locations_arg args = {
5143 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005144 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005145 .page = page,
5146 .bitmask = bitmask,
5147 };
Benny Halevy22958462009-04-01 09:22:02 -04005148 struct nfs4_fs_locations_res res = {
5149 .fs_locations = fs_locations,
5150 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005151 struct rpc_message msg = {
5152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5153 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005154 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005155 };
5156 int status;
5157
Harvey Harrison3110ff82008-05-02 13:42:44 -07005158 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005159
5160 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5161 * is not supported */
5162 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5163 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5164 else
5165 bitmask[0] |= FATTR4_WORD0_FILEID;
5166
Trond Myklebustc228fd32007-01-13 02:28:11 -05005167 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005168 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005169 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005170 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005171 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005172 return status;
5173}
5174
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005175int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5176 const struct qstr *name,
5177 struct nfs4_fs_locations *fs_locations,
5178 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005179{
5180 struct nfs4_exception exception = { };
5181 int err;
5182 do {
5183 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005184 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005185 &exception);
5186 } while (exception.retry);
5187 return err;
5188}
5189
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005190static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5191{
5192 int status;
5193 struct nfs4_secinfo_arg args = {
5194 .dir_fh = NFS_FH(dir),
5195 .name = name,
5196 };
5197 struct nfs4_secinfo_res res = {
5198 .flavors = flavors,
5199 };
5200 struct rpc_message msg = {
5201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5202 .rpc_argp = &args,
5203 .rpc_resp = &res,
5204 };
5205
5206 dprintk("NFS call secinfo %s\n", name->name);
5207 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5208 dprintk("NFS reply secinfo: %d\n", status);
5209 return status;
5210}
5211
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005212int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5213 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005214{
5215 struct nfs4_exception exception = { };
5216 int err;
5217 do {
5218 err = nfs4_handle_exception(NFS_SERVER(dir),
5219 _nfs4_proc_secinfo(dir, name, flavors),
5220 &exception);
5221 } while (exception.retry);
5222 return err;
5223}
5224
Andy Adamson557134a2009-04-01 09:21:53 -04005225#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005226/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005227 * Check the exchange flags returned by the server for invalid flags, having
5228 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5229 * DS flags set.
5230 */
5231static int nfs4_check_cl_exchange_flags(u32 flags)
5232{
5233 if (flags & ~EXCHGID4_FLAG_MASK_R)
5234 goto out_inval;
5235 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5236 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5237 goto out_inval;
5238 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5239 goto out_inval;
5240 return NFS_OK;
5241out_inval:
5242 return -NFS4ERR_INVAL;
5243}
5244
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005245static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005246nfs41_same_server_scope(struct nfs41_server_scope *a,
5247 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005248{
5249 if (a->server_scope_sz == b->server_scope_sz &&
5250 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5251 return true;
5252
5253 return false;
5254}
5255
Andy Adamson357f54d2010-12-14 10:11:57 -05005256/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005257 * nfs4_proc_bind_conn_to_session()
5258 *
5259 * The 4.1 client currently uses the same TCP connection for the
5260 * fore and backchannel.
5261 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005262int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005263{
5264 int status;
5265 struct nfs41_bind_conn_to_session_res res;
5266 struct rpc_message msg = {
5267 .rpc_proc =
5268 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5269 .rpc_argp = clp,
5270 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005271 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005272 };
5273
5274 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005275
5276 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5277 if (unlikely(res.session == NULL)) {
5278 status = -ENOMEM;
5279 goto out;
5280 }
5281
5282 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5283 if (status == 0) {
5284 if (memcmp(res.session->sess_id.data,
5285 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5286 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5287 status = -EIO;
5288 goto out_session;
5289 }
5290 if (res.dir != NFS4_CDFS4_BOTH) {
5291 dprintk("NFS: %s: Unexpected direction from server\n",
5292 __func__);
5293 status = -EIO;
5294 goto out_session;
5295 }
5296 if (res.use_conn_in_rdma_mode) {
5297 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5298 __func__);
5299 status = -EIO;
5300 goto out_session;
5301 }
5302 }
5303out_session:
5304 kfree(res.session);
5305out:
5306 dprintk("<-- %s status= %d\n", __func__, status);
5307 return status;
5308}
5309
5310/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005311 * nfs4_proc_exchange_id()
5312 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005313 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5314 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005315 * Since the clientid has expired, all compounds using sessions
5316 * associated with the stale clientid will be returning
5317 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5318 * be in some phase of session reset.
5319 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005320int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005321{
5322 nfs4_verifier verifier;
5323 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005324 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005325 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005326 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005327 };
5328 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005329 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005330 };
5331 int status;
5332 struct rpc_message msg = {
5333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5334 .rpc_argp = &args,
5335 .rpc_resp = &res,
5336 .rpc_cred = cred,
5337 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005338
Chuck Leverf0920752012-05-21 22:45:41 -04005339 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005340 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5341 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005342 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5343 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5344 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005345
Chuck Leveracdeb692012-05-21 22:46:16 -04005346 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005347 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005348 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005349 status = -ENOMEM;
5350 goto out;
5351 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005352
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005353 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005354 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005355 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005356 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005357 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005358 }
5359
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005360 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005361 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005362 status = -ENOMEM;
5363 goto out_server_scope;
5364 }
5365
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005366 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005367 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005368 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005369
Chuck Lever177313f2012-05-21 22:44:58 -04005370 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005371 clp->cl_clientid = res.clientid;
5372 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5373 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5374 clp->cl_seqid = res.seqid;
5375
Chuck Leveracdeb692012-05-21 22:46:16 -04005376 kfree(clp->cl_serverowner);
5377 clp->cl_serverowner = res.server_owner;
5378 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005379
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005380 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005381 kfree(clp->cl_implid);
5382 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005383
Chuck Lever177313f2012-05-21 22:44:58 -04005384 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005385 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005386 res.server_scope)) {
5387 dprintk("%s: server_scope mismatch detected\n",
5388 __func__);
5389 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005390 kfree(clp->cl_serverscope);
5391 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005392 }
5393
Chuck Lever177313f2012-05-21 22:44:58 -04005394 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005395 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005396 goto out;
5397 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005398 } else
5399 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005400
Chuck Leveracdeb692012-05-21 22:46:16 -04005401out_server_owner:
5402 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005403out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005404 kfree(res.server_scope);
5405out:
Chuck Lever177313f2012-05-21 22:44:58 -04005406 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005407 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005408 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005409 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005410 clp->cl_implid->date.seconds,
5411 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005412 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005413 return status;
5414}
5415
Trond Myklebust66245532012-05-25 17:18:09 -04005416static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5417 struct rpc_cred *cred)
5418{
5419 struct rpc_message msg = {
5420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5421 .rpc_argp = clp,
5422 .rpc_cred = cred,
5423 };
5424 int status;
5425
5426 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5427 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005428 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005429 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5430 return status;
5431}
5432
5433static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5434 struct rpc_cred *cred)
5435{
5436 unsigned int loop;
5437 int ret;
5438
5439 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5440 ret = _nfs4_proc_destroy_clientid(clp, cred);
5441 switch (ret) {
5442 case -NFS4ERR_DELAY:
5443 case -NFS4ERR_CLIENTID_BUSY:
5444 ssleep(1);
5445 break;
5446 default:
5447 return ret;
5448 }
5449 }
5450 return 0;
5451}
5452
5453int nfs4_destroy_clientid(struct nfs_client *clp)
5454{
5455 struct rpc_cred *cred;
5456 int ret = 0;
5457
5458 if (clp->cl_mvops->minor_version < 1)
5459 goto out;
5460 if (clp->cl_exchange_flags == 0)
5461 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005462 if (clp->cl_preserve_clid)
5463 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005464 cred = nfs4_get_exchange_id_cred(clp);
5465 ret = nfs4_proc_destroy_clientid(clp, cred);
5466 if (cred)
5467 put_rpccred(cred);
5468 switch (ret) {
5469 case 0:
5470 case -NFS4ERR_STALE_CLIENTID:
5471 clp->cl_exchange_flags = 0;
5472 }
5473out:
5474 return ret;
5475}
5476
Andy Adamson2050f0c2009-04-01 09:22:30 -04005477struct nfs4_get_lease_time_data {
5478 struct nfs4_get_lease_time_args *args;
5479 struct nfs4_get_lease_time_res *res;
5480 struct nfs_client *clp;
5481};
5482
5483static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5484 void *calldata)
5485{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005486 struct nfs4_get_lease_time_data *data =
5487 (struct nfs4_get_lease_time_data *)calldata;
5488
5489 dprintk("--> %s\n", __func__);
5490 /* just setup sequence, do not trigger session recovery
5491 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005492 nfs41_setup_sequence(data->clp->cl_session,
5493 &data->args->la_seq_args,
5494 &data->res->lr_seq_res,
5495 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005496 dprintk("<-- %s\n", __func__);
5497}
5498
5499/*
5500 * Called from nfs4_state_manager thread for session setup, so don't recover
5501 * from sequence operation or clientid errors.
5502 */
5503static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5504{
5505 struct nfs4_get_lease_time_data *data =
5506 (struct nfs4_get_lease_time_data *)calldata;
5507
5508 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005509 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5510 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005511 switch (task->tk_status) {
5512 case -NFS4ERR_DELAY:
5513 case -NFS4ERR_GRACE:
5514 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5515 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5516 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005517 /* fall through */
5518 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005519 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005520 return;
5521 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005522 dprintk("<-- %s\n", __func__);
5523}
5524
Trond Myklebust17280172012-03-11 13:11:00 -04005525static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005526 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5527 .rpc_call_done = nfs4_get_lease_time_done,
5528};
5529
5530int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5531{
5532 struct rpc_task *task;
5533 struct nfs4_get_lease_time_args args;
5534 struct nfs4_get_lease_time_res res = {
5535 .lr_fsinfo = fsinfo,
5536 };
5537 struct nfs4_get_lease_time_data data = {
5538 .args = &args,
5539 .res = &res,
5540 .clp = clp,
5541 };
5542 struct rpc_message msg = {
5543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5544 .rpc_argp = &args,
5545 .rpc_resp = &res,
5546 };
5547 struct rpc_task_setup task_setup = {
5548 .rpc_client = clp->cl_rpcclient,
5549 .rpc_message = &msg,
5550 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005551 .callback_data = &data,
5552 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005553 };
5554 int status;
5555
Trond Myklebust9d12b212012-01-17 22:04:25 -05005556 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005557 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005558 dprintk("--> %s\n", __func__);
5559 task = rpc_run_task(&task_setup);
5560
5561 if (IS_ERR(task))
5562 status = PTR_ERR(task);
5563 else {
5564 status = task->tk_status;
5565 rpc_put_task(task);
5566 }
5567 dprintk("<-- %s return %d\n", __func__, status);
5568
5569 return status;
5570}
5571
Andy Adamsonfc931582009-04-01 09:22:31 -04005572/*
5573 * Initialize the values to be used by the client in CREATE_SESSION
5574 * If nfs4_init_session set the fore channel request and response sizes,
5575 * use them.
5576 *
5577 * Set the back channel max_resp_sz_cached to zero to force the client to
5578 * always set csa_cachethis to FALSE because the current implementation
5579 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5580 */
5581static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5582{
5583 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005584 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5585 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005586
5587 if (mxrqst_sz == 0)
5588 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5589 if (mxresp_sz == 0)
5590 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5591 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005592 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5593 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005594 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005595 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005596
5597 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005598 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005599 __func__,
5600 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005601 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005602
5603 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005604 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5605 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5606 args->bc_attrs.max_resp_sz_cached = 0;
5607 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5608 args->bc_attrs.max_reqs = 1;
5609
5610 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5611 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5612 __func__,
5613 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5614 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5615 args->bc_attrs.max_reqs);
5616}
5617
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005618static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005619{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005620 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5621 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5622
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005623 if (rcvd->max_resp_sz > sent->max_resp_sz)
5624 return -EINVAL;
5625 /*
5626 * Our requested max_ops is the minimum we need; we're not
5627 * prepared to break up compounds into smaller pieces than that.
5628 * So, no point even trying to continue if the server won't
5629 * cooperate:
5630 */
5631 if (rcvd->max_ops < sent->max_ops)
5632 return -EINVAL;
5633 if (rcvd->max_reqs == 0)
5634 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005635 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5636 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005637 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005638}
5639
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005640static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5641{
5642 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5643 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005644
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005645 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5646 return -EINVAL;
5647 if (rcvd->max_resp_sz < sent->max_resp_sz)
5648 return -EINVAL;
5649 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5650 return -EINVAL;
5651 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005652 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005653 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005654 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005655 return -EINVAL;
5656 return 0;
5657}
Andy Adamson8d353012009-04-01 09:22:32 -04005658
Andy Adamson8d353012009-04-01 09:22:32 -04005659static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5660 struct nfs4_session *session)
5661{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005662 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005663
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005664 ret = nfs4_verify_fore_channel_attrs(args, session);
5665 if (ret)
5666 return ret;
5667 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005668}
5669
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005670static int _nfs4_proc_create_session(struct nfs_client *clp,
5671 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005672{
5673 struct nfs4_session *session = clp->cl_session;
5674 struct nfs41_create_session_args args = {
5675 .client = clp,
5676 .cb_program = NFS4_CALLBACK,
5677 };
5678 struct nfs41_create_session_res res = {
5679 .client = clp,
5680 };
5681 struct rpc_message msg = {
5682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5683 .rpc_argp = &args,
5684 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005685 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005686 };
5687 int status;
5688
5689 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005690 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005691
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005692 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005693
Trond Myklebust43095d32012-11-20 11:13:12 -05005694 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005695 /* Verify the session's negotiated channel_attrs values */
5696 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005697 /* Increment the clientid slot sequence id */
5698 clp->cl_seqid++;
5699 }
5700
5701 return status;
5702}
5703
5704/*
5705 * Issues a CREATE_SESSION operation to the server.
5706 * It is the responsibility of the caller to verify the session is
5707 * expired before calling this routine.
5708 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005709int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005710{
5711 int status;
5712 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005713 struct nfs4_session *session = clp->cl_session;
5714
5715 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5716
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005717 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005718 if (status)
5719 goto out;
5720
Andy Adamsonaacd5532011-11-09 13:58:21 -05005721 /* Init or reset the session slot tables */
5722 status = nfs4_setup_session_slot_tables(session);
5723 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005724 if (status)
5725 goto out;
5726
5727 ptr = (unsigned *)&session->sess_id.data[0];
5728 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5729 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005730out:
5731 dprintk("<-- %s\n", __func__);
5732 return status;
5733}
5734
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005735/*
5736 * Issue the over-the-wire RPC DESTROY_SESSION.
5737 * The caller must serialize access to this routine.
5738 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005739int nfs4_proc_destroy_session(struct nfs4_session *session,
5740 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005741{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005742 struct rpc_message msg = {
5743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5744 .rpc_argp = session,
5745 .rpc_cred = cred,
5746 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005747 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005748
5749 dprintk("--> nfs4_proc_destroy_session\n");
5750
5751 /* session is still being setup */
5752 if (session->clp->cl_cons_state != NFS_CS_READY)
5753 return status;
5754
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005755 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005756
5757 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005758 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005759 "Session has been destroyed regardless...\n", status);
5760
5761 dprintk("<-- nfs4_proc_destroy_session\n");
5762 return status;
5763}
5764
Trond Myklebust7b38c362012-05-23 13:23:31 -04005765/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005766 * Renew the cl_session lease.
5767 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005768struct nfs4_sequence_data {
5769 struct nfs_client *clp;
5770 struct nfs4_sequence_args args;
5771 struct nfs4_sequence_res res;
5772};
5773
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005774static void nfs41_sequence_release(void *data)
5775{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005776 struct nfs4_sequence_data *calldata = data;
5777 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005778
Alexandros Batsakis71358402010-02-05 03:45:05 -08005779 if (atomic_read(&clp->cl_count) > 1)
5780 nfs4_schedule_state_renewal(clp);
5781 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005782 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005783}
5784
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005785static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5786{
5787 switch(task->tk_status) {
5788 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005789 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5790 return -EAGAIN;
5791 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005792 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005793 }
5794 return 0;
5795}
5796
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005797static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005798{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005799 struct nfs4_sequence_data *calldata = data;
5800 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005801
Trond Myklebust14516c32010-07-31 14:29:06 -04005802 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5803 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005804
5805 if (task->tk_status < 0) {
5806 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005807 if (atomic_read(&clp->cl_count) == 1)
5808 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005809
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005810 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5811 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005812 return;
5813 }
5814 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005815 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005816out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005817 dprintk("<-- %s\n", __func__);
5818}
5819
5820static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5821{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005822 struct nfs4_sequence_data *calldata = data;
5823 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005824 struct nfs4_sequence_args *args;
5825 struct nfs4_sequence_res *res;
5826
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005827 args = task->tk_msg.rpc_argp;
5828 res = task->tk_msg.rpc_resp;
5829
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005830 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005831}
5832
5833static const struct rpc_call_ops nfs41_sequence_ops = {
5834 .rpc_call_done = nfs41_sequence_call_done,
5835 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005836 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005837};
5838
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005839static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5840 struct rpc_cred *cred,
5841 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005842{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005843 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005844 struct rpc_message msg = {
5845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5846 .rpc_cred = cred,
5847 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005848 struct rpc_task_setup task_setup_data = {
5849 .rpc_client = clp->cl_rpcclient,
5850 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005851 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005852 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5853 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005854
Alexandros Batsakis71358402010-02-05 03:45:05 -08005855 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005856 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005857 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005858 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005859 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005860 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005861 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005862 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005863 if (is_privileged)
5864 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005865 msg.rpc_argp = &calldata->args;
5866 msg.rpc_resp = &calldata->res;
5867 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005868 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005869
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005870 return rpc_run_task(&task_setup_data);
5871}
5872
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005873static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005874{
5875 struct rpc_task *task;
5876 int ret = 0;
5877
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005878 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5879 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005880 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005881 if (IS_ERR(task))
5882 ret = PTR_ERR(task);
5883 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005884 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005885 dprintk("<-- %s status=%d\n", __func__, ret);
5886 return ret;
5887}
5888
5889static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5890{
5891 struct rpc_task *task;
5892 int ret;
5893
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005894 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005895 if (IS_ERR(task)) {
5896 ret = PTR_ERR(task);
5897 goto out;
5898 }
5899 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005900 if (!ret) {
5901 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5902
5903 if (task->tk_status == 0)
5904 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005905 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005906 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005907 rpc_put_task(task);
5908out:
5909 dprintk("<-- %s status=%d\n", __func__, ret);
5910 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005911}
5912
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005913struct nfs4_reclaim_complete_data {
5914 struct nfs_client *clp;
5915 struct nfs41_reclaim_complete_args arg;
5916 struct nfs41_reclaim_complete_res res;
5917};
5918
5919static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5920{
5921 struct nfs4_reclaim_complete_data *calldata = data;
5922
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005923 nfs41_setup_sequence(calldata->clp->cl_session,
5924 &calldata->arg.seq_args,
5925 &calldata->res.seq_res,
5926 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005927}
5928
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005929static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5930{
5931 switch(task->tk_status) {
5932 case 0:
5933 case -NFS4ERR_COMPLETE_ALREADY:
5934 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5935 break;
5936 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005937 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005938 /* fall through */
5939 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005940 return -EAGAIN;
5941 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005942 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005943 }
5944 return 0;
5945}
5946
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005947static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5948{
5949 struct nfs4_reclaim_complete_data *calldata = data;
5950 struct nfs_client *clp = calldata->clp;
5951 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5952
5953 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005954 if (!nfs41_sequence_done(task, res))
5955 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005956
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005957 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5958 rpc_restart_call_prepare(task);
5959 return;
5960 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005961 dprintk("<-- %s\n", __func__);
5962}
5963
5964static void nfs4_free_reclaim_complete_data(void *data)
5965{
5966 struct nfs4_reclaim_complete_data *calldata = data;
5967
5968 kfree(calldata);
5969}
5970
5971static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5972 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5973 .rpc_call_done = nfs4_reclaim_complete_done,
5974 .rpc_release = nfs4_free_reclaim_complete_data,
5975};
5976
5977/*
5978 * Issue a global reclaim complete.
5979 */
5980static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5981{
5982 struct nfs4_reclaim_complete_data *calldata;
5983 struct rpc_task *task;
5984 struct rpc_message msg = {
5985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5986 };
5987 struct rpc_task_setup task_setup_data = {
5988 .rpc_client = clp->cl_rpcclient,
5989 .rpc_message = &msg,
5990 .callback_ops = &nfs4_reclaim_complete_call_ops,
5991 .flags = RPC_TASK_ASYNC,
5992 };
5993 int status = -ENOMEM;
5994
5995 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005996 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005997 if (calldata == NULL)
5998 goto out;
5999 calldata->clp = clp;
6000 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006001
Trond Myklebust9d12b212012-01-17 22:04:25 -05006002 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006003 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006004 msg.rpc_argp = &calldata->arg;
6005 msg.rpc_resp = &calldata->res;
6006 task_setup_data.callback_data = calldata;
6007 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006008 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006009 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006010 goto out;
6011 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006012 status = nfs4_wait_for_completion_rpc_task(task);
6013 if (status == 0)
6014 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006015 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006016 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006017out:
6018 dprintk("<-- %s status=%d\n", __func__, status);
6019 return status;
6020}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006021
6022static void
6023nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6024{
6025 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006026 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006027 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006028
6029 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006030 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6031 * right now covering the LAYOUTGET we are about to send.
6032 * However, that is not so catastrophic, and there seems
6033 * to be no way to prevent it completely.
6034 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006035 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006036 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006037 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006038 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6039 NFS_I(lgp->args.inode)->layout,
6040 lgp->args.ctx->state)) {
6041 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006042 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006043}
6044
6045static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6046{
6047 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006048 struct inode *inode = lgp->args.inode;
6049 struct nfs_server *server = NFS_SERVER(inode);
6050 struct pnfs_layout_hdr *lo;
6051 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006052 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006053
6054 dprintk("--> %s\n", __func__);
6055
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006056 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006057 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006058
6059 switch (task->tk_status) {
6060 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006061 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006062 case -NFS4ERR_LAYOUTTRYLATER:
6063 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006064 timeo = rpc_get_timeout(task->tk_client);
6065 giveup = lgp->args.timestamp + timeo;
6066 if (time_after(giveup, jiffies))
6067 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006068 break;
6069 case -NFS4ERR_EXPIRED:
6070 case -NFS4ERR_BAD_STATEID:
6071 spin_lock(&inode->i_lock);
6072 lo = NFS_I(inode)->layout;
6073 if (!lo || list_empty(&lo->plh_segs)) {
6074 spin_unlock(&inode->i_lock);
6075 /* If the open stateid was bad, then recover it. */
6076 state = lgp->args.ctx->state;
6077 } else {
6078 LIST_HEAD(head);
6079
6080 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6081 spin_unlock(&inode->i_lock);
6082 /* Mark the bad layout state as invalid, then
6083 * retry using the open stateid. */
6084 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006085 }
6086 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006087 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6088 rpc_restart_call_prepare(task);
6089out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006090 dprintk("<-- %s\n", __func__);
6091}
6092
Idan Kedar85541162012-08-02 11:47:10 +03006093static size_t max_response_pages(struct nfs_server *server)
6094{
6095 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6096 return nfs_page_array_len(0, max_resp_sz);
6097}
6098
6099static void nfs4_free_pages(struct page **pages, size_t size)
6100{
6101 int i;
6102
6103 if (!pages)
6104 return;
6105
6106 for (i = 0; i < size; i++) {
6107 if (!pages[i])
6108 break;
6109 __free_page(pages[i]);
6110 }
6111 kfree(pages);
6112}
6113
6114static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6115{
6116 struct page **pages;
6117 int i;
6118
6119 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6120 if (!pages) {
6121 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6122 return NULL;
6123 }
6124
6125 for (i = 0; i < size; i++) {
6126 pages[i] = alloc_page(gfp_flags);
6127 if (!pages[i]) {
6128 dprintk("%s: failed to allocate page\n", __func__);
6129 nfs4_free_pages(pages, size);
6130 return NULL;
6131 }
6132 }
6133
6134 return pages;
6135}
6136
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006137static void nfs4_layoutget_release(void *calldata)
6138{
6139 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006140 struct inode *inode = lgp->args.inode;
6141 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006142 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006143
6144 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006145 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006146 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006147 put_nfs_open_context(lgp->args.ctx);
6148 kfree(calldata);
6149 dprintk("<-- %s\n", __func__);
6150}
6151
6152static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6153 .rpc_call_prepare = nfs4_layoutget_prepare,
6154 .rpc_call_done = nfs4_layoutget_done,
6155 .rpc_release = nfs4_layoutget_release,
6156};
6157
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006158struct pnfs_layout_segment *
6159nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006160{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006161 struct inode *inode = lgp->args.inode;
6162 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006163 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006164 struct rpc_task *task;
6165 struct rpc_message msg = {
6166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6167 .rpc_argp = &lgp->args,
6168 .rpc_resp = &lgp->res,
6169 };
6170 struct rpc_task_setup task_setup_data = {
6171 .rpc_client = server->client,
6172 .rpc_message = &msg,
6173 .callback_ops = &nfs4_layoutget_call_ops,
6174 .callback_data = lgp,
6175 .flags = RPC_TASK_ASYNC,
6176 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006177 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006178 int status = 0;
6179
6180 dprintk("--> %s\n", __func__);
6181
Idan Kedar85541162012-08-02 11:47:10 +03006182 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6183 if (!lgp->args.layout.pages) {
6184 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006185 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006186 }
6187 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006188 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006189
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006190 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006191 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006192 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006193
6194 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6195 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6196
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006197 task = rpc_run_task(&task_setup_data);
6198 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006199 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006200 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006201 if (status == 0)
6202 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006203 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6204 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006205 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006206 rpc_put_task(task);
6207 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006208 if (status)
6209 return ERR_PTR(status);
6210 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006211}
6212
Benny Halevycbe82602011-05-22 19:52:37 +03006213static void
6214nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6215{
6216 struct nfs4_layoutreturn *lrp = calldata;
6217
6218 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006219 nfs41_setup_sequence(lrp->clp->cl_session,
6220 &lrp->args.seq_args,
6221 &lrp->res.seq_res,
6222 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006223}
6224
6225static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6226{
6227 struct nfs4_layoutreturn *lrp = calldata;
6228 struct nfs_server *server;
6229
6230 dprintk("--> %s\n", __func__);
6231
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006232 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006233 return;
6234
6235 server = NFS_SERVER(lrp->args.inode);
6236 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006237 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006238 return;
6239 }
Benny Halevycbe82602011-05-22 19:52:37 +03006240 dprintk("<-- %s\n", __func__);
6241}
6242
6243static void nfs4_layoutreturn_release(void *calldata)
6244{
6245 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006246 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006247
6248 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006249 spin_lock(&lo->plh_inode->i_lock);
6250 if (lrp->res.lrs_present)
6251 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6252 lo->plh_block_lgets--;
6253 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006254 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006255 kfree(calldata);
6256 dprintk("<-- %s\n", __func__);
6257}
6258
6259static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6260 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6261 .rpc_call_done = nfs4_layoutreturn_done,
6262 .rpc_release = nfs4_layoutreturn_release,
6263};
6264
6265int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6266{
6267 struct rpc_task *task;
6268 struct rpc_message msg = {
6269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6270 .rpc_argp = &lrp->args,
6271 .rpc_resp = &lrp->res,
6272 };
6273 struct rpc_task_setup task_setup_data = {
6274 .rpc_client = lrp->clp->cl_rpcclient,
6275 .rpc_message = &msg,
6276 .callback_ops = &nfs4_layoutreturn_call_ops,
6277 .callback_data = lrp,
6278 };
6279 int status;
6280
6281 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006282 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006283 task = rpc_run_task(&task_setup_data);
6284 if (IS_ERR(task))
6285 return PTR_ERR(task);
6286 status = task->tk_status;
6287 dprintk("<-- %s status=%d\n", __func__, status);
6288 rpc_put_task(task);
6289 return status;
6290}
6291
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006292/*
6293 * Retrieve the list of Data Server devices from the MDS.
6294 */
6295static int _nfs4_getdevicelist(struct nfs_server *server,
6296 const struct nfs_fh *fh,
6297 struct pnfs_devicelist *devlist)
6298{
6299 struct nfs4_getdevicelist_args args = {
6300 .fh = fh,
6301 .layoutclass = server->pnfs_curr_ld->id,
6302 };
6303 struct nfs4_getdevicelist_res res = {
6304 .devlist = devlist,
6305 };
6306 struct rpc_message msg = {
6307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6308 .rpc_argp = &args,
6309 .rpc_resp = &res,
6310 };
6311 int status;
6312
6313 dprintk("--> %s\n", __func__);
6314 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6315 &res.seq_res, 0);
6316 dprintk("<-- %s status=%d\n", __func__, status);
6317 return status;
6318}
6319
6320int nfs4_proc_getdevicelist(struct nfs_server *server,
6321 const struct nfs_fh *fh,
6322 struct pnfs_devicelist *devlist)
6323{
6324 struct nfs4_exception exception = { };
6325 int err;
6326
6327 do {
6328 err = nfs4_handle_exception(server,
6329 _nfs4_getdevicelist(server, fh, devlist),
6330 &exception);
6331 } while (exception.retry);
6332
6333 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6334 err, devlist->num_devs);
6335
6336 return err;
6337}
6338EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6339
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006340static int
6341_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6342{
6343 struct nfs4_getdeviceinfo_args args = {
6344 .pdev = pdev,
6345 };
6346 struct nfs4_getdeviceinfo_res res = {
6347 .pdev = pdev,
6348 };
6349 struct rpc_message msg = {
6350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6351 .rpc_argp = &args,
6352 .rpc_resp = &res,
6353 };
6354 int status;
6355
6356 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006357 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006358 dprintk("<-- %s status=%d\n", __func__, status);
6359
6360 return status;
6361}
6362
6363int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6364{
6365 struct nfs4_exception exception = { };
6366 int err;
6367
6368 do {
6369 err = nfs4_handle_exception(server,
6370 _nfs4_proc_getdeviceinfo(server, pdev),
6371 &exception);
6372 } while (exception.retry);
6373 return err;
6374}
6375EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6376
Andy Adamson863a3c62011-03-23 13:27:54 +00006377static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6378{
6379 struct nfs4_layoutcommit_data *data = calldata;
6380 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006381 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006382
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006383 nfs41_setup_sequence(session,
6384 &data->args.seq_args,
6385 &data->res.seq_res,
6386 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006387}
6388
6389static void
6390nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6391{
6392 struct nfs4_layoutcommit_data *data = calldata;
6393 struct nfs_server *server = NFS_SERVER(data->args.inode);
6394
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006395 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006396 return;
6397
6398 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006399 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6400 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6401 case -NFS4ERR_BADLAYOUT: /* no layout */
6402 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006403 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006404 break;
6405 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006406 nfs_post_op_update_inode_force_wcc(data->args.inode,
6407 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006408 break;
6409 default:
6410 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6411 rpc_restart_call_prepare(task);
6412 return;
6413 }
6414 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006415}
6416
6417static void nfs4_layoutcommit_release(void *calldata)
6418{
6419 struct nfs4_layoutcommit_data *data = calldata;
6420
Andy Adamsondb29c082011-07-30 20:52:38 -04006421 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006422 put_rpccred(data->cred);
6423 kfree(data);
6424}
6425
6426static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6427 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6428 .rpc_call_done = nfs4_layoutcommit_done,
6429 .rpc_release = nfs4_layoutcommit_release,
6430};
6431
6432int
Andy Adamsonef311532011-03-12 02:58:10 -05006433nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006434{
6435 struct rpc_message msg = {
6436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6437 .rpc_argp = &data->args,
6438 .rpc_resp = &data->res,
6439 .rpc_cred = data->cred,
6440 };
6441 struct rpc_task_setup task_setup_data = {
6442 .task = &data->task,
6443 .rpc_client = NFS_CLIENT(data->args.inode),
6444 .rpc_message = &msg,
6445 .callback_ops = &nfs4_layoutcommit_ops,
6446 .callback_data = data,
6447 .flags = RPC_TASK_ASYNC,
6448 };
6449 struct rpc_task *task;
6450 int status = 0;
6451
6452 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6453 "lbw: %llu inode %lu\n",
6454 data->task.tk_pid, sync,
6455 data->args.lastbytewritten,
6456 data->args.inode->i_ino);
6457
Trond Myklebust9d12b212012-01-17 22:04:25 -05006458 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006459 task = rpc_run_task(&task_setup_data);
6460 if (IS_ERR(task))
6461 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006462 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006463 goto out;
6464 status = nfs4_wait_for_completion_rpc_task(task);
6465 if (status != 0)
6466 goto out;
6467 status = task->tk_status;
6468out:
6469 dprintk("%s: status %d\n", __func__, status);
6470 rpc_put_task(task);
6471 return status;
6472}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006473
6474static int
6475_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6476 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6477{
6478 struct nfs41_secinfo_no_name_args args = {
6479 .style = SECINFO_STYLE_CURRENT_FH,
6480 };
6481 struct nfs4_secinfo_res res = {
6482 .flavors = flavors,
6483 };
6484 struct rpc_message msg = {
6485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6486 .rpc_argp = &args,
6487 .rpc_resp = &res,
6488 };
6489 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6490}
6491
6492static int
6493nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6494 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6495{
6496 struct nfs4_exception exception = { };
6497 int err;
6498 do {
6499 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6500 switch (err) {
6501 case 0:
6502 case -NFS4ERR_WRONGSEC:
6503 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006504 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006505 default:
6506 err = nfs4_handle_exception(server, err, &exception);
6507 }
6508 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006509out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006510 return err;
6511}
6512
6513static int
6514nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6515 struct nfs_fsinfo *info)
6516{
6517 int err;
6518 struct page *page;
6519 rpc_authflavor_t flavor;
6520 struct nfs4_secinfo_flavors *flavors;
6521
6522 page = alloc_page(GFP_KERNEL);
6523 if (!page) {
6524 err = -ENOMEM;
6525 goto out;
6526 }
6527
6528 flavors = page_address(page);
6529 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6530
6531 /*
6532 * Fall back on "guess and check" method if
6533 * the server doesn't support SECINFO_NO_NAME
6534 */
6535 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6536 err = nfs4_find_root_sec(server, fhandle, info);
6537 goto out_freepage;
6538 }
6539 if (err)
6540 goto out_freepage;
6541
6542 flavor = nfs_find_best_sec(flavors);
6543 if (err == 0)
6544 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6545
6546out_freepage:
6547 put_page(page);
6548 if (err == -EACCES)
6549 return -EPERM;
6550out:
6551 return err;
6552}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006553
6554static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006555{
6556 int status;
6557 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006558 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006559 };
6560 struct nfs41_test_stateid_res res;
6561 struct rpc_message msg = {
6562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6563 .rpc_argp = &args,
6564 .rpc_resp = &res,
6565 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006566
Chuck Lever38527b12012-07-11 16:30:23 -04006567 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006568 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006569 nfs4_set_sequence_privileged(&args.seq_args);
6570 status = nfs4_call_sync_sequence(server->client, server, &msg,
6571 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006572 if (status != NFS_OK) {
6573 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006574 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006575 }
6576 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006577 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006578}
6579
Chuck Lever38527b12012-07-11 16:30:23 -04006580/**
6581 * nfs41_test_stateid - perform a TEST_STATEID operation
6582 *
6583 * @server: server / transport on which to perform the operation
6584 * @stateid: state ID to test
6585 *
6586 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6587 * Otherwise a negative NFS4ERR value is returned if the operation
6588 * failed or the state ID is not currently valid.
6589 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006590static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006591{
6592 struct nfs4_exception exception = { };
6593 int err;
6594 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006595 err = _nfs41_test_stateid(server, stateid);
6596 if (err != -NFS4ERR_DELAY)
6597 break;
6598 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006599 } while (exception.retry);
6600 return err;
6601}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006602
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006603static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006604{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006605 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006606 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006607 };
6608 struct nfs41_free_stateid_res res;
6609 struct rpc_message msg = {
6610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6611 .rpc_argp = &args,
6612 .rpc_resp = &res,
6613 };
Chuck Lever38527b12012-07-11 16:30:23 -04006614 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006615
Chuck Lever38527b12012-07-11 16:30:23 -04006616 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006617 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006618 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006619 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006620 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006621 dprintk("NFS reply free_stateid: %d\n", status);
6622 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006623}
6624
Chuck Lever38527b12012-07-11 16:30:23 -04006625/**
6626 * nfs41_free_stateid - perform a FREE_STATEID operation
6627 *
6628 * @server: server / transport on which to perform the operation
6629 * @stateid: state ID to release
6630 *
6631 * Returns NFS_OK if the server freed "stateid". Otherwise a
6632 * negative NFS4ERR value is returned.
6633 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006634static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006635{
6636 struct nfs4_exception exception = { };
6637 int err;
6638 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006639 err = _nfs4_free_stateid(server, stateid);
6640 if (err != -NFS4ERR_DELAY)
6641 break;
6642 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006643 } while (exception.retry);
6644 return err;
6645}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006646
6647static bool nfs41_match_stateid(const nfs4_stateid *s1,
6648 const nfs4_stateid *s2)
6649{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006650 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006651 return false;
6652
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006653 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006654 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006655 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006656 return true;
6657
6658 return false;
6659}
6660
Andy Adamson557134a2009-04-01 09:21:53 -04006661#endif /* CONFIG_NFS_V4_1 */
6662
Trond Myklebust36281ca2012-03-04 18:13:56 -05006663static bool nfs4_match_stateid(const nfs4_stateid *s1,
6664 const nfs4_stateid *s2)
6665{
Trond Myklebustf597c532012-03-04 18:13:56 -05006666 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006667}
6668
6669
Trond Myklebust17280172012-03-11 13:11:00 -04006670static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006671 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006672 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673 .recover_open = nfs4_open_reclaim,
6674 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006675 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006676 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006677 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678};
6679
Andy Adamson591d71c2009-04-01 09:22:47 -04006680#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006681static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006682 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6683 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6684 .recover_open = nfs4_open_reclaim,
6685 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006686 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006687 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006688 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006689 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006690};
6691#endif /* CONFIG_NFS_V4_1 */
6692
Trond Myklebust17280172012-03-11 13:11:00 -04006693static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006694 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006695 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 .recover_open = nfs4_open_expired,
6697 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006698 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006699 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700};
6701
Andy Adamson591d71c2009-04-01 09:22:47 -04006702#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006703static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006704 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6705 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006706 .recover_open = nfs41_open_expired,
6707 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006708 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006709 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006710};
6711#endif /* CONFIG_NFS_V4_1 */
6712
Trond Myklebust17280172012-03-11 13:11:00 -04006713static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006714 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006715 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006716 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006717};
6718
6719#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006720static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006721 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006722 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006723 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006724};
6725#endif
6726
Trond Myklebust97dc1352010-06-16 09:52:26 -04006727static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6728 .minor_version = 0,
6729 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006730 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006731 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006732 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6733 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6734 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006735};
6736
6737#if defined(CONFIG_NFS_V4_1)
6738static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6739 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006740 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006741 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006742 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006743 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6744 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6745 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006746};
6747#endif
6748
Trond Myklebust97dc1352010-06-16 09:52:26 -04006749const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6750 [0] = &nfs_v4_0_minor_ops,
6751#if defined(CONFIG_NFS_V4_1)
6752 [1] = &nfs_v4_1_minor_ops,
6753#endif
6754};
6755
Bryan Schumaker73a79702012-07-16 16:39:12 -04006756const struct inode_operations nfs4_dir_inode_operations = {
6757 .create = nfs_create,
6758 .lookup = nfs_lookup,
6759 .atomic_open = nfs_atomic_open,
6760 .link = nfs_link,
6761 .unlink = nfs_unlink,
6762 .symlink = nfs_symlink,
6763 .mkdir = nfs_mkdir,
6764 .rmdir = nfs_rmdir,
6765 .mknod = nfs_mknod,
6766 .rename = nfs_rename,
6767 .permission = nfs_permission,
6768 .getattr = nfs_getattr,
6769 .setattr = nfs_setattr,
6770 .getxattr = generic_getxattr,
6771 .setxattr = generic_setxattr,
6772 .listxattr = generic_listxattr,
6773 .removexattr = generic_removexattr,
6774};
6775
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006776static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006777 .permission = nfs_permission,
6778 .getattr = nfs_getattr,
6779 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006780 .getxattr = generic_getxattr,
6781 .setxattr = generic_setxattr,
6782 .listxattr = generic_listxattr,
6783 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006784};
6785
David Howells509de812006-08-22 20:06:11 -04006786const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 .version = 4, /* protocol version */
6788 .dentry_ops = &nfs4_dentry_operations,
6789 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006790 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006791 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006793 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006794 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 .getattr = nfs4_proc_getattr,
6796 .setattr = nfs4_proc_setattr,
6797 .lookup = nfs4_proc_lookup,
6798 .access = nfs4_proc_access,
6799 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 .create = nfs4_proc_create,
6801 .remove = nfs4_proc_remove,
6802 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006803 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 .unlink_done = nfs4_proc_unlink_done,
6805 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006806 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006807 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006808 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 .link = nfs4_proc_link,
6810 .symlink = nfs4_proc_symlink,
6811 .mkdir = nfs4_proc_mkdir,
6812 .rmdir = nfs4_proc_remove,
6813 .readdir = nfs4_proc_readdir,
6814 .mknod = nfs4_proc_mknod,
6815 .statfs = nfs4_proc_statfs,
6816 .fsinfo = nfs4_proc_fsinfo,
6817 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006818 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 .decode_dirent = nfs4_decode_dirent,
6820 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006821 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006822 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006823 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006825 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006826 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006827 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006829 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006830 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006832 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006833 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006834 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006835 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006836 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006837 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006838 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006839 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006840 .create_server = nfs4_create_server,
6841 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842};
6843
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006844static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6845 .prefix = XATTR_NAME_NFSV4_ACL,
6846 .list = nfs4_xattr_list_nfs4_acl,
6847 .get = nfs4_xattr_get_nfs4_acl,
6848 .set = nfs4_xattr_set_nfs4_acl,
6849};
6850
6851const struct xattr_handler *nfs4_xattr_handlers[] = {
6852 &nfs4_xattr_nfs4_acl_handler,
6853 NULL
6854};
6855
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856/*
6857 * Local variables:
6858 * c-basic-offset: 8
6859 * End:
6860 */