blob: b2671cb0f901b8e904ee4b91961d2029e12fb2dc [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 Myklebustaf22f942007-08-10 17:45:10 -04001049 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001050 if (ret != 0)
1051 goto out;
1052 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001053
1054 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001055 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001056 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058out:
1059 return ERR_PTR(ret);
1060out_return_state:
1061 atomic_inc(&state->count);
1062 return state;
1063}
1064
Andy Adamsone23008e2012-10-02 21:07:32 -04001065static void
1066nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1067{
1068 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1069 struct nfs_delegation *delegation;
1070 int delegation_flags = 0;
1071
1072 rcu_read_lock();
1073 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1074 if (delegation)
1075 delegation_flags = delegation->flags;
1076 rcu_read_unlock();
1077 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1078 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1079 "returning a delegation for "
1080 "OPEN(CLAIM_DELEGATE_CUR)\n",
1081 clp->cl_hostname);
1082 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1083 nfs_inode_set_delegation(state->inode,
1084 data->owner->so_cred,
1085 &data->o_res);
1086 else
1087 nfs_inode_reclaim_delegation(state->inode,
1088 data->owner->so_cred,
1089 &data->o_res);
1090}
1091
1092/*
1093 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1094 * and update the nfs4_state.
1095 */
1096static struct nfs4_state *
1097_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1098{
1099 struct inode *inode = data->state->inode;
1100 struct nfs4_state *state = data->state;
1101 int ret;
1102
1103 if (!data->rpc_done) {
1104 ret = data->rpc_status;
1105 goto err;
1106 }
1107
1108 ret = -ESTALE;
1109 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1110 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1111 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1112 goto err;
1113
1114 ret = -ENOMEM;
1115 state = nfs4_get_open_state(inode, data->owner);
1116 if (state == NULL)
1117 goto err;
1118
1119 ret = nfs_refresh_inode(inode, &data->f_attr);
1120 if (ret)
1121 goto err;
1122
1123 if (data->o_res.delegation_type != 0)
1124 nfs4_opendata_check_deleg(data, state);
1125 update_open_stateid(state, &data->o_res.stateid, NULL,
1126 data->o_arg.fmode);
1127
1128 return state;
1129err:
1130 return ERR_PTR(ret);
1131
1132}
1133
1134static struct nfs4_state *
1135_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001136{
1137 struct inode *inode;
1138 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001139 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001140
Trond Myklebustaac00a82007-07-05 19:02:21 -04001141 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001142 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001143 goto out;
1144 }
1145
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001146 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001147 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001148 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001149 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001150 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001151 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001152 goto err;
1153 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001154 state = nfs4_get_open_state(inode, data->owner);
1155 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001156 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001157 if (data->o_res.delegation_type != 0)
1158 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001159 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001160 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001161 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001162out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001163 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001164 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001165err_put_inode:
1166 iput(inode);
1167err:
1168 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001169}
1170
Andy Adamsone23008e2012-10-02 21:07:32 -04001171static struct nfs4_state *
1172nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1173{
1174 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1175 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1176 return _nfs4_opendata_to_nfs4_state(data);
1177}
1178
Trond Myklebust864472e2006-01-03 09:55:15 +01001179static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1180{
1181 struct nfs_inode *nfsi = NFS_I(state->inode);
1182 struct nfs_open_context *ctx;
1183
1184 spin_lock(&state->inode->i_lock);
1185 list_for_each_entry(ctx, &nfsi->open_files, list) {
1186 if (ctx->state != state)
1187 continue;
1188 get_nfs_open_context(ctx);
1189 spin_unlock(&state->inode->i_lock);
1190 return ctx;
1191 }
1192 spin_unlock(&state->inode->i_lock);
1193 return ERR_PTR(-ENOENT);
1194}
1195
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001196static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1197{
1198 struct nfs4_opendata *opendata;
1199
Al Viro3d4ff432011-06-22 18:40:12 -04001200 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001201 if (opendata == NULL)
1202 return ERR_PTR(-ENOMEM);
1203 opendata->state = state;
1204 atomic_inc(&state->count);
1205 return opendata;
1206}
1207
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001208static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001209{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001210 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 int ret;
1212
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001213 opendata->o_arg.open_flags = 0;
1214 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001215 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1216 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1217 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001218 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 if (ret != 0)
1220 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001221 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001222 if (IS_ERR(newstate))
1223 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001224 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001225 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 return 0;
1227}
1228
1229static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1230{
Trond Myklebust864472e2006-01-03 09:55:15 +01001231 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001232 int ret;
1233
1234 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001235 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 smp_rmb();
1237 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001238 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001239 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001240 if (ret != 0)
1241 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001242 if (newstate != state)
1243 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001244 }
1245 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001246 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001247 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001248 if (ret != 0)
1249 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001250 if (newstate != state)
1251 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001252 }
1253 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001254 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001255 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001256 if (ret != 0)
1257 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001258 if (newstate != state)
1259 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001261 /*
1262 * We may have performed cached opens for all three recoveries.
1263 * Check if we need to update the current stateid.
1264 */
1265 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001266 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001267 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001268 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001269 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001270 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001271 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001272 return 0;
1273}
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275/*
1276 * OPEN_RECLAIM:
1277 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001279static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001281 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001283 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int status;
1285
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001286 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1287 if (IS_ERR(opendata))
1288 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1290 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001291 rcu_read_lock();
1292 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001293 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001294 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001295 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 opendata->o_arg.u.delegation_type = delegation_type;
1297 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001298 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return status;
1300}
1301
Trond Myklebust539cd032007-06-05 11:46:42 -04001302static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct nfs_server *server = NFS_SERVER(state->inode);
1305 struct nfs4_exception exception = { };
1306 int err;
1307 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001308 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001309 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001310 break;
1311 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 } while (exception.retry);
1313 return err;
1314}
1315
Trond Myklebust864472e2006-01-03 09:55:15 +01001316static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1317{
1318 struct nfs_open_context *ctx;
1319 int ret;
1320
1321 ctx = nfs4_state_find_open_context(state);
1322 if (IS_ERR(ctx))
1323 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001324 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001325 put_nfs_open_context(ctx);
1326 return ret;
1327}
1328
Trond Myklebust13437e12007-07-06 15:10:43 -04001329static 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 -07001330{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001331 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001332 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001334 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1335 if (IS_ERR(opendata))
1336 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001337 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001338 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001339 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001340 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Trond Myklebust13437e12007-07-06 15:10:43 -04001344int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001347 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 int err;
1349 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001350 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 switch (err) {
1352 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001353 case -ENOENT:
1354 case -ESTALE:
1355 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001356 case -NFS4ERR_BADSESSION:
1357 case -NFS4ERR_BADSLOT:
1358 case -NFS4ERR_BAD_HIGH_SLOT:
1359 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1360 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05001361 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04001362 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05001363 err = -EAGAIN;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 case -NFS4ERR_STALE_CLIENTID:
1366 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05001367 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 case -NFS4ERR_EXPIRED:
1369 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001370 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05001371 err = -EAGAIN;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001372 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001373 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001374 case -NFS4ERR_ADMIN_REVOKED:
1375 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001376 nfs_inode_find_state_and_recover(state->inode,
1377 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001378 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001379 case -ENOMEM:
1380 err = 0;
1381 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Trond Myklebust37380e42013-02-05 12:35:16 -05001383 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 err = nfs4_handle_exception(server, err, &exception);
1385 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001386out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return err;
1388}
1389
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001390static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1391{
1392 struct nfs4_opendata *data = calldata;
1393
1394 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001395 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001396 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001397 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001398 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001399 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001400 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401}
1402
1403static void nfs4_open_confirm_release(void *calldata)
1404{
1405 struct nfs4_opendata *data = calldata;
1406 struct nfs4_state *state = NULL;
1407
1408 /* If this request hasn't been cancelled, do nothing */
1409 if (data->cancelled == 0)
1410 goto out_free;
1411 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001412 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001413 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001414 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001415 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001416 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001417out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001418 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001419}
1420
1421static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001422 .rpc_call_done = nfs4_open_confirm_done,
1423 .rpc_release = nfs4_open_confirm_release,
1424};
1425
1426/*
1427 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1428 */
1429static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1430{
1431 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1432 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001433 struct rpc_message msg = {
1434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1435 .rpc_argp = &data->c_arg,
1436 .rpc_resp = &data->c_res,
1437 .rpc_cred = data->owner->so_cred,
1438 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001439 struct rpc_task_setup task_setup_data = {
1440 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001441 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001442 .callback_ops = &nfs4_open_confirm_ops,
1443 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001444 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001445 .flags = RPC_TASK_ASYNC,
1446 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int status;
1448
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001449 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001450 data->rpc_done = 0;
1451 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001452 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001453 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001454 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001455 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001456 status = nfs4_wait_for_completion_rpc_task(task);
1457 if (status != 0) {
1458 data->cancelled = 1;
1459 smp_wmb();
1460 } else
1461 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001462 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return status;
1464}
1465
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468 struct nfs4_opendata *data = calldata;
1469 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001470
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001472 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001473 /*
1474 * Check if we still need to send an OPEN call, or if we can use
1475 * a delegation instead.
1476 */
1477 if (data->state != NULL) {
1478 struct nfs_delegation *delegation;
1479
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001480 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001481 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001482 rcu_read_lock();
1483 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001484 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1485 can_open_delegated(delegation, data->o_arg.fmode))
1486 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001487 rcu_read_unlock();
1488 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001489 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001490 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001491 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001492 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001493 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001494 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1495 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001496 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001497 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001498 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001499 &data->o_res.seq_res,
1500 task) != 0)
1501 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001502 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001503unlock_no_action:
1504 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001505out_no_action:
1506 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001507out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001508 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001509}
1510
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511static void nfs4_open_done(struct rpc_task *task, void *calldata)
1512{
1513 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001515 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001516
Trond Myklebust14516c32010-07-31 14:29:06 -04001517 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1518 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001519
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001521 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1522 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001523 case S_IFREG:
1524 break;
1525 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001527 break;
1528 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001529 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530 break;
1531 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001533 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001534 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001535 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001536 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1537 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001538 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001539 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001540}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001541
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542static void nfs4_open_release(void *calldata)
1543{
1544 struct nfs4_opendata *data = calldata;
1545 struct nfs4_state *state = NULL;
1546
1547 /* If this request hasn't been cancelled, do nothing */
1548 if (data->cancelled == 0)
1549 goto out_free;
1550 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001551 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001552 goto out_free;
1553 /* In case we need an open_confirm, no cleanup! */
1554 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1555 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001556 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001557 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001558 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001560 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001561}
1562
1563static const struct rpc_call_ops nfs4_open_ops = {
1564 .rpc_call_prepare = nfs4_open_prepare,
1565 .rpc_call_done = nfs4_open_done,
1566 .rpc_release = nfs4_open_release,
1567};
1568
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001569static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001570{
1571 struct inode *dir = data->dir->d_inode;
1572 struct nfs_server *server = NFS_SERVER(dir);
1573 struct nfs_openargs *o_arg = &data->o_arg;
1574 struct nfs_openres *o_res = &data->o_res;
1575 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001576 struct rpc_message msg = {
1577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1578 .rpc_argp = o_arg,
1579 .rpc_resp = o_res,
1580 .rpc_cred = data->owner->so_cred,
1581 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001582 struct rpc_task_setup task_setup_data = {
1583 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001584 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001585 .callback_ops = &nfs4_open_ops,
1586 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001587 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001588 .flags = RPC_TASK_ASYNC,
1589 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001590 int status;
1591
Trond Myklebust9d12b212012-01-17 22:04:25 -05001592 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001593 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001594 data->rpc_done = 0;
1595 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001596 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001597 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001598 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001599 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001600 if (IS_ERR(task))
1601 return PTR_ERR(task);
1602 status = nfs4_wait_for_completion_rpc_task(task);
1603 if (status != 0) {
1604 data->cancelled = 1;
1605 smp_wmb();
1606 } else
1607 status = data->rpc_status;
1608 rpc_put_task(task);
1609
1610 return status;
1611}
1612
1613static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1614{
1615 struct inode *dir = data->dir->d_inode;
1616 struct nfs_openres *o_res = &data->o_res;
1617 int status;
1618
1619 status = nfs4_run_open_task(data, 1);
1620 if (status != 0 || !data->rpc_done)
1621 return status;
1622
Trond Myklebust6926afd2012-01-07 13:22:46 -05001623 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1624
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001625 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1626 status = _nfs4_proc_open_confirm(data);
1627 if (status != 0)
1628 return status;
1629 }
1630
1631 return status;
1632}
1633
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001634static int nfs4_opendata_access(struct rpc_cred *cred,
1635 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001636 struct nfs4_state *state, fmode_t fmode,
1637 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001638{
1639 struct nfs_access_entry cache;
1640 u32 mask;
1641
1642 /* access call failed or for some reason the server doesn't
1643 * support any access modes -- defer access call until later */
1644 if (opendata->o_res.access_supported == 0)
1645 return 0;
1646
1647 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001648 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001649 * write mode bits, but POSIX allows the creating process to write.
1650 * use openflags to check for exec, because fmode won't
1651 * always have FMODE_EXEC set when file open for exec. */
1652 if (openflags & __FMODE_EXEC) {
1653 /* ONLY check for exec rights */
1654 mask = MAY_EXEC;
1655 } else if (fmode & FMODE_READ)
1656 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001657
1658 cache.cred = cred;
1659 cache.jiffies = jiffies;
1660 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1661 nfs_access_add_cache(state->inode, &cache);
1662
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001663 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001664 return 0;
1665
1666 /* even though OPEN succeeded, access is denied. Close the file */
1667 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001668 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001669}
1670
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001671/*
1672 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1673 */
1674static int _nfs4_proc_open(struct nfs4_opendata *data)
1675{
1676 struct inode *dir = data->dir->d_inode;
1677 struct nfs_server *server = NFS_SERVER(dir);
1678 struct nfs_openargs *o_arg = &data->o_arg;
1679 struct nfs_openres *o_res = &data->o_res;
1680 int status;
1681
1682 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001683 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001684 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001685 if (status != 0) {
1686 if (status == -NFS4ERR_BADNAME &&
1687 !(o_arg->open_flags & O_CREAT))
1688 return -ENOENT;
1689 return status;
1690 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001691
Trond Myklebust6926afd2012-01-07 13:22:46 -05001692 nfs_fattr_map_and_free_names(server, &data->f_attr);
1693
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001694 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001695 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001696 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1697 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001699 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001701 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001704 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Andy Adamsond83217c2011-03-01 01:34:17 +00001708static int nfs4_recover_expired_lease(struct nfs_server *server)
1709{
1710 return nfs4_client_recover_expired_lease(server->nfs_client);
1711}
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713/*
1714 * OPEN_EXPIRED:
1715 * reclaim state on the server after a network partition.
1716 * Assumes caller holds the appropriate lock
1717 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001718static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001720 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001721 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001723 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1724 if (IS_ERR(opendata))
1725 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001726 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001727 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001728 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001729 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001730 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001733static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001734{
Trond Myklebust539cd032007-06-05 11:46:42 -04001735 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001736 struct nfs4_exception exception = { };
1737 int err;
1738
1739 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001740 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001741 switch (err) {
1742 default:
1743 goto out;
1744 case -NFS4ERR_GRACE:
1745 case -NFS4ERR_DELAY:
1746 nfs4_handle_exception(server, err, &exception);
1747 err = 0;
1748 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001749 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001750out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001751 return err;
1752}
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1755{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001757 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Trond Myklebust864472e2006-01-03 09:55:15 +01001759 ctx = nfs4_state_find_open_context(state);
1760 if (IS_ERR(ctx))
1761 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001762 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001763 put_nfs_open_context(ctx);
1764 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001767#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001768static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001769{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001770 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001771 nfs4_stateid *stateid = &state->stateid;
1772 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001773
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001774 /* If a state reset has been done, test_stateid is unneeded */
1775 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1776 return;
1777
1778 status = nfs41_test_stateid(server, stateid);
1779 if (status != NFS_OK) {
1780 /* Free the stateid unless the server explicitly
1781 * informs us the stateid is unrecognized. */
1782 if (status != -NFS4ERR_BAD_STATEID)
1783 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001784 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001785
Bryan Schumaker69388672012-09-26 15:25:52 -04001786 write_seqlock(&state->seqlock);
1787 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1788 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001789 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1790 }
1791}
1792
1793/**
1794 * nfs41_check_open_stateid - possibly free an open stateid
1795 *
1796 * @state: NFSv4 state for an inode
1797 *
1798 * Returns NFS_OK if recovery for this stateid is now finished.
1799 * Otherwise a negative NFS4ERR value is returned.
1800 */
1801static int nfs41_check_open_stateid(struct nfs4_state *state)
1802{
1803 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001804 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001805 int status;
1806
1807 /* If a state reset has been done, test_stateid is unneeded */
1808 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1809 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1810 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1811 return -NFS4ERR_BAD_STATEID;
1812
1813 status = nfs41_test_stateid(server, stateid);
1814 if (status != NFS_OK) {
1815 /* Free the stateid unless the server explicitly
1816 * informs us the stateid is unrecognized. */
1817 if (status != -NFS4ERR_BAD_STATEID)
1818 nfs41_free_stateid(server, stateid);
1819
1820 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1821 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1822 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001823 }
1824 return status;
1825}
1826
1827static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1828{
Chuck Levereb64cf92012-07-11 16:30:05 -04001829 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001830
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001831 nfs41_clear_delegation_stateid(state);
1832 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001833 if (status != NFS_OK)
1834 status = nfs4_open_expired(sp, state);
1835 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001836}
1837#endif
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001840 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1841 * fields corresponding to attributes that were used to store the verifier.
1842 * Make sure we clobber those fields in the later setattr call
1843 */
1844static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1845{
1846 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1847 !(sattr->ia_valid & ATTR_ATIME_SET))
1848 sattr->ia_valid |= ATTR_ATIME;
1849
1850 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1851 !(sattr->ia_valid & ATTR_MTIME_SET))
1852 sattr->ia_valid |= ATTR_MTIME;
1853}
1854
Trond Myklebustc21443c2013-02-07 14:26:21 -05001855static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1856 fmode_t fmode,
1857 int flags,
1858 struct nfs4_state **res)
1859{
1860 struct nfs4_state_owner *sp = opendata->owner;
1861 struct nfs_server *server = sp->so_server;
1862 struct nfs4_state *state;
1863 unsigned int seq;
1864 int ret;
1865
1866 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1867
1868 ret = _nfs4_proc_open(opendata);
1869 if (ret != 0)
1870 goto out;
1871
1872 state = nfs4_opendata_to_nfs4_state(opendata);
1873 ret = PTR_ERR(state);
1874 if (IS_ERR(state))
1875 goto out;
1876 if (server->caps & NFS_CAP_POSIX_LOCK)
1877 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1878
1879 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1880 if (ret != 0)
1881 goto out;
1882
1883 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
1884 nfs4_schedule_stateid_recovery(server, state);
1885 nfs4_wait_clnt_recover(server->nfs_client);
1886 }
1887 *res = state;
1888out:
1889 return ret;
1890}
1891
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001892/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001893 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
Andy Adamson82be4172012-05-23 05:02:35 -04001895static int _nfs4_do_open(struct inode *dir,
1896 struct dentry *dentry,
1897 fmode_t fmode,
1898 int flags,
1899 struct iattr *sattr,
1900 struct rpc_cred *cred,
1901 struct nfs4_state **res,
1902 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct nfs4_state_owner *sp;
1905 struct nfs4_state *state = NULL;
1906 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001907 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001908 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001912 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1913 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1915 goto out_err;
1916 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001917 status = nfs4_recover_expired_lease(server);
1918 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001919 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001920 if (dentry->d_inode != NULL)
1921 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001922 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001923 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001924 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001925 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Andy Adamson82be4172012-05-23 05:02:35 -04001927 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1928 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1929 if (!opendata->f_attr.mdsthreshold)
1930 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001931 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001932 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001933 if (dentry->d_inode != NULL)
1934 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001935
Trond Myklebustc21443c2013-02-07 14:26:21 -05001936 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001937 if (status != 0)
1938 goto err_opendata_put;
1939
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001940 if (opendata->o_arg.open_flags & O_EXCL) {
1941 nfs4_exclusive_attrset(opendata, sattr);
1942
1943 nfs_fattr_init(opendata->o_res.f_attr);
1944 status = nfs4_do_setattr(state->inode, cred,
1945 opendata->o_res.f_attr, sattr,
1946 state);
1947 if (status == 0)
1948 nfs_setattr_update_inode(state->inode, sattr);
1949 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1950 }
Andy Adamson82be4172012-05-23 05:02:35 -04001951
1952 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1953 *ctx_th = opendata->f_attr.mdsthreshold;
1954 else
1955 kfree(opendata->f_attr.mdsthreshold);
1956 opendata->f_attr.mdsthreshold = NULL;
1957
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001958 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 *res = state;
1961 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001962err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001963 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001964 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001965err_put_state_owner:
1966 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 *res = NULL;
1969 return status;
1970}
1971
1972
Andy Adamson82be4172012-05-23 05:02:35 -04001973static struct nfs4_state *nfs4_do_open(struct inode *dir,
1974 struct dentry *dentry,
1975 fmode_t fmode,
1976 int flags,
1977 struct iattr *sattr,
1978 struct rpc_cred *cred,
1979 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 struct nfs4_exception exception = { };
1982 struct nfs4_state *res;
1983 int status;
1984
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001985 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001987 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1988 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (status == 0)
1990 break;
1991 /* NOTE: BAD_SEQID means the server and client disagree about the
1992 * book-keeping w.r.t. state-changing operations
1993 * (OPEN/CLOSE/LOCK/LOCKU...)
1994 * It is actually a sign of a bug on the client or on the server.
1995 *
1996 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001997 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 * have unhashed the old state_owner for us, and that we can
1999 * therefore safely retry using a new one. We should still warn
2000 * the user though...
2001 */
2002 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002003 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002004 " returned a bad sequence-id error!\n",
2005 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 exception.retry = 1;
2007 continue;
2008 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002009 /*
2010 * BAD_STATEID on OPEN means that the server cancelled our
2011 * state before it received the OPEN_CONFIRM.
2012 * Recover by retrying the request as per the discussion
2013 * on Page 181 of RFC3530.
2014 */
2015 if (status == -NFS4ERR_BAD_STATEID) {
2016 exception.retry = 1;
2017 continue;
2018 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002019 if (status == -EAGAIN) {
2020 /* We must have found a delegation */
2021 exception.retry = 1;
2022 continue;
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2025 status, &exception));
2026 } while (exception.retry);
2027 return res;
2028}
2029
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002030static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2031 struct nfs_fattr *fattr, struct iattr *sattr,
2032 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002034 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002036 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 .iap = sattr,
2038 .server = server,
2039 .bitmask = server->attr_bitmask,
2040 };
2041 struct nfs_setattrres res = {
2042 .fattr = fattr,
2043 .server = server,
2044 };
2045 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2047 .rpc_argp = &arg,
2048 .rpc_resp = &res,
2049 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002051 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002052 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Trond Myklebust0e574af2005-10-27 22:12:38 -04002054 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Trond Myklebust4fc87962012-03-08 17:42:01 -05002056 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002057 struct nfs_lockowner lockowner = {
2058 .l_owner = current->files,
2059 .l_pid = current->tgid,
2060 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002061 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002062 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002063 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2064 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002065 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002066 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002067 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Bryan Schumaker7c513052011-03-24 17:12:24 +00002069 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002070 if (status == 0 && state != NULL)
2071 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002072 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002075static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2076 struct nfs_fattr *fattr, struct iattr *sattr,
2077 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002079 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002080 struct nfs4_exception exception = {
2081 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002082 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002083 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 int err;
2085 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002086 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2087 switch (err) {
2088 case -NFS4ERR_OPENMODE:
2089 if (state && !(state->state & FMODE_WRITE)) {
2090 err = -EBADF;
2091 if (sattr->ia_valid & ATTR_OPEN)
2092 err = -EACCES;
2093 goto out;
2094 }
2095 }
2096 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002098out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return err;
2100}
2101
2102struct nfs4_closedata {
2103 struct inode *inode;
2104 struct nfs4_state *state;
2105 struct nfs_closeargs arg;
2106 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002107 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002108 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002109 bool roc;
2110 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111};
2112
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002113static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002114{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002115 struct nfs4_closedata *calldata = data;
2116 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002117 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002118
Fred Isamanf7e89172011-01-06 11:36:32 +00002119 if (calldata->roc)
2120 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002121 nfs4_put_open_state(calldata->state);
2122 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002123 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002124 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002125 kfree(calldata);
2126}
2127
Trond Myklebust88069f72009-12-08 08:33:16 -05002128static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2129 fmode_t fmode)
2130{
2131 spin_lock(&state->owner->so_lock);
2132 if (!(fmode & FMODE_READ))
2133 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2134 if (!(fmode & FMODE_WRITE))
2135 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2136 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2137 spin_unlock(&state->owner->so_lock);
2138}
2139
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002140static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002142 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 struct nfs_server *server = NFS_SERVER(calldata->inode);
2145
Chuck Levera3ca5652012-03-01 17:00:40 -05002146 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002147 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2148 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 /* hmm. we are done with the inode, and in the process of freeing
2150 * the state_owner. we keep this around to process errors
2151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 switch (task->tk_status) {
2153 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002154 if (calldata->roc)
2155 pnfs_roc_set_barrier(state->inode,
2156 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002157 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002158 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002159 nfs4_close_clear_stateid_flags(state,
2160 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 break;
2162 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002163 case -NFS4ERR_OLD_STATEID:
2164 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002166 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002169 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2170 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002172 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002173 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002174 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002177static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002179 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002180 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002181 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002182 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002183
Chuck Levera3ca5652012-03-01 17:00:40 -05002184 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002185 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002186 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002187
Trond Myklebust88069f72009-12-08 08:33:16 -05002188 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2189 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002190 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002191 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002192 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002193 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002194 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2195 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2196 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002197 }
2198 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002199 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2200 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2201 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002202 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002203 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002204 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002205
2206 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002207 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002208 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002209 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002210
Fred Isamanf7e89172011-01-06 11:36:32 +00002211 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002212 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002213 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002214 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002215 goto out_wait;
Fred Isamanf7e89172011-01-06 11:36:32 +00002216 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002217
Trond Myklebust516a6af2005-10-27 22:12:41 -04002218 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002219 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002220 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002221 &calldata->arg.seq_args,
2222 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002223 task) != 0)
2224 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002225 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002226 return;
2227out_no_action:
2228 task->tk_action = NULL;
2229out_wait:
2230 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002233static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002234 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002235 .rpc_call_done = nfs4_close_done,
2236 .rpc_release = nfs4_free_closedata,
2237};
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239/*
2240 * It is possible for data to be read/written from a mem-mapped file
2241 * after the sys_close call (which hits the vfs layer as a flush).
2242 * This means that we can't safely call nfsv4 close on a file until
2243 * the inode is cleared. This in turn means that we are not good
2244 * NFSv4 citizens - we do not indicate to the server to update the file's
2245 * share state even when we are done with one of the three share
2246 * stateid's in the inode.
2247 *
2248 * NOTE: Caller must be holding the sp->so_owner semaphore!
2249 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002250int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002252 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002254 struct nfs4_state_owner *sp = state->owner;
2255 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002256 struct rpc_message msg = {
2257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2258 .rpc_cred = state->owner->so_cred,
2259 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002260 struct rpc_task_setup task_setup_data = {
2261 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002262 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002263 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002264 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002265 .flags = RPC_TASK_ASYNC,
2266 };
Trond Myklebust95121352005-10-18 14:20:12 -07002267 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002269 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002271 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002272 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002273 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002275 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002276 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002278 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002279 if (calldata->arg.seqid == NULL)
2280 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002281 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002282 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002283 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002284 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002285 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002286 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002287 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002288
Trond Myklebust1174dd12010-12-21 10:52:24 -05002289 msg.rpc_argp = &calldata->arg;
2290 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002291 task_setup_data.callback_data = calldata;
2292 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002293 if (IS_ERR(task))
2294 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002295 status = 0;
2296 if (wait)
2297 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002298 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002299 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002300out_free_calldata:
2301 kfree(calldata);
2302out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002303 nfs4_put_open_state(state);
2304 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002305 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
Trond Myklebust2b484292010-09-17 10:56:51 -04002308static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002309nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 struct nfs4_state *state;
2312
Trond Myklebust565277f2007-10-15 18:17:53 -04002313 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002314 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2315 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002316 if (IS_ERR(state))
2317 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002318 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002319 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Trond Myklebust1185a552009-12-03 15:54:02 -05002322static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002323{
2324 if (ctx->state == NULL)
2325 return;
2326 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002327 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002328 else
Al Viro643168c2011-06-22 18:20:23 -04002329 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2333{
Benny Halevy43652ad2009-04-01 09:21:54 -04002334 struct nfs4_server_caps_arg args = {
2335 .fhandle = fhandle,
2336 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 struct nfs4_server_caps_res res = {};
2338 struct rpc_message msg = {
2339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002340 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 .rpc_resp = &res,
2342 };
2343 int status;
2344
Bryan Schumaker7c513052011-03-24 17:12:24 +00002345 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (status == 0) {
2347 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002348 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2349 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2350 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2351 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2352 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2354 server->caps |= NFS_CAP_ACLS;
2355 if (res.has_links != 0)
2356 server->caps |= NFS_CAP_HARDLINKS;
2357 if (res.has_symlinks != 0)
2358 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002359 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2360 server->caps |= NFS_CAP_FILEID;
2361 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2362 server->caps |= NFS_CAP_MODE;
2363 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2364 server->caps |= NFS_CAP_NLINK;
2365 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2366 server->caps |= NFS_CAP_OWNER;
2367 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2368 server->caps |= NFS_CAP_OWNER_GROUP;
2369 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2370 server->caps |= NFS_CAP_ATIME;
2371 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2372 server->caps |= NFS_CAP_CTIME;
2373 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2374 server->caps |= NFS_CAP_MTIME;
2375
Trond Myklebusta65318b2009-03-11 14:10:28 -04002376 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2377 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2378 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002380 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return status;
2384}
2385
Trond Myklebust55a97592006-06-09 09:34:19 -04002386int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
2388 struct nfs4_exception exception = { };
2389 int err;
2390 do {
2391 err = nfs4_handle_exception(server,
2392 _nfs4_server_capabilities(server, fhandle),
2393 &exception);
2394 } while (exception.retry);
2395 return err;
2396}
2397
2398static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2399 struct nfs_fsinfo *info)
2400{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 struct nfs4_lookup_root_arg args = {
2402 .bitmask = nfs4_fattr_bitmap,
2403 };
2404 struct nfs4_lookup_res res = {
2405 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002406 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 .fh = fhandle,
2408 };
2409 struct rpc_message msg = {
2410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2411 .rpc_argp = &args,
2412 .rpc_resp = &res,
2413 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002414
Trond Myklebust0e574af2005-10-27 22:12:38 -04002415 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002416 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
2418
2419static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2420 struct nfs_fsinfo *info)
2421{
2422 struct nfs4_exception exception = { };
2423 int err;
2424 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002425 err = _nfs4_lookup_root(server, fhandle, info);
2426 switch (err) {
2427 case 0:
2428 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002429 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002430 default:
2431 err = nfs4_handle_exception(server, err, &exception);
2432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002434out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return err;
2436}
2437
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002438static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2439 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2440{
2441 struct rpc_auth *auth;
2442 int ret;
2443
2444 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002445 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002446 ret = -EIO;
2447 goto out;
2448 }
2449 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002450out:
2451 return ret;
2452}
2453
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002454static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002455 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002457 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002458 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002460 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002461 if (len < 0)
2462 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002463
2464 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002465 /* AUTH_UNIX is the default flavor if none was specified,
2466 * thus has already been tried. */
2467 if (flav_array[i] == RPC_AUTH_UNIX)
2468 continue;
2469
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002470 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002471 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002472 continue;
2473 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002474 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002475 /*
2476 * -EACCESS could mean that the user doesn't have correct permissions
2477 * to access the mount. It could also mean that we tried to mount
2478 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2479 * existing mount programs don't handle -EACCES very well so it should
2480 * be mapped to -EPERM instead.
2481 */
2482 if (status == -EACCES)
2483 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002484 return status;
2485}
2486
2487/*
2488 * get the file handle for the "/" directory on the server
2489 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002490int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2491 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002492{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002493 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002494 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002495 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2496 /*
2497 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2498 * by nfs4_map_errors() as this function exits.
2499 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002500 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (status == 0)
2502 status = nfs4_server_capabilities(server, fhandle);
2503 if (status == 0)
2504 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002505 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Bryan Schumakerbae36242012-05-10 15:07:31 -04002508static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2509 struct nfs_fsinfo *info)
2510{
2511 int error;
2512 struct nfs_fattr *fattr = info->fattr;
2513
2514 error = nfs4_server_capabilities(server, mntfh);
2515 if (error < 0) {
2516 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2517 return error;
2518 }
2519
2520 error = nfs4_proc_getattr(server, mntfh, fattr);
2521 if (error < 0) {
2522 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2523 return error;
2524 }
2525
2526 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2527 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2528 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2529
2530 return error;
2531}
2532
Manoj Naik6b97fd32006-06-09 09:34:29 -04002533/*
2534 * Get locations and (maybe) other attributes of a referral.
2535 * Note that we'll actually follow the referral later when
2536 * we detect fsid mismatch in inode revalidation
2537 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002538static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2539 const struct qstr *name, struct nfs_fattr *fattr,
2540 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002541{
2542 int status = -ENOMEM;
2543 struct page *page = NULL;
2544 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002545
2546 page = alloc_page(GFP_KERNEL);
2547 if (page == NULL)
2548 goto out;
2549 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2550 if (locations == NULL)
2551 goto out;
2552
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002553 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002554 if (status != 0)
2555 goto out;
2556 /* Make sure server returned a different fsid for the referral */
2557 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002558 dprintk("%s: server did not return a different fsid for"
2559 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002560 status = -EIO;
2561 goto out;
2562 }
Andy Adamson533eb462011-06-13 18:25:56 -04002563 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2564 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002565
Andy Adamson533eb462011-06-13 18:25:56 -04002566 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002567 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002568 memset(fhandle, 0, sizeof(struct nfs_fh));
2569out:
2570 if (page)
2571 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002572 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002573 return status;
2574}
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2577{
2578 struct nfs4_getattr_arg args = {
2579 .fh = fhandle,
2580 .bitmask = server->attr_bitmask,
2581 };
2582 struct nfs4_getattr_res res = {
2583 .fattr = fattr,
2584 .server = server,
2585 };
2586 struct rpc_message msg = {
2587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2588 .rpc_argp = &args,
2589 .rpc_resp = &res,
2590 };
2591
Trond Myklebust0e574af2005-10-27 22:12:38 -04002592 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002593 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
2596static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2597{
2598 struct nfs4_exception exception = { };
2599 int err;
2600 do {
2601 err = nfs4_handle_exception(server,
2602 _nfs4_proc_getattr(server, fhandle, fattr),
2603 &exception);
2604 } while (exception.retry);
2605 return err;
2606}
2607
2608/*
2609 * The file is not closed if it is opened due to the a request to change
2610 * the size of the file. The open call will not be needed once the
2611 * VFS layer lookup-intents are implemented.
2612 *
2613 * Close is called when the inode is destroyed.
2614 * If we haven't opened the file for O_WRONLY, we
2615 * need to in the size_change case to obtain a stateid.
2616 *
2617 * Got race?
2618 * Because OPEN is always done by name in nfsv4, it is
2619 * possible that we opened a different file by the same
2620 * name. We can recognize this race condition, but we
2621 * can't do anything about it besides returning an error.
2622 *
2623 * This will be fixed with VFS changes (lookup-intent).
2624 */
2625static int
2626nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2627 struct iattr *sattr)
2628{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002629 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002630 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002631 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 int status;
2633
Benny Halevy8a1636c2010-07-14 15:43:57 -04002634 if (pnfs_ld_layoutret_on_setattr(inode))
2635 pnfs_return_layout(inode);
2636
Trond Myklebust0e574af2005-10-27 22:12:38 -04002637 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Andy Adamson26699402012-05-30 16:12:24 -04002639 /* Deal with open(O_TRUNC) */
2640 if (sattr->ia_valid & ATTR_OPEN)
2641 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2642
2643 /* Optimization: if the end result is no change, don't RPC */
2644 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2645 return 0;
2646
Trond Myklebustd5308382005-11-04 15:33:38 -05002647 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002648 if (sattr->ia_valid & ATTR_FILE) {
2649 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002650
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002651 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002652 if (ctx) {
2653 cred = ctx->cred;
2654 state = ctx->state;
2655 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002656 }
2657
2658 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002659 if (status == 0)
2660 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 return status;
2662}
2663
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002664static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2665 const struct qstr *name, struct nfs_fh *fhandle,
2666 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002667{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002668 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002669 int status;
2670 struct nfs4_lookup_arg args = {
2671 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002672 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002673 .name = name,
2674 };
2675 struct nfs4_lookup_res res = {
2676 .server = server,
2677 .fattr = fattr,
2678 .fh = fhandle,
2679 };
2680 struct rpc_message msg = {
2681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2682 .rpc_argp = &args,
2683 .rpc_resp = &res,
2684 };
2685
2686 nfs_fattr_init(fattr);
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002689 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 dprintk("NFS reply lookup: %d\n", status);
2691 return status;
2692}
2693
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002694static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002695{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002696 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002697 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002698 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2699 fattr->nlink = 2;
2700}
2701
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002702static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2703 struct qstr *name, struct nfs_fh *fhandle,
2704 struct nfs_fattr *fattr)
2705{
2706 struct nfs4_exception exception = { };
2707 struct rpc_clnt *client = *clnt;
2708 int err;
2709 do {
2710 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2711 switch (err) {
2712 case -NFS4ERR_BADNAME:
2713 err = -ENOENT;
2714 goto out;
2715 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002716 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002717 goto out;
2718 case -NFS4ERR_WRONGSEC:
2719 err = -EPERM;
2720 if (client != *clnt)
2721 goto out;
2722
2723 client = nfs4_create_sec_client(client, dir, name);
2724 if (IS_ERR(client))
2725 return PTR_ERR(client);
2726
2727 exception.retry = 1;
2728 break;
2729 default:
2730 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2731 }
2732 } while (exception.retry);
2733
2734out:
2735 if (err == 0)
2736 *clnt = client;
2737 else if (client != *clnt)
2738 rpc_shutdown_client(client);
2739
2740 return err;
2741}
2742
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002743static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002744 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002746 int status;
2747 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002748
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002749 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2750 if (client != NFS_CLIENT(dir)) {
2751 rpc_shutdown_client(client);
2752 nfs_fixup_secinfo_attributes(fattr);
2753 }
2754 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002757struct rpc_clnt *
2758nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2759 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2760{
2761 int status;
2762 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2763
2764 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2765 if (status < 0) {
2766 rpc_shutdown_client(client);
2767 return ERR_PTR(status);
2768 }
2769 return client;
2770}
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2773{
Trond Myklebust76b32992007-08-10 17:45:11 -04002774 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 struct nfs4_accessargs args = {
2776 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002777 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002779 struct nfs4_accessres res = {
2780 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002781 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 struct rpc_message msg = {
2783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2784 .rpc_argp = &args,
2785 .rpc_resp = &res,
2786 .rpc_cred = entry->cred,
2787 };
2788 int mode = entry->mask;
2789 int status;
2790
2791 /*
2792 * Determine which access bits we want to ask for...
2793 */
2794 if (mode & MAY_READ)
2795 args.access |= NFS4_ACCESS_READ;
2796 if (S_ISDIR(inode->i_mode)) {
2797 if (mode & MAY_WRITE)
2798 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2799 if (mode & MAY_EXEC)
2800 args.access |= NFS4_ACCESS_LOOKUP;
2801 } else {
2802 if (mode & MAY_WRITE)
2803 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2804 if (mode & MAY_EXEC)
2805 args.access |= NFS4_ACCESS_EXECUTE;
2806 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002807
2808 res.fattr = nfs_alloc_fattr();
2809 if (res.fattr == NULL)
2810 return -ENOMEM;
2811
Bryan Schumaker7c513052011-03-24 17:12:24 +00002812 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002814 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002815 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002817 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return status;
2819}
2820
2821static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2822{
2823 struct nfs4_exception exception = { };
2824 int err;
2825 do {
2826 err = nfs4_handle_exception(NFS_SERVER(inode),
2827 _nfs4_proc_access(inode, entry),
2828 &exception);
2829 } while (exception.retry);
2830 return err;
2831}
2832
2833/*
2834 * TODO: For the time being, we don't try to get any attributes
2835 * along with any of the zero-copy operations READ, READDIR,
2836 * READLINK, WRITE.
2837 *
2838 * In the case of the first three, we want to put the GETATTR
2839 * after the read-type operation -- this is because it is hard
2840 * to predict the length of a GETATTR response in v4, and thus
2841 * align the READ data correctly. This means that the GETATTR
2842 * may end up partially falling into the page cache, and we should
2843 * shift it into the 'tail' of the xdr_buf before processing.
2844 * To do this efficiently, we need to know the total length
2845 * of data received, which doesn't seem to be available outside
2846 * of the RPC layer.
2847 *
2848 * In the case of WRITE, we also want to put the GETATTR after
2849 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002850 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 *
2852 * Both of these changes to the XDR layer would in fact be quite
2853 * minor, but I decided to leave them for a subsequent patch.
2854 */
2855static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2856 unsigned int pgbase, unsigned int pglen)
2857{
2858 struct nfs4_readlink args = {
2859 .fh = NFS_FH(inode),
2860 .pgbase = pgbase,
2861 .pglen = pglen,
2862 .pages = &page,
2863 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002864 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct rpc_message msg = {
2866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2867 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002868 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 };
2870
Bryan Schumaker7c513052011-03-24 17:12:24 +00002871 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 -07002872}
2873
2874static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2875 unsigned int pgbase, unsigned int pglen)
2876{
2877 struct nfs4_exception exception = { };
2878 int err;
2879 do {
2880 err = nfs4_handle_exception(NFS_SERVER(inode),
2881 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2882 &exception);
2883 } while (exception.retry);
2884 return err;
2885}
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002888 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890static int
2891nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002892 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002894 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 int status = 0;
2897
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002898 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2899 if (IS_ERR(ctx))
2900 return PTR_ERR(ctx);
2901
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002902 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002903 state = nfs4_do_open(dir, dentry, ctx->mode,
2904 flags, sattr, ctx->cred,
2905 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002906 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (IS_ERR(state)) {
2908 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002911 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002912 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002913 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002915 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return status;
2917}
2918
2919static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2920{
Trond Myklebust16e42952005-10-27 22:12:44 -04002921 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002922 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002924 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002926 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002927 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002928 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2931 .rpc_argp = &args,
2932 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002934 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002935
Bryan Schumaker7c513052011-03-24 17:12:24 +00002936 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002937 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002938 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 return status;
2940}
2941
2942static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2943{
2944 struct nfs4_exception exception = { };
2945 int err;
2946 do {
2947 err = nfs4_handle_exception(NFS_SERVER(dir),
2948 _nfs4_proc_remove(dir, name),
2949 &exception);
2950 } while (exception.retry);
2951 return err;
2952}
2953
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002954static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002956 struct nfs_server *server = NFS_SERVER(dir);
2957 struct nfs_removeargs *args = msg->rpc_argp;
2958 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002960 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002962 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002965static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2966{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002967 nfs4_setup_sequence(NFS_SERVER(data->dir),
2968 &data->args.seq_args,
2969 &data->res.seq_res,
2970 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002973static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002975 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2976
Trond Myklebust14516c32010-07-31 14:29:06 -04002977 if (!nfs4_sequence_done(task, &res->seq_res))
2978 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002979 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002980 return 0;
2981 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002982 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
2984
Jeff Laytond3d41522010-09-17 17:31:57 -04002985static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2986{
2987 struct nfs_server *server = NFS_SERVER(dir);
2988 struct nfs_renameargs *arg = msg->rpc_argp;
2989 struct nfs_renameres *res = msg->rpc_resp;
2990
2991 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002992 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002993 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002994}
2995
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002996static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2997{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002998 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2999 &data->args.seq_args,
3000 &data->res.seq_res,
3001 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003002}
3003
3004static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3005 struct inode *new_dir)
3006{
3007 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3008
3009 if (!nfs4_sequence_done(task, &res->seq_res))
3010 return 0;
3011 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3012 return 0;
3013
3014 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003015 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003016 return 1;
3017}
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3020 struct inode *new_dir, struct qstr *new_name)
3021{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003022 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003023 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 .old_dir = NFS_FH(old_dir),
3025 .new_dir = NFS_FH(new_dir),
3026 .old_name = old_name,
3027 .new_name = new_name,
3028 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003029 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003030 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003031 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 struct rpc_message msg = {
3033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3034 .rpc_argp = &arg,
3035 .rpc_resp = &res,
3036 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003037 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Bryan Schumaker7c513052011-03-24 17:12:24 +00003039 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (!status) {
3041 update_changeattr(old_dir, &res.old_cinfo);
3042 update_changeattr(new_dir, &res.new_cinfo);
3043 }
3044 return status;
3045}
3046
3047static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3048 struct inode *new_dir, struct qstr *new_name)
3049{
3050 struct nfs4_exception exception = { };
3051 int err;
3052 do {
3053 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3054 _nfs4_proc_rename(old_dir, old_name,
3055 new_dir, new_name),
3056 &exception);
3057 } while (exception.retry);
3058 return err;
3059}
3060
3061static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3062{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003063 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 struct nfs4_link_arg arg = {
3065 .fh = NFS_FH(inode),
3066 .dir_fh = NFS_FH(dir),
3067 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003068 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003070 struct nfs4_link_res res = {
3071 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003072 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 struct rpc_message msg = {
3074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3075 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003076 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003078 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Trond Myklebust136f2622010-04-16 16:22:49 -04003080 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003081 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003082 goto out;
3083
Bryan Schumaker7c513052011-03-24 17:12:24 +00003084 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003085 if (!status) {
3086 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003087 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003088 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003089out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003090 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return status;
3092}
3093
3094static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3095{
3096 struct nfs4_exception exception = { };
3097 int err;
3098 do {
3099 err = nfs4_handle_exception(NFS_SERVER(inode),
3100 _nfs4_proc_link(inode, dir, name),
3101 &exception);
3102 } while (exception.retry);
3103 return err;
3104}
3105
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003106struct nfs4_createdata {
3107 struct rpc_message msg;
3108 struct nfs4_create_arg arg;
3109 struct nfs4_create_res res;
3110 struct nfs_fh fh;
3111 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112};
3113
3114static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3115 struct qstr *name, struct iattr *sattr, u32 ftype)
3116{
3117 struct nfs4_createdata *data;
3118
3119 data = kzalloc(sizeof(*data), GFP_KERNEL);
3120 if (data != NULL) {
3121 struct nfs_server *server = NFS_SERVER(dir);
3122
3123 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3124 data->msg.rpc_argp = &data->arg;
3125 data->msg.rpc_resp = &data->res;
3126 data->arg.dir_fh = NFS_FH(dir);
3127 data->arg.server = server;
3128 data->arg.name = name;
3129 data->arg.attrs = sattr;
3130 data->arg.ftype = ftype;
3131 data->arg.bitmask = server->attr_bitmask;
3132 data->res.server = server;
3133 data->res.fh = &data->fh;
3134 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003135 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003136 }
3137 return data;
3138}
3139
3140static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3141{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003142 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003143 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003144 if (status == 0) {
3145 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003146 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3147 }
3148 return status;
3149}
3150
3151static void nfs4_free_createdata(struct nfs4_createdata *data)
3152{
3153 kfree(data);
3154}
3155
Chuck Lever4f390c12006-08-22 20:06:22 -04003156static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003157 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003159 struct nfs4_createdata *data;
3160 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Chuck Lever94a6d752006-08-22 20:06:23 -04003162 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003163 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003164
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003165 status = -ENOMEM;
3166 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3167 if (data == NULL)
3168 goto out;
3169
3170 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3171 data->arg.u.symlink.pages = &page;
3172 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003174 status = nfs4_do_create(dir, dentry, data);
3175
3176 nfs4_free_createdata(data);
3177out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 return status;
3179}
3180
Chuck Lever4f390c12006-08-22 20:06:22 -04003181static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003182 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
3184 struct nfs4_exception exception = { };
3185 int err;
3186 do {
3187 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003188 _nfs4_proc_symlink(dir, dentry, page,
3189 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 &exception);
3191 } while (exception.retry);
3192 return err;
3193}
3194
3195static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3196 struct iattr *sattr)
3197{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003198 struct nfs4_createdata *data;
3199 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003201 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3202 if (data == NULL)
3203 goto out;
3204
3205 status = nfs4_do_create(dir, dentry, data);
3206
3207 nfs4_free_createdata(data);
3208out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 return status;
3210}
3211
3212static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3213 struct iattr *sattr)
3214{
3215 struct nfs4_exception exception = { };
3216 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003217
3218 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 do {
3220 err = nfs4_handle_exception(NFS_SERVER(dir),
3221 _nfs4_proc_mkdir(dir, dentry, sattr),
3222 &exception);
3223 } while (exception.retry);
3224 return err;
3225}
3226
3227static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003228 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
3230 struct inode *dir = dentry->d_inode;
3231 struct nfs4_readdir_arg args = {
3232 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003233 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 .pgbase = 0,
3235 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003236 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003237 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 };
3239 struct nfs4_readdir_res res;
3240 struct rpc_message msg = {
3241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3242 .rpc_argp = &args,
3243 .rpc_resp = &res,
3244 .rpc_cred = cred,
3245 };
3246 int status;
3247
Harvey Harrison3110ff82008-05-02 13:42:44 -07003248 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003249 dentry->d_parent->d_name.name,
3250 dentry->d_name.name,
3251 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003252 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003254 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 -05003255 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003256 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003257 status += args.pgbase;
3258 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003259
3260 nfs_invalidate_atime(dir);
3261
Harvey Harrison3110ff82008-05-02 13:42:44 -07003262 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return status;
3264}
3265
3266static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003267 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
3269 struct nfs4_exception exception = { };
3270 int err;
3271 do {
3272 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3273 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003274 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 &exception);
3276 } while (exception.retry);
3277 return err;
3278}
3279
3280static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3281 struct iattr *sattr, dev_t rdev)
3282{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003283 struct nfs4_createdata *data;
3284 int mode = sattr->ia_mode;
3285 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003287 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3288 if (data == NULL)
3289 goto out;
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003292 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003294 data->arg.ftype = NF4BLK;
3295 data->arg.u.device.specdata1 = MAJOR(rdev);
3296 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 }
3298 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003299 data->arg.ftype = NF4CHR;
3300 data->arg.u.device.specdata1 = MAJOR(rdev);
3301 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003302 } else if (!S_ISSOCK(mode)) {
3303 status = -EINVAL;
3304 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003307 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003308out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003309 nfs4_free_createdata(data);
3310out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 return status;
3312}
3313
3314static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3315 struct iattr *sattr, dev_t rdev)
3316{
3317 struct nfs4_exception exception = { };
3318 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003319
3320 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 do {
3322 err = nfs4_handle_exception(NFS_SERVER(dir),
3323 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3324 &exception);
3325 } while (exception.retry);
3326 return err;
3327}
3328
3329static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3330 struct nfs_fsstat *fsstat)
3331{
3332 struct nfs4_statfs_arg args = {
3333 .fh = fhandle,
3334 .bitmask = server->attr_bitmask,
3335 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003336 struct nfs4_statfs_res res = {
3337 .fsstat = fsstat,
3338 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 struct rpc_message msg = {
3340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3341 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003342 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 };
3344
Trond Myklebust0e574af2005-10-27 22:12:38 -04003345 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003346 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
3349static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3350{
3351 struct nfs4_exception exception = { };
3352 int err;
3353 do {
3354 err = nfs4_handle_exception(server,
3355 _nfs4_proc_statfs(server, fhandle, fsstat),
3356 &exception);
3357 } while (exception.retry);
3358 return err;
3359}
3360
3361static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3362 struct nfs_fsinfo *fsinfo)
3363{
3364 struct nfs4_fsinfo_arg args = {
3365 .fh = fhandle,
3366 .bitmask = server->attr_bitmask,
3367 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003368 struct nfs4_fsinfo_res res = {
3369 .fsinfo = fsinfo,
3370 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 struct rpc_message msg = {
3372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3373 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003374 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 };
3376
Bryan Schumaker7c513052011-03-24 17:12:24 +00003377 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
3380static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3381{
3382 struct nfs4_exception exception = { };
3383 int err;
3384
3385 do {
3386 err = nfs4_handle_exception(server,
3387 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3388 &exception);
3389 } while (exception.retry);
3390 return err;
3391}
3392
3393static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3394{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003395 int error;
3396
Trond Myklebust0e574af2005-10-27 22:12:38 -04003397 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003398 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003399 if (error == 0) {
3400 /* block layout checks this! */
3401 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003402 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003403 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003404
3405 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406}
3407
3408static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3409 struct nfs_pathconf *pathconf)
3410{
3411 struct nfs4_pathconf_arg args = {
3412 .fh = fhandle,
3413 .bitmask = server->attr_bitmask,
3414 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003415 struct nfs4_pathconf_res res = {
3416 .pathconf = pathconf,
3417 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 struct rpc_message msg = {
3419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3420 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003421 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 };
3423
3424 /* None of the pathconf attributes are mandatory to implement */
3425 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3426 memset(pathconf, 0, sizeof(*pathconf));
3427 return 0;
3428 }
3429
Trond Myklebust0e574af2005-10-27 22:12:38 -04003430 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003431 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432}
3433
3434static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3435 struct nfs_pathconf *pathconf)
3436{
3437 struct nfs4_exception exception = { };
3438 int err;
3439
3440 do {
3441 err = nfs4_handle_exception(server,
3442 _nfs4_proc_pathconf(server, fhandle, pathconf),
3443 &exception);
3444 } while (exception.retry);
3445 return err;
3446}
3447
Benny Halevyd20581a2011-05-22 19:52:03 +03003448void __nfs4_read_done_cb(struct nfs_read_data *data)
3449{
Fred Isamancd841602012-04-20 14:47:44 -04003450 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003451}
3452
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003453static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
Fred Isamancd841602012-04-20 14:47:44 -04003455 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003457 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003458 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003459 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003461
Benny Halevyd20581a2011-05-22 19:52:03 +03003462 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003464 renew_lease(server, data->timestamp);
3465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466}
3467
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003468static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3469{
3470
3471 dprintk("--> %s\n", __func__);
3472
3473 if (!nfs4_sequence_done(task, &data->res.seq_res))
3474 return -EAGAIN;
3475
Benny Halevyd20581a2011-05-22 19:52:03 +03003476 return data->read_done_cb ? data->read_done_cb(task, data) :
3477 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003478}
3479
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003480static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003483 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003484 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003485 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003488static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3489{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003490 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3491 &data->args.seq_args,
3492 &data->res.seq_res,
3493 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
3495
Fred Isamanb029bc92011-03-03 15:13:42 +00003496static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
Fred Isamancd841602012-04-20 14:47:44 -04003498 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003500 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003501 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003502 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003504 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003506 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003507 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509}
3510
Fred Isamanb029bc92011-03-03 15:13:42 +00003511static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3512{
3513 if (!nfs4_sequence_done(task, &data->res.seq_res))
3514 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003515 return data->write_done_cb ? data->write_done_cb(task, data) :
3516 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003517}
3518
Trond Myklebust5a37f852012-04-28 14:55:16 -04003519static
3520bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003521{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003522 const struct nfs_pgio_header *hdr = data->header;
3523
3524 /* Don't request attributes for pNFS or O_DIRECT writes */
3525 if (data->ds_clp != NULL || hdr->dreq != NULL)
3526 return false;
3527 /* Otherwise, request attributes if and only if we don't hold
3528 * a delegation
3529 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003530 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003531}
Fred Isamana69aef12011-03-03 15:13:47 +00003532
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003533static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
Fred Isamancd841602012-04-20 14:47:44 -04003535 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003536
Trond Myklebust5a37f852012-04-28 14:55:16 -04003537 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003538 data->args.bitmask = NULL;
3539 data->res.fattr = NULL;
3540 } else
3541 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003542
Fred Isamanb029bc92011-03-03 15:13:42 +00003543 if (!data->write_done_cb)
3544 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003545 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 data->timestamp = jiffies;
3547
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003548 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003549 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550}
3551
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003552static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3553{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003554 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3555 &data->args.seq_args,
3556 &data->res.seq_res,
3557 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558}
3559
Fred Isaman0b7c0152012-04-20 14:47:39 -04003560static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3561{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003562 nfs4_setup_sequence(NFS_SERVER(data->inode),
3563 &data->args.seq_args,
3564 &data->res.seq_res,
3565 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
Fred Isaman0b7c0152012-04-20 14:47:39 -04003568static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003571
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003572 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003573 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003574 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Fred Isaman0b7c0152012-04-20 14:47:39 -04003579static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003580{
3581 if (!nfs4_sequence_done(task, &data->res.seq_res))
3582 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003583 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003584}
3585
Fred Isaman0b7c0152012-04-20 14:47:39 -04003586static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003588 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003589
Fred Isaman0b7c0152012-04-20 14:47:39 -04003590 if (data->commit_done_cb == NULL)
3591 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003592 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003593 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003594 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003597struct nfs4_renewdata {
3598 struct nfs_client *client;
3599 unsigned long timestamp;
3600};
3601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602/*
3603 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3604 * standalone procedure for queueing an asynchronous RENEW.
3605 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003606static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003607{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003608 struct nfs4_renewdata *data = calldata;
3609 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003610
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003611 if (atomic_read(&clp->cl_count) > 1)
3612 nfs4_schedule_state_renewal(clp);
3613 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003614 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003615}
3616
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003617static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003619 struct nfs4_renewdata *data = calldata;
3620 struct nfs_client *clp = data->client;
3621 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
3623 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003624 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003625 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3626 return;
3627 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003628 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003629 return;
3630 }
3631 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003633 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634}
3635
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003636static const struct rpc_call_ops nfs4_renew_ops = {
3637 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003638 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003639};
3640
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003641static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
3643 struct rpc_message msg = {
3644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3645 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003646 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003648 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003650 if (renew_flags == 0)
3651 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003652 if (!atomic_inc_not_zero(&clp->cl_count))
3653 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003654 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003655 if (data == NULL)
3656 return -ENOMEM;
3657 data->client = clp;
3658 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003660 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661}
3662
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003663static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
3665 struct rpc_message msg = {
3666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3667 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003668 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 };
3670 unsigned long now = jiffies;
3671 int status;
3672
3673 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3674 if (status < 0)
3675 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003676 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return 0;
3678}
3679
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003680static inline int nfs4_server_supports_acls(struct nfs_server *server)
3681{
3682 return (server->caps & NFS_CAP_ACLS)
3683 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3684 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3685}
3686
Trond Myklebust21f498c2012-08-24 10:59:25 -04003687/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3688 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003689 * the stack.
3690 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003691#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003692
Neil Hormane9e3d722011-03-04 19:26:03 -05003693static int buf_to_pages_noslab(const void *buf, size_t buflen,
3694 struct page **pages, unsigned int *pgbase)
3695{
3696 struct page *newpage, **spages;
3697 int rc = 0;
3698 size_t len;
3699 spages = pages;
3700
3701 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003702 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003703 newpage = alloc_page(GFP_KERNEL);
3704
3705 if (newpage == NULL)
3706 goto unwind;
3707 memcpy(page_address(newpage), buf, len);
3708 buf += len;
3709 buflen -= len;
3710 *pages++ = newpage;
3711 rc++;
3712 } while (buflen != 0);
3713
3714 return rc;
3715
3716unwind:
3717 for(; rc > 0; rc--)
3718 __free_page(spages[rc-1]);
3719 return -ENOMEM;
3720}
3721
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003722struct nfs4_cached_acl {
3723 int cached;
3724 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003725 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003726};
3727
3728static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003729{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003730 struct nfs_inode *nfsi = NFS_I(inode);
3731
3732 spin_lock(&inode->i_lock);
3733 kfree(nfsi->nfs4_acl);
3734 nfsi->nfs4_acl = acl;
3735 spin_unlock(&inode->i_lock);
3736}
3737
3738static void nfs4_zap_acl_attr(struct inode *inode)
3739{
3740 nfs4_set_cached_acl(inode, NULL);
3741}
3742
3743static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3744{
3745 struct nfs_inode *nfsi = NFS_I(inode);
3746 struct nfs4_cached_acl *acl;
3747 int ret = -ENOENT;
3748
3749 spin_lock(&inode->i_lock);
3750 acl = nfsi->nfs4_acl;
3751 if (acl == NULL)
3752 goto out;
3753 if (buf == NULL) /* user is just asking for length */
3754 goto out_len;
3755 if (acl->cached == 0)
3756 goto out;
3757 ret = -ERANGE; /* see getxattr(2) man page */
3758 if (acl->len > buflen)
3759 goto out;
3760 memcpy(buf, acl->data, acl->len);
3761out_len:
3762 ret = acl->len;
3763out:
3764 spin_unlock(&inode->i_lock);
3765 return ret;
3766}
3767
Sachin Prabhu5794d212012-04-17 14:36:40 +01003768static 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 +00003769{
3770 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003771 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003772
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003773 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003774 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003775 if (acl == NULL)
3776 goto out;
3777 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003778 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003779 } else {
3780 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3781 if (acl == NULL)
3782 goto out;
3783 acl->cached = 0;
3784 }
3785 acl->len = acl_len;
3786out:
3787 nfs4_set_cached_acl(inode, acl);
3788}
3789
Andy Adamsonbf118a32011-12-07 11:55:27 -05003790/*
3791 * The getxattr API returns the required buffer length when called with a
3792 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3793 * the required buf. On a NULL buf, we send a page of data to the server
3794 * guessing that the ACL request can be serviced by a page. If so, we cache
3795 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3796 * the cache. If not so, we throw away the page, and cache the required
3797 * length. The next getxattr call will then produce another round trip to
3798 * the server, this time with the input buf of the required size.
3799 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003800static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003801{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003802 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003803 struct nfs_getaclargs args = {
3804 .fh = NFS_FH(inode),
3805 .acl_pages = pages,
3806 .acl_len = buflen,
3807 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003808 struct nfs_getaclres res = {
3809 .acl_len = buflen,
3810 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003811 struct rpc_message msg = {
3812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3813 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003814 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003815 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003816 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3817 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003818
Andy Adamsonbf118a32011-12-07 11:55:27 -05003819 /* As long as we're doing a round trip to the server anyway,
3820 * let's be prepared for a page of acl data. */
3821 if (npages == 0)
3822 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003823 if (npages > ARRAY_SIZE(pages))
3824 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003825
Andy Adamsonbf118a32011-12-07 11:55:27 -05003826 for (i = 0; i < npages; i++) {
3827 pages[i] = alloc_page(GFP_KERNEL);
3828 if (!pages[i])
3829 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003830 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003831
3832 /* for decoding across pages */
3833 res.acl_scratch = alloc_page(GFP_KERNEL);
3834 if (!res.acl_scratch)
3835 goto out_free;
3836
Andy Adamsonbf118a32011-12-07 11:55:27 -05003837 args.acl_len = npages * PAGE_SIZE;
3838 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003839
Peng Taode040be2012-01-10 22:42:47 +08003840 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003841 __func__, buf, buflen, npages, args.acl_len);
3842 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3843 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003844 if (ret)
3845 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003846
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003847 /* Handle the case where the passed-in buffer is too short */
3848 if (res.acl_flags & NFS4_ACL_TRUNC) {
3849 /* Did the user only issue a request for the acl length? */
3850 if (buf == NULL)
3851 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003852 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003853 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003854 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003855 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003856 if (buf) {
3857 if (res.acl_len > buflen) {
3858 ret = -ERANGE;
3859 goto out_free;
3860 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003861 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003862 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003863out_ok:
3864 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003865out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003866 for (i = 0; i < npages; i++)
3867 if (pages[i])
3868 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003869 if (res.acl_scratch)
3870 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003871 return ret;
3872}
3873
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003874static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3875{
3876 struct nfs4_exception exception = { };
3877 ssize_t ret;
3878 do {
3879 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3880 if (ret >= 0)
3881 break;
3882 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3883 } while (exception.retry);
3884 return ret;
3885}
3886
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003887static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3888{
3889 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003890 int ret;
3891
3892 if (!nfs4_server_supports_acls(server))
3893 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003894 ret = nfs_revalidate_inode(server, inode);
3895 if (ret < 0)
3896 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003897 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3898 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003899 ret = nfs4_read_cached_acl(inode, buf, buflen);
3900 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003901 /* -ENOENT is returned if there is no ACL or if there is an ACL
3902 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003903 return ret;
3904 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003905}
3906
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003907static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003908{
3909 struct nfs_server *server = NFS_SERVER(inode);
3910 struct page *pages[NFS4ACL_MAXPAGES];
3911 struct nfs_setaclargs arg = {
3912 .fh = NFS_FH(inode),
3913 .acl_pages = pages,
3914 .acl_len = buflen,
3915 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003916 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003917 struct rpc_message msg = {
3918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3919 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003920 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003921 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003922 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003923 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003924
3925 if (!nfs4_server_supports_acls(server))
3926 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003927 if (npages > ARRAY_SIZE(pages))
3928 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003929 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3930 if (i < 0)
3931 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003932 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003933 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003934
3935 /*
3936 * Free each page after tx, so the only ref left is
3937 * held by the network stack
3938 */
3939 for (; i > 0; i--)
3940 put_page(pages[i-1]);
3941
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003942 /*
3943 * Acl update can result in inode attribute update.
3944 * so mark the attribute cache invalid.
3945 */
3946 spin_lock(&inode->i_lock);
3947 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3948 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003949 nfs_access_zap_cache(inode);
3950 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003951 return ret;
3952}
3953
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003954static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3955{
3956 struct nfs4_exception exception = { };
3957 int err;
3958 do {
3959 err = nfs4_handle_exception(NFS_SERVER(inode),
3960 __nfs4_proc_set_acl(inode, buf, buflen),
3961 &exception);
3962 } while (exception.retry);
3963 return err;
3964}
3965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003967nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003969 struct nfs_client *clp = server->nfs_client;
3970
3971 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 return 0;
3973 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003974 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003975 case -NFS4ERR_ADMIN_REVOKED:
3976 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003977 if (state == NULL)
3978 break;
3979 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003980 case -NFS4ERR_OPENMODE:
3981 if (state == NULL)
3982 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003983 nfs4_schedule_stateid_recovery(server, state);
3984 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003985 case -NFS4ERR_EXPIRED:
3986 if (state != NULL)
3987 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003989 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003990 nfs4_schedule_lease_recovery(clp);
3991 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003992#if defined(CONFIG_NFS_V4_1)
3993 case -NFS4ERR_BADSESSION:
3994 case -NFS4ERR_BADSLOT:
3995 case -NFS4ERR_BAD_HIGH_SLOT:
3996 case -NFS4ERR_DEADSESSION:
3997 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3998 case -NFS4ERR_SEQ_FALSE_RETRY:
3999 case -NFS4ERR_SEQ_MISORDERED:
4000 dprintk("%s ERROR %d, Reset session\n", __func__,
4001 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004002 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004003 task->tk_status = 0;
4004 return -EAGAIN;
4005#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004007 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004008 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4010 task->tk_status = 0;
4011 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004012 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 case -NFS4ERR_OLD_STATEID:
4014 task->tk_status = 0;
4015 return -EAGAIN;
4016 }
4017 task->tk_status = nfs4_map_errors(task->tk_status);
4018 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004019wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004020 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004021 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4022 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4023 task->tk_status = 0;
4024 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025}
4026
Chuck Leverf0920752012-05-21 22:45:41 -04004027static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4028 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004029{
4030 __be32 verf[2];
4031
Chuck Lever2c820d92012-05-21 22:45:33 -04004032 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4033 /* An impossible timestamp guarantees this value
4034 * will never match a generated boot time. */
4035 verf[0] = 0;
4036 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4037 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004038 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4039 verf[0] = (__be32)nn->boot_time.tv_sec;
4040 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004041 }
Chuck Levercd937102012-03-02 17:14:31 -05004042 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4043}
4044
Chuck Levere984a552012-09-14 17:24:21 -04004045static unsigned int
4046nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4047 char *buf, size_t len)
4048{
4049 unsigned int result;
4050
4051 rcu_read_lock();
4052 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4053 clp->cl_ipaddr,
4054 rpc_peeraddr2str(clp->cl_rpcclient,
4055 RPC_DISPLAY_ADDR),
4056 rpc_peeraddr2str(clp->cl_rpcclient,
4057 RPC_DISPLAY_PROTO));
4058 rcu_read_unlock();
4059 return result;
4060}
4061
4062static unsigned int
4063nfs4_init_uniform_client_string(const struct nfs_client *clp,
4064 char *buf, size_t len)
4065{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004066 char *nodename = clp->cl_rpcclient->cl_nodename;
4067
4068 if (nfs4_client_id_uniquifier[0] != '\0')
4069 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004070 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4071 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004072 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004073}
4074
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004075/**
4076 * nfs4_proc_setclientid - Negotiate client ID
4077 * @clp: state data structure
4078 * @program: RPC program for NFSv4 callback service
4079 * @port: IP port number for NFS4 callback service
4080 * @cred: RPC credential to use for this call
4081 * @res: where to place the result
4082 *
4083 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4084 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004085int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4086 unsigned short port, struct rpc_cred *cred,
4087 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088{
4089 nfs4_verifier sc_verifier;
4090 struct nfs4_setclientid setclientid = {
4091 .sc_verifier = &sc_verifier,
4092 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004093 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 };
4095 struct rpc_message msg = {
4096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4097 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004098 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004099 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004101 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Chuck Leverde734832012-07-11 16:30:50 -04004103 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004104 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004105 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4106 setclientid.sc_name_len =
4107 nfs4_init_uniform_client_string(clp,
4108 setclientid.sc_name,
4109 sizeof(setclientid.sc_name));
4110 else
4111 setclientid.sc_name_len =
4112 nfs4_init_nonuniform_client_string(clp,
4113 setclientid.sc_name,
4114 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004115 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004116 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004117 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004118 sizeof(setclientid.sc_netid),
4119 rpc_peeraddr2str(clp->cl_rpcclient,
4120 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004121 rcu_read_unlock();
4122 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004123 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 clp->cl_ipaddr, port >> 8, port & 255);
4125
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004126 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4127 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4128 setclientid.sc_name_len, setclientid.sc_name);
4129 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4130 dprintk("NFS reply setclientid: %d\n", status);
4131 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132}
4133
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004134/**
4135 * nfs4_proc_setclientid_confirm - Confirm client ID
4136 * @clp: state data structure
4137 * @res: result of a previous SETCLIENTID
4138 * @cred: RPC credential to use for this call
4139 *
4140 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4141 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004142int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004143 struct nfs4_setclientid_res *arg,
4144 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145{
4146 struct nfs_fsinfo fsinfo;
4147 struct rpc_message msg = {
4148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004149 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004151 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 };
4153 unsigned long now;
4154 int status;
4155
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004156 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4157 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4158 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004160 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 if (status == 0) {
4162 spin_lock(&clp->cl_lock);
4163 clp->cl_lease_time = fsinfo.lease_time * HZ;
4164 clp->cl_last_renewal = now;
4165 spin_unlock(&clp->cl_lock);
4166 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004167 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 return status;
4169}
4170
Trond Myklebustfe650402006-01-03 09:55:18 +01004171struct nfs4_delegreturndata {
4172 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004173 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004174 struct nfs_fh fh;
4175 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004176 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004177 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004178 int rpc_status;
4179};
4180
Trond Myklebustfe650402006-01-03 09:55:18 +01004181static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4182{
4183 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004184
Trond Myklebust14516c32010-07-31 14:29:06 -04004185 if (!nfs4_sequence_done(task, &data->res.seq_res))
4186 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004187
Ricardo Labiaga79708862009-12-07 09:23:21 -05004188 switch (task->tk_status) {
4189 case -NFS4ERR_STALE_STATEID:
4190 case -NFS4ERR_EXPIRED:
4191 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004192 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004193 break;
4194 default:
4195 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4196 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004197 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004198 return;
4199 }
4200 }
4201 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004202}
4203
4204static void nfs4_delegreturn_release(void *calldata)
4205{
Trond Myklebustfe650402006-01-03 09:55:18 +01004206 kfree(calldata);
4207}
4208
Andy Adamson938e1012009-04-01 09:22:28 -04004209#if defined(CONFIG_NFS_V4_1)
4210static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4211{
4212 struct nfs4_delegreturndata *d_data;
4213
4214 d_data = (struct nfs4_delegreturndata *)data;
4215
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004216 nfs4_setup_sequence(d_data->res.server,
4217 &d_data->args.seq_args,
4218 &d_data->res.seq_res,
4219 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004220}
4221#endif /* CONFIG_NFS_V4_1 */
4222
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004223static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004224#if defined(CONFIG_NFS_V4_1)
4225 .rpc_call_prepare = nfs4_delegreturn_prepare,
4226#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004227 .rpc_call_done = nfs4_delegreturn_done,
4228 .rpc_release = nfs4_delegreturn_release,
4229};
4230
Trond Myklebuste6f81072008-01-24 18:14:34 -05004231static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004232{
4233 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004234 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004235 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004236 struct rpc_message msg = {
4237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4238 .rpc_cred = cred,
4239 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004240 struct rpc_task_setup task_setup_data = {
4241 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004242 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004243 .callback_ops = &nfs4_delegreturn_ops,
4244 .flags = RPC_TASK_ASYNC,
4245 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004246 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004247
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004248 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004249 if (data == NULL)
4250 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004251 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004252 data->args.fhandle = &data->fh;
4253 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004254 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004255 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004256 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004257 data->res.fattr = &data->fattr;
4258 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004259 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004260 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004261 data->rpc_status = 0;
4262
Trond Myklebustc970aa82007-07-14 15:39:59 -04004263 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004264 msg.rpc_argp = &data->args;
4265 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004266 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004267 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004268 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004269 if (!issync)
4270 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004271 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004272 if (status != 0)
4273 goto out;
4274 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004275 if (status == 0)
4276 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4277 else
4278 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004279out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004280 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004281 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282}
4283
Trond Myklebuste6f81072008-01-24 18:14:34 -05004284int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285{
4286 struct nfs_server *server = NFS_SERVER(inode);
4287 struct nfs4_exception exception = { };
4288 int err;
4289 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004290 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 switch (err) {
4292 case -NFS4ERR_STALE_STATEID:
4293 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 case 0:
4295 return 0;
4296 }
4297 err = nfs4_handle_exception(server, err, &exception);
4298 } while (exception.retry);
4299 return err;
4300}
4301
4302#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4303#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4304
4305/*
4306 * sleep, with exponential backoff, and retry the LOCK operation.
4307 */
4308static unsigned long
4309nfs4_set_lock_task_retry(unsigned long timeout)
4310{
Jeff Laytond3103102011-12-01 22:44:39 +01004311 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 timeout <<= 1;
4313 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4314 return NFS4_LOCK_MAXTIMEOUT;
4315 return timeout;
4316}
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4319{
4320 struct inode *inode = state->inode;
4321 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004322 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004323 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004325 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004327 struct nfs_lockt_res res = {
4328 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 };
4330 struct rpc_message msg = {
4331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4332 .rpc_argp = &arg,
4333 .rpc_resp = &res,
4334 .rpc_cred = state->owner->so_cred,
4335 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 struct nfs4_lock_state *lsp;
4337 int status;
4338
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004339 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004340 status = nfs4_set_lock_state(state, request);
4341 if (status != 0)
4342 goto out;
4343 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004344 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004345 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004346 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004347 switch (status) {
4348 case 0:
4349 request->fl_type = F_UNLCK;
4350 break;
4351 case -NFS4ERR_DENIED:
4352 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004354 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004355out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 return status;
4357}
4358
4359static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4360{
4361 struct nfs4_exception exception = { };
4362 int err;
4363
4364 do {
4365 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4366 _nfs4_proc_getlk(state, cmd, request),
4367 &exception);
4368 } while (exception.retry);
4369 return err;
4370}
4371
4372static int do_vfs_lock(struct file *file, struct file_lock *fl)
4373{
4374 int res = 0;
4375 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4376 case FL_POSIX:
4377 res = posix_lock_file_wait(file, fl);
4378 break;
4379 case FL_FLOCK:
4380 res = flock_lock_file_wait(file, fl);
4381 break;
4382 default:
4383 BUG();
4384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 return res;
4386}
4387
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004388struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004389 struct nfs_locku_args arg;
4390 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004391 struct nfs4_lock_state *lsp;
4392 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004393 struct file_lock fl;
4394 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004395 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004396};
4397
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004398static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4399 struct nfs_open_context *ctx,
4400 struct nfs4_lock_state *lsp,
4401 struct nfs_seqid *seqid)
4402{
4403 struct nfs4_unlockdata *p;
4404 struct inode *inode = lsp->ls_state->inode;
4405
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004406 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004407 if (p == NULL)
4408 return NULL;
4409 p->arg.fh = NFS_FH(inode);
4410 p->arg.fl = &p->fl;
4411 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004412 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004413 p->arg.stateid = &lsp->ls_stateid;
4414 p->lsp = lsp;
4415 atomic_inc(&lsp->ls_count);
4416 /* Ensure we don't close file until we're done freeing locks! */
4417 p->ctx = get_nfs_open_context(ctx);
4418 memcpy(&p->fl, fl, sizeof(p->fl));
4419 p->server = NFS_SERVER(inode);
4420 return p;
4421}
4422
Trond Myklebust06f814a2006-01-03 09:55:07 +01004423static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004424{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004425 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004426 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004427 nfs4_put_lock_state(calldata->lsp);
4428 put_nfs_open_context(calldata->ctx);
4429 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004430}
4431
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004432static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004433{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004434 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004435
Trond Myklebust14516c32010-07-31 14:29:06 -04004436 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4437 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004438 switch (task->tk_status) {
4439 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004440 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4441 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004442 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004443 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004444 case -NFS4ERR_BAD_STATEID:
4445 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004446 case -NFS4ERR_STALE_STATEID:
4447 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004448 break;
4449 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004450 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004451 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004452 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004453 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004454}
4455
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004456static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004457{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004458 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004460 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004461 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004462 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004463 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004464 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004465 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004466 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004467 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004468 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004469 &calldata->res.seq_res,
4470 task) != 0)
4471 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004472 return;
4473out_no_action:
4474 task->tk_action = NULL;
4475out_wait:
4476 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477}
4478
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004479static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004480 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004481 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004482 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004483};
4484
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004485static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4486 struct nfs_open_context *ctx,
4487 struct nfs4_lock_state *lsp,
4488 struct nfs_seqid *seqid)
4489{
4490 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004491 struct rpc_message msg = {
4492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4493 .rpc_cred = ctx->cred,
4494 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004495 struct rpc_task_setup task_setup_data = {
4496 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004497 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004498 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004499 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004500 .flags = RPC_TASK_ASYNC,
4501 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004502
Frank Filz137d6ac2007-07-09 15:32:29 -07004503 /* Ensure this is an unlock - when canceling a lock, the
4504 * canceled lock is passed in, and it won't be an unlock.
4505 */
4506 fl->fl_type = F_UNLCK;
4507
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004508 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4509 if (data == NULL) {
4510 nfs_free_seqid(seqid);
4511 return ERR_PTR(-ENOMEM);
4512 }
4513
Trond Myklebust9d12b212012-01-17 22:04:25 -05004514 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004515 msg.rpc_argp = &data->arg;
4516 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004517 task_setup_data.callback_data = data;
4518 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004519}
4520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4522{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004523 struct inode *inode = state->inode;
4524 struct nfs4_state_owner *sp = state->owner;
4525 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004526 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004527 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004528 struct rpc_task *task;
4529 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004530 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
Trond Myklebust9b073572006-06-29 16:38:34 -04004532 status = nfs4_set_lock_state(state, request);
4533 /* Unlock _before_ we do the RPC call */
4534 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004535 /* Exclude nfs_delegation_claim_locks() */
4536 mutex_lock(&sp->so_delegreturn_mutex);
4537 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004538 down_read(&nfsi->rwsem);
4539 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4540 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004541 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004542 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004543 }
4544 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004545 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004546 if (status != 0)
4547 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004548 /* Is this a delegated lock? */
4549 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004550 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004551 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004552 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004553 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004554 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004555 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004556 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004557 status = PTR_ERR(task);
4558 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004559 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004560 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004561 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004562out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004563 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004564 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565}
4566
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567struct nfs4_lockdata {
4568 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004569 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004570 struct nfs4_lock_state *lsp;
4571 struct nfs_open_context *ctx;
4572 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004573 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004574 int rpc_status;
4575 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004576 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577};
4578
4579static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004580 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4581 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004582{
4583 struct nfs4_lockdata *p;
4584 struct inode *inode = lsp->ls_state->inode;
4585 struct nfs_server *server = NFS_SERVER(inode);
4586
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004587 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004588 if (p == NULL)
4589 return NULL;
4590
4591 p->arg.fh = NFS_FH(inode);
4592 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004593 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004594 if (p->arg.open_seqid == NULL)
4595 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004596 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004597 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004598 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004599 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004600 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004601 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004602 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004603 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004604 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004605 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004606 atomic_inc(&lsp->ls_count);
4607 p->ctx = get_nfs_open_context(ctx);
4608 memcpy(&p->fl, fl, sizeof(p->fl));
4609 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004610out_free_seqid:
4611 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004612out_free:
4613 kfree(p);
4614 return NULL;
4615}
4616
4617static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4618{
4619 struct nfs4_lockdata *data = calldata;
4620 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
Harvey Harrison3110ff82008-05-02 13:42:44 -07004622 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004623 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004624 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004625 /* Do we need to do an open_to_lock_owner? */
4626 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004627 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004628 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004629 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004630 data->arg.open_stateid = &state->stateid;
4631 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004632 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004633 } else
4634 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004635 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004636 if (nfs4_setup_sequence(data->server,
4637 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004638 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004639 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004640 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004641 nfs_release_seqid(data->arg.open_seqid);
4642out_release_lock_seqid:
4643 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004644out_wait:
4645 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004646 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004647}
4648
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004649static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4650{
4651 struct nfs4_lockdata *data = calldata;
4652
Harvey Harrison3110ff82008-05-02 13:42:44 -07004653 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004654
Trond Myklebust14516c32010-07-31 14:29:06 -04004655 if (!nfs4_sequence_done(task, &data->res.seq_res))
4656 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004657
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004658 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004659 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004660 if (data->rpc_status == 0)
4661 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4662 else
4663 goto out;
4664 }
4665 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004666 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004667 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004668 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004669 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004670out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004671 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004672}
4673
4674static void nfs4_lock_release(void *calldata)
4675{
4676 struct nfs4_lockdata *data = calldata;
4677
Harvey Harrison3110ff82008-05-02 13:42:44 -07004678 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004679 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004680 if (data->cancelled != 0) {
4681 struct rpc_task *task;
4682 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4683 data->arg.lock_seqid);
4684 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004685 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004686 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004687 } else
4688 nfs_free_seqid(data->arg.lock_seqid);
4689 nfs4_put_lock_state(data->lsp);
4690 put_nfs_open_context(data->ctx);
4691 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004692 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004693}
4694
4695static const struct rpc_call_ops nfs4_lock_ops = {
4696 .rpc_call_prepare = nfs4_lock_prepare,
4697 .rpc_call_done = nfs4_lock_done,
4698 .rpc_release = nfs4_lock_release,
4699};
4700
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004701static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4702{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004703 switch (error) {
4704 case -NFS4ERR_ADMIN_REVOKED:
4705 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004706 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004707 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004708 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004709 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004710 break;
4711 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004712 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004713 case -NFS4ERR_EXPIRED:
4714 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004715 };
4716}
4717
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004718static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004719{
4720 struct nfs4_lockdata *data;
4721 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004722 struct rpc_message msg = {
4723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4724 .rpc_cred = state->owner->so_cred,
4725 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004726 struct rpc_task_setup task_setup_data = {
4727 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004728 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004729 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004730 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004731 .flags = RPC_TASK_ASYNC,
4732 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004733 int ret;
4734
Harvey Harrison3110ff82008-05-02 13:42:44 -07004735 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004736 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004737 fl->fl_u.nfs4_fl.owner,
4738 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004739 if (data == NULL)
4740 return -ENOMEM;
4741 if (IS_SETLKW(cmd))
4742 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004743 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004744 msg.rpc_argp = &data->arg;
4745 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004746 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004747 if (recovery_type > NFS_LOCK_NEW) {
4748 if (recovery_type == NFS_LOCK_RECLAIM)
4749 data->arg.reclaim = NFS_LOCK_RECLAIM;
4750 nfs4_set_sequence_privileged(&data->arg.seq_args);
4751 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004752 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004753 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004754 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004755 ret = nfs4_wait_for_completion_rpc_task(task);
4756 if (ret == 0) {
4757 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004758 if (ret)
4759 nfs4_handle_setlk_error(data->server, data->lsp,
4760 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004761 } else
4762 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004763 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004764 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004765 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766}
4767
4768static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4769{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004770 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004771 struct nfs4_exception exception = {
4772 .inode = state->inode,
4773 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004774 int err;
4775
4776 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004777 /* Cache the lock if possible... */
4778 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4779 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004780 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004781 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004782 break;
4783 nfs4_handle_exception(server, err, &exception);
4784 } while (exception.retry);
4785 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
4788static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4789{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004790 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004791 struct nfs4_exception exception = {
4792 .inode = state->inode,
4793 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004794 int err;
4795
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004796 err = nfs4_set_lock_state(state, request);
4797 if (err != 0)
4798 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004799 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004800 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4801 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004802 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004803 switch (err) {
4804 default:
4805 goto out;
4806 case -NFS4ERR_GRACE:
4807 case -NFS4ERR_DELAY:
4808 nfs4_handle_exception(server, err, &exception);
4809 err = 0;
4810 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004811 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004812out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004813 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814}
4815
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004816#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004817/**
4818 * nfs41_check_expired_locks - possibly free a lock stateid
4819 *
4820 * @state: NFSv4 state for an inode
4821 *
4822 * Returns NFS_OK if recovery for this stateid is now finished.
4823 * Otherwise a negative NFS4ERR value is returned.
4824 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004825static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004826{
Chuck Levereb64cf92012-07-11 16:30:05 -04004827 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004828 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004829 struct nfs_server *server = NFS_SERVER(state->inode);
4830
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004831 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004832 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004833 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4834 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004835 /* Free the stateid unless the server
4836 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004837 if (status != -NFS4ERR_BAD_STATEID)
4838 nfs41_free_stateid(server,
4839 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004840 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004841 ret = status;
4842 }
4843 }
4844 };
4845
4846 return ret;
4847}
4848
4849static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4850{
4851 int status = NFS_OK;
4852
4853 if (test_bit(LK_STATE_IN_USE, &state->flags))
4854 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004855 if (status != NFS_OK)
4856 status = nfs4_lock_expired(state, request);
4857 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004858}
4859#endif
4860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4862{
Trond Myklebust9a99af492013-02-04 20:17:49 -05004863 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004864 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004865 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004866 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004867 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004869 if ((fl_flags & FL_POSIX) &&
4870 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4871 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004872 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004873 status = nfs4_set_lock_state(state, request);
4874 if (status != 0)
4875 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004876 request->fl_flags |= FL_ACCESS;
4877 status = do_vfs_lock(request->fl_file, request);
4878 if (status < 0)
4879 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004880 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004881 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004882 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004883 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004884 request->fl_flags = fl_flags & ~FL_SLEEP;
4885 status = do_vfs_lock(request->fl_file, request);
4886 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004887 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05004888 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4889 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004890 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004891 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05004892 goto out;
4893 down_read(&nfsi->rwsem);
4894 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4895 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004896 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004897 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004898 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004899 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004900 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004901 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4902 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004903out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004904 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004905out:
4906 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 return status;
4908}
4909
4910static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4911{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004912 struct nfs4_exception exception = {
4913 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004914 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004915 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 int err;
4917
4918 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004919 err = _nfs4_proc_setlk(state, cmd, request);
4920 if (err == -NFS4ERR_DENIED)
4921 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004923 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 } while (exception.retry);
4925 return err;
4926}
4927
4928static int
4929nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4930{
4931 struct nfs_open_context *ctx;
4932 struct nfs4_state *state;
4933 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4934 int status;
4935
4936 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004937 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 state = ctx->state;
4939
4940 if (request->fl_start < 0 || request->fl_end < 0)
4941 return -EINVAL;
4942
Trond Myklebustd9531262009-07-21 19:22:38 -04004943 if (IS_GETLK(cmd)) {
4944 if (state != NULL)
4945 return nfs4_proc_getlk(state, F_GETLK, request);
4946 return 0;
4947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
4949 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4950 return -EINVAL;
4951
Trond Myklebustd9531262009-07-21 19:22:38 -04004952 if (request->fl_type == F_UNLCK) {
4953 if (state != NULL)
4954 return nfs4_proc_unlck(state, cmd, request);
4955 return 0;
4956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Trond Myklebustd9531262009-07-21 19:22:38 -04004958 if (state == NULL)
4959 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004960 /*
4961 * Don't rely on the VFS having checked the file open mode,
4962 * since it won't do this for flock() locks.
4963 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004964 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004965 case F_RDLCK:
4966 if (!(filp->f_mode & FMODE_READ))
4967 return -EBADF;
4968 break;
4969 case F_WRLCK:
4970 if (!(filp->f_mode & FMODE_WRITE))
4971 return -EBADF;
4972 }
4973
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 do {
4975 status = nfs4_proc_setlk(state, cmd, request);
4976 if ((status != -EAGAIN) || IS_SETLK(cmd))
4977 break;
4978 timeout = nfs4_set_lock_task_retry(timeout);
4979 status = -ERESTARTSYS;
4980 if (signalled())
4981 break;
4982 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 return status;
4984}
4985
Trond Myklebust888e6942005-11-04 15:38:11 -05004986int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4987{
4988 struct nfs_server *server = NFS_SERVER(state->inode);
4989 struct nfs4_exception exception = { };
4990 int err;
4991
4992 err = nfs4_set_lock_state(state, fl);
4993 if (err != 0)
4994 goto out;
4995 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004996 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004997 switch (err) {
4998 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004999 printk(KERN_ERR "NFS: %s: unhandled error "
5000 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005001 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005002 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005003 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005004 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005005 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05005006 set_bit(NFS_DELEGATED_STATE, &state->flags);
5007 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005008 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05005009 err = -EAGAIN;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005010 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005011 case -NFS4ERR_BADSESSION:
5012 case -NFS4ERR_BADSLOT:
5013 case -NFS4ERR_BAD_HIGH_SLOT:
5014 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5015 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05005016 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04005017 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05005018 err = -EAGAIN;
Trond Myklebustd5122202009-06-17 13:22:58 -07005019 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005020 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005021 case -NFS4ERR_ADMIN_REVOKED:
5022 case -NFS4ERR_BAD_STATEID:
5023 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005024 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005025 err = 0;
5026 goto out;
5027 case -ENOMEM:
5028 case -NFS4ERR_DENIED:
5029 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5030 err = 0;
5031 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005032 }
Trond Myklebust37380e42013-02-05 12:35:16 -05005033 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust888e6942005-11-04 15:38:11 -05005034 err = nfs4_handle_exception(server, err, &exception);
5035 } while (exception.retry);
5036out:
5037 return err;
5038}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005039
Trond Myklebustcf470c32012-03-07 13:49:12 -05005040struct nfs_release_lockowner_data {
5041 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005042 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005043 struct nfs_release_lockowner_args args;
5044};
5045
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005046static void nfs4_release_lockowner_release(void *calldata)
5047{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005048 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005049 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005050 kfree(calldata);
5051}
5052
Trond Myklebust17280172012-03-11 13:11:00 -04005053static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005054 .rpc_release = nfs4_release_lockowner_release,
5055};
5056
Trond Myklebustcf470c32012-03-07 13:49:12 -05005057int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005058{
5059 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005060 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005061 struct rpc_message msg = {
5062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5063 };
5064
5065 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005066 return -EINVAL;
5067 data = kmalloc(sizeof(*data), GFP_NOFS);
5068 if (!data)
5069 return -ENOMEM;
5070 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005071 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005072 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5073 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5074 data->args.lock_owner.s_dev = server->s_dev;
5075 msg.rpc_argp = &data->args;
5076 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5077 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005078}
5079
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005080#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5081
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005082static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5083 const void *buf, size_t buflen,
5084 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005085{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005086 if (strcmp(key, "") != 0)
5087 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005088
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005089 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005090}
5091
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005092static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5093 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005094{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005095 if (strcmp(key, "") != 0)
5096 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005097
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005098 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005099}
5100
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005101static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5102 size_t list_len, const char *name,
5103 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005104{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005105 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005106
J. Bruce Fields096455a2006-03-20 23:23:42 -05005107 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5108 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005109
5110 if (list && len <= list_len)
5111 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005112 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005113}
5114
Andy Adamson533eb462011-06-13 18:25:56 -04005115/*
5116 * nfs_fhget will use either the mounted_on_fileid or the fileid
5117 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005118static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5119{
Andy Adamson533eb462011-06-13 18:25:56 -04005120 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5121 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5122 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005123 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005124 return;
5125
5126 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005127 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005128 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5129 fattr->nlink = 2;
5130}
5131
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005132static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5133 const struct qstr *name,
5134 struct nfs4_fs_locations *fs_locations,
5135 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005136{
5137 struct nfs_server *server = NFS_SERVER(dir);
5138 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005139 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005140 };
5141 struct nfs4_fs_locations_arg args = {
5142 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005143 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005144 .page = page,
5145 .bitmask = bitmask,
5146 };
Benny Halevy22958462009-04-01 09:22:02 -04005147 struct nfs4_fs_locations_res res = {
5148 .fs_locations = fs_locations,
5149 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005150 struct rpc_message msg = {
5151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5152 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005153 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005154 };
5155 int status;
5156
Harvey Harrison3110ff82008-05-02 13:42:44 -07005157 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005158
5159 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5160 * is not supported */
5161 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5162 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5163 else
5164 bitmask[0] |= FATTR4_WORD0_FILEID;
5165
Trond Myklebustc228fd32007-01-13 02:28:11 -05005166 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005167 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005168 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005169 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005170 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005171 return status;
5172}
5173
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005174int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5175 const struct qstr *name,
5176 struct nfs4_fs_locations *fs_locations,
5177 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005178{
5179 struct nfs4_exception exception = { };
5180 int err;
5181 do {
5182 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005183 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005184 &exception);
5185 } while (exception.retry);
5186 return err;
5187}
5188
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005189static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5190{
5191 int status;
5192 struct nfs4_secinfo_arg args = {
5193 .dir_fh = NFS_FH(dir),
5194 .name = name,
5195 };
5196 struct nfs4_secinfo_res res = {
5197 .flavors = flavors,
5198 };
5199 struct rpc_message msg = {
5200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5201 .rpc_argp = &args,
5202 .rpc_resp = &res,
5203 };
5204
5205 dprintk("NFS call secinfo %s\n", name->name);
5206 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5207 dprintk("NFS reply secinfo: %d\n", status);
5208 return status;
5209}
5210
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005211int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5212 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005213{
5214 struct nfs4_exception exception = { };
5215 int err;
5216 do {
5217 err = nfs4_handle_exception(NFS_SERVER(dir),
5218 _nfs4_proc_secinfo(dir, name, flavors),
5219 &exception);
5220 } while (exception.retry);
5221 return err;
5222}
5223
Andy Adamson557134a2009-04-01 09:21:53 -04005224#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005225/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005226 * Check the exchange flags returned by the server for invalid flags, having
5227 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5228 * DS flags set.
5229 */
5230static int nfs4_check_cl_exchange_flags(u32 flags)
5231{
5232 if (flags & ~EXCHGID4_FLAG_MASK_R)
5233 goto out_inval;
5234 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5235 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5236 goto out_inval;
5237 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5238 goto out_inval;
5239 return NFS_OK;
5240out_inval:
5241 return -NFS4ERR_INVAL;
5242}
5243
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005244static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005245nfs41_same_server_scope(struct nfs41_server_scope *a,
5246 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005247{
5248 if (a->server_scope_sz == b->server_scope_sz &&
5249 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5250 return true;
5251
5252 return false;
5253}
5254
Andy Adamson357f54d2010-12-14 10:11:57 -05005255/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005256 * nfs4_proc_bind_conn_to_session()
5257 *
5258 * The 4.1 client currently uses the same TCP connection for the
5259 * fore and backchannel.
5260 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005261int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005262{
5263 int status;
5264 struct nfs41_bind_conn_to_session_res res;
5265 struct rpc_message msg = {
5266 .rpc_proc =
5267 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5268 .rpc_argp = clp,
5269 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005270 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005271 };
5272
5273 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005274
5275 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5276 if (unlikely(res.session == NULL)) {
5277 status = -ENOMEM;
5278 goto out;
5279 }
5280
5281 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5282 if (status == 0) {
5283 if (memcmp(res.session->sess_id.data,
5284 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5285 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5286 status = -EIO;
5287 goto out_session;
5288 }
5289 if (res.dir != NFS4_CDFS4_BOTH) {
5290 dprintk("NFS: %s: Unexpected direction from server\n",
5291 __func__);
5292 status = -EIO;
5293 goto out_session;
5294 }
5295 if (res.use_conn_in_rdma_mode) {
5296 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5297 __func__);
5298 status = -EIO;
5299 goto out_session;
5300 }
5301 }
5302out_session:
5303 kfree(res.session);
5304out:
5305 dprintk("<-- %s status= %d\n", __func__, status);
5306 return status;
5307}
5308
5309/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005310 * nfs4_proc_exchange_id()
5311 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005312 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5313 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005314 * Since the clientid has expired, all compounds using sessions
5315 * associated with the stale clientid will be returning
5316 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5317 * be in some phase of session reset.
5318 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005319int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005320{
5321 nfs4_verifier verifier;
5322 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005323 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005324 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005325 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005326 };
5327 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005328 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005329 };
5330 int status;
5331 struct rpc_message msg = {
5332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5333 .rpc_argp = &args,
5334 .rpc_resp = &res,
5335 .rpc_cred = cred,
5336 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005337
Chuck Leverf0920752012-05-21 22:45:41 -04005338 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005339 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5340 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005341 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5342 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5343 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005344
Chuck Leveracdeb692012-05-21 22:46:16 -04005345 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005346 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005347 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005348 status = -ENOMEM;
5349 goto out;
5350 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005351
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005352 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005353 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005354 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005355 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005356 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005357 }
5358
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005359 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005360 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005361 status = -ENOMEM;
5362 goto out_server_scope;
5363 }
5364
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005365 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005366 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005367 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005368
Chuck Lever177313f2012-05-21 22:44:58 -04005369 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005370 clp->cl_clientid = res.clientid;
5371 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5372 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5373 clp->cl_seqid = res.seqid;
5374
Chuck Leveracdeb692012-05-21 22:46:16 -04005375 kfree(clp->cl_serverowner);
5376 clp->cl_serverowner = res.server_owner;
5377 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005378
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005379 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005380 kfree(clp->cl_implid);
5381 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005382
Chuck Lever177313f2012-05-21 22:44:58 -04005383 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005384 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005385 res.server_scope)) {
5386 dprintk("%s: server_scope mismatch detected\n",
5387 __func__);
5388 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005389 kfree(clp->cl_serverscope);
5390 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005391 }
5392
Chuck Lever177313f2012-05-21 22:44:58 -04005393 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005394 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005395 goto out;
5396 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005397 } else
5398 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005399
Chuck Leveracdeb692012-05-21 22:46:16 -04005400out_server_owner:
5401 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005402out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005403 kfree(res.server_scope);
5404out:
Chuck Lever177313f2012-05-21 22:44:58 -04005405 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005406 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005407 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005408 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005409 clp->cl_implid->date.seconds,
5410 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005411 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005412 return status;
5413}
5414
Trond Myklebust66245532012-05-25 17:18:09 -04005415static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5416 struct rpc_cred *cred)
5417{
5418 struct rpc_message msg = {
5419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5420 .rpc_argp = clp,
5421 .rpc_cred = cred,
5422 };
5423 int status;
5424
5425 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5426 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005427 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005428 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5429 return status;
5430}
5431
5432static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5433 struct rpc_cred *cred)
5434{
5435 unsigned int loop;
5436 int ret;
5437
5438 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5439 ret = _nfs4_proc_destroy_clientid(clp, cred);
5440 switch (ret) {
5441 case -NFS4ERR_DELAY:
5442 case -NFS4ERR_CLIENTID_BUSY:
5443 ssleep(1);
5444 break;
5445 default:
5446 return ret;
5447 }
5448 }
5449 return 0;
5450}
5451
5452int nfs4_destroy_clientid(struct nfs_client *clp)
5453{
5454 struct rpc_cred *cred;
5455 int ret = 0;
5456
5457 if (clp->cl_mvops->minor_version < 1)
5458 goto out;
5459 if (clp->cl_exchange_flags == 0)
5460 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005461 if (clp->cl_preserve_clid)
5462 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005463 cred = nfs4_get_exchange_id_cred(clp);
5464 ret = nfs4_proc_destroy_clientid(clp, cred);
5465 if (cred)
5466 put_rpccred(cred);
5467 switch (ret) {
5468 case 0:
5469 case -NFS4ERR_STALE_CLIENTID:
5470 clp->cl_exchange_flags = 0;
5471 }
5472out:
5473 return ret;
5474}
5475
Andy Adamson2050f0c2009-04-01 09:22:30 -04005476struct nfs4_get_lease_time_data {
5477 struct nfs4_get_lease_time_args *args;
5478 struct nfs4_get_lease_time_res *res;
5479 struct nfs_client *clp;
5480};
5481
5482static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5483 void *calldata)
5484{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005485 struct nfs4_get_lease_time_data *data =
5486 (struct nfs4_get_lease_time_data *)calldata;
5487
5488 dprintk("--> %s\n", __func__);
5489 /* just setup sequence, do not trigger session recovery
5490 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005491 nfs41_setup_sequence(data->clp->cl_session,
5492 &data->args->la_seq_args,
5493 &data->res->lr_seq_res,
5494 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005495 dprintk("<-- %s\n", __func__);
5496}
5497
5498/*
5499 * Called from nfs4_state_manager thread for session setup, so don't recover
5500 * from sequence operation or clientid errors.
5501 */
5502static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5503{
5504 struct nfs4_get_lease_time_data *data =
5505 (struct nfs4_get_lease_time_data *)calldata;
5506
5507 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005508 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5509 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005510 switch (task->tk_status) {
5511 case -NFS4ERR_DELAY:
5512 case -NFS4ERR_GRACE:
5513 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5514 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5515 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005516 /* fall through */
5517 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005518 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005519 return;
5520 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005521 dprintk("<-- %s\n", __func__);
5522}
5523
Trond Myklebust17280172012-03-11 13:11:00 -04005524static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005525 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5526 .rpc_call_done = nfs4_get_lease_time_done,
5527};
5528
5529int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5530{
5531 struct rpc_task *task;
5532 struct nfs4_get_lease_time_args args;
5533 struct nfs4_get_lease_time_res res = {
5534 .lr_fsinfo = fsinfo,
5535 };
5536 struct nfs4_get_lease_time_data data = {
5537 .args = &args,
5538 .res = &res,
5539 .clp = clp,
5540 };
5541 struct rpc_message msg = {
5542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5543 .rpc_argp = &args,
5544 .rpc_resp = &res,
5545 };
5546 struct rpc_task_setup task_setup = {
5547 .rpc_client = clp->cl_rpcclient,
5548 .rpc_message = &msg,
5549 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005550 .callback_data = &data,
5551 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005552 };
5553 int status;
5554
Trond Myklebust9d12b212012-01-17 22:04:25 -05005555 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005556 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005557 dprintk("--> %s\n", __func__);
5558 task = rpc_run_task(&task_setup);
5559
5560 if (IS_ERR(task))
5561 status = PTR_ERR(task);
5562 else {
5563 status = task->tk_status;
5564 rpc_put_task(task);
5565 }
5566 dprintk("<-- %s return %d\n", __func__, status);
5567
5568 return status;
5569}
5570
Andy Adamsonfc931582009-04-01 09:22:31 -04005571/*
5572 * Initialize the values to be used by the client in CREATE_SESSION
5573 * If nfs4_init_session set the fore channel request and response sizes,
5574 * use them.
5575 *
5576 * Set the back channel max_resp_sz_cached to zero to force the client to
5577 * always set csa_cachethis to FALSE because the current implementation
5578 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5579 */
5580static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5581{
5582 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005583 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5584 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005585
5586 if (mxrqst_sz == 0)
5587 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5588 if (mxresp_sz == 0)
5589 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5590 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005591 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5592 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005593 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005594 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005595
5596 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005597 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005598 __func__,
5599 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005600 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005601
5602 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005603 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5604 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5605 args->bc_attrs.max_resp_sz_cached = 0;
5606 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5607 args->bc_attrs.max_reqs = 1;
5608
5609 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5610 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5611 __func__,
5612 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5613 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5614 args->bc_attrs.max_reqs);
5615}
5616
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005617static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005618{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005619 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5620 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5621
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005622 if (rcvd->max_resp_sz > sent->max_resp_sz)
5623 return -EINVAL;
5624 /*
5625 * Our requested max_ops is the minimum we need; we're not
5626 * prepared to break up compounds into smaller pieces than that.
5627 * So, no point even trying to continue if the server won't
5628 * cooperate:
5629 */
5630 if (rcvd->max_ops < sent->max_ops)
5631 return -EINVAL;
5632 if (rcvd->max_reqs == 0)
5633 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005634 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5635 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005636 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005637}
5638
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005639static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5640{
5641 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5642 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005643
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005644 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5645 return -EINVAL;
5646 if (rcvd->max_resp_sz < sent->max_resp_sz)
5647 return -EINVAL;
5648 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5649 return -EINVAL;
5650 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005651 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005652 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005653 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005654 return -EINVAL;
5655 return 0;
5656}
Andy Adamson8d353012009-04-01 09:22:32 -04005657
Andy Adamson8d353012009-04-01 09:22:32 -04005658static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5659 struct nfs4_session *session)
5660{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005661 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005662
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005663 ret = nfs4_verify_fore_channel_attrs(args, session);
5664 if (ret)
5665 return ret;
5666 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005667}
5668
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005669static int _nfs4_proc_create_session(struct nfs_client *clp,
5670 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005671{
5672 struct nfs4_session *session = clp->cl_session;
5673 struct nfs41_create_session_args args = {
5674 .client = clp,
5675 .cb_program = NFS4_CALLBACK,
5676 };
5677 struct nfs41_create_session_res res = {
5678 .client = clp,
5679 };
5680 struct rpc_message msg = {
5681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5682 .rpc_argp = &args,
5683 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005684 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005685 };
5686 int status;
5687
5688 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005689 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005690
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005691 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005692
Trond Myklebust43095d32012-11-20 11:13:12 -05005693 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005694 /* Verify the session's negotiated channel_attrs values */
5695 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005696 /* Increment the clientid slot sequence id */
5697 clp->cl_seqid++;
5698 }
5699
5700 return status;
5701}
5702
5703/*
5704 * Issues a CREATE_SESSION operation to the server.
5705 * It is the responsibility of the caller to verify the session is
5706 * expired before calling this routine.
5707 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005708int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005709{
5710 int status;
5711 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005712 struct nfs4_session *session = clp->cl_session;
5713
5714 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5715
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005716 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005717 if (status)
5718 goto out;
5719
Andy Adamsonaacd5532011-11-09 13:58:21 -05005720 /* Init or reset the session slot tables */
5721 status = nfs4_setup_session_slot_tables(session);
5722 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005723 if (status)
5724 goto out;
5725
5726 ptr = (unsigned *)&session->sess_id.data[0];
5727 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5728 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005729out:
5730 dprintk("<-- %s\n", __func__);
5731 return status;
5732}
5733
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005734/*
5735 * Issue the over-the-wire RPC DESTROY_SESSION.
5736 * The caller must serialize access to this routine.
5737 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005738int nfs4_proc_destroy_session(struct nfs4_session *session,
5739 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005740{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005741 struct rpc_message msg = {
5742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5743 .rpc_argp = session,
5744 .rpc_cred = cred,
5745 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005746 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005747
5748 dprintk("--> nfs4_proc_destroy_session\n");
5749
5750 /* session is still being setup */
5751 if (session->clp->cl_cons_state != NFS_CS_READY)
5752 return status;
5753
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005754 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005755
5756 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005757 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005758 "Session has been destroyed regardless...\n", status);
5759
5760 dprintk("<-- nfs4_proc_destroy_session\n");
5761 return status;
5762}
5763
Trond Myklebust7b38c362012-05-23 13:23:31 -04005764/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005765 * Renew the cl_session lease.
5766 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005767struct nfs4_sequence_data {
5768 struct nfs_client *clp;
5769 struct nfs4_sequence_args args;
5770 struct nfs4_sequence_res res;
5771};
5772
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005773static void nfs41_sequence_release(void *data)
5774{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005775 struct nfs4_sequence_data *calldata = data;
5776 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005777
Alexandros Batsakis71358402010-02-05 03:45:05 -08005778 if (atomic_read(&clp->cl_count) > 1)
5779 nfs4_schedule_state_renewal(clp);
5780 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005781 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005782}
5783
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005784static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5785{
5786 switch(task->tk_status) {
5787 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005788 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5789 return -EAGAIN;
5790 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005791 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005792 }
5793 return 0;
5794}
5795
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005796static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005797{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005798 struct nfs4_sequence_data *calldata = data;
5799 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005800
Trond Myklebust14516c32010-07-31 14:29:06 -04005801 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5802 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005803
5804 if (task->tk_status < 0) {
5805 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005806 if (atomic_read(&clp->cl_count) == 1)
5807 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005808
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005809 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5810 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005811 return;
5812 }
5813 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005814 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005815out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005816 dprintk("<-- %s\n", __func__);
5817}
5818
5819static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5820{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005821 struct nfs4_sequence_data *calldata = data;
5822 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005823 struct nfs4_sequence_args *args;
5824 struct nfs4_sequence_res *res;
5825
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005826 args = task->tk_msg.rpc_argp;
5827 res = task->tk_msg.rpc_resp;
5828
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005829 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005830}
5831
5832static const struct rpc_call_ops nfs41_sequence_ops = {
5833 .rpc_call_done = nfs41_sequence_call_done,
5834 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005835 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005836};
5837
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005838static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5839 struct rpc_cred *cred,
5840 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005841{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005842 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005843 struct rpc_message msg = {
5844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5845 .rpc_cred = cred,
5846 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005847 struct rpc_task_setup task_setup_data = {
5848 .rpc_client = clp->cl_rpcclient,
5849 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005850 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005851 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5852 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005853
Alexandros Batsakis71358402010-02-05 03:45:05 -08005854 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005855 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005856 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005857 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005858 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005859 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005860 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005861 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005862 if (is_privileged)
5863 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005864 msg.rpc_argp = &calldata->args;
5865 msg.rpc_resp = &calldata->res;
5866 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005867 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005868
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005869 return rpc_run_task(&task_setup_data);
5870}
5871
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005872static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005873{
5874 struct rpc_task *task;
5875 int ret = 0;
5876
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005877 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5878 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005879 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005880 if (IS_ERR(task))
5881 ret = PTR_ERR(task);
5882 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005883 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005884 dprintk("<-- %s status=%d\n", __func__, ret);
5885 return ret;
5886}
5887
5888static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5889{
5890 struct rpc_task *task;
5891 int ret;
5892
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005893 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005894 if (IS_ERR(task)) {
5895 ret = PTR_ERR(task);
5896 goto out;
5897 }
5898 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005899 if (!ret) {
5900 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5901
5902 if (task->tk_status == 0)
5903 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005904 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005905 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005906 rpc_put_task(task);
5907out:
5908 dprintk("<-- %s status=%d\n", __func__, ret);
5909 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005910}
5911
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005912struct nfs4_reclaim_complete_data {
5913 struct nfs_client *clp;
5914 struct nfs41_reclaim_complete_args arg;
5915 struct nfs41_reclaim_complete_res res;
5916};
5917
5918static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5919{
5920 struct nfs4_reclaim_complete_data *calldata = data;
5921
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005922 nfs41_setup_sequence(calldata->clp->cl_session,
5923 &calldata->arg.seq_args,
5924 &calldata->res.seq_res,
5925 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005926}
5927
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005928static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5929{
5930 switch(task->tk_status) {
5931 case 0:
5932 case -NFS4ERR_COMPLETE_ALREADY:
5933 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5934 break;
5935 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005936 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005937 /* fall through */
5938 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005939 return -EAGAIN;
5940 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005941 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005942 }
5943 return 0;
5944}
5945
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005946static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5947{
5948 struct nfs4_reclaim_complete_data *calldata = data;
5949 struct nfs_client *clp = calldata->clp;
5950 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5951
5952 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005953 if (!nfs41_sequence_done(task, res))
5954 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005955
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005956 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5957 rpc_restart_call_prepare(task);
5958 return;
5959 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005960 dprintk("<-- %s\n", __func__);
5961}
5962
5963static void nfs4_free_reclaim_complete_data(void *data)
5964{
5965 struct nfs4_reclaim_complete_data *calldata = data;
5966
5967 kfree(calldata);
5968}
5969
5970static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5971 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5972 .rpc_call_done = nfs4_reclaim_complete_done,
5973 .rpc_release = nfs4_free_reclaim_complete_data,
5974};
5975
5976/*
5977 * Issue a global reclaim complete.
5978 */
5979static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5980{
5981 struct nfs4_reclaim_complete_data *calldata;
5982 struct rpc_task *task;
5983 struct rpc_message msg = {
5984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5985 };
5986 struct rpc_task_setup task_setup_data = {
5987 .rpc_client = clp->cl_rpcclient,
5988 .rpc_message = &msg,
5989 .callback_ops = &nfs4_reclaim_complete_call_ops,
5990 .flags = RPC_TASK_ASYNC,
5991 };
5992 int status = -ENOMEM;
5993
5994 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005995 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005996 if (calldata == NULL)
5997 goto out;
5998 calldata->clp = clp;
5999 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006000
Trond Myklebust9d12b212012-01-17 22:04:25 -05006001 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006002 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006003 msg.rpc_argp = &calldata->arg;
6004 msg.rpc_resp = &calldata->res;
6005 task_setup_data.callback_data = calldata;
6006 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006007 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006008 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006009 goto out;
6010 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006011 status = nfs4_wait_for_completion_rpc_task(task);
6012 if (status == 0)
6013 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006014 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006015 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006016out:
6017 dprintk("<-- %s status=%d\n", __func__, status);
6018 return status;
6019}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006020
6021static void
6022nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6023{
6024 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006025 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006026 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006027
6028 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006029 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6030 * right now covering the LAYOUTGET we are about to send.
6031 * However, that is not so catastrophic, and there seems
6032 * to be no way to prevent it completely.
6033 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006034 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006035 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006036 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006037 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6038 NFS_I(lgp->args.inode)->layout,
6039 lgp->args.ctx->state)) {
6040 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006041 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006042}
6043
6044static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6045{
6046 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006047 struct inode *inode = lgp->args.inode;
6048 struct nfs_server *server = NFS_SERVER(inode);
6049 struct pnfs_layout_hdr *lo;
6050 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006051 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006052
6053 dprintk("--> %s\n", __func__);
6054
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006055 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006056 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006057
6058 switch (task->tk_status) {
6059 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006060 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006061 case -NFS4ERR_LAYOUTTRYLATER:
6062 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006063 timeo = rpc_get_timeout(task->tk_client);
6064 giveup = lgp->args.timestamp + timeo;
6065 if (time_after(giveup, jiffies))
6066 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006067 break;
6068 case -NFS4ERR_EXPIRED:
6069 case -NFS4ERR_BAD_STATEID:
6070 spin_lock(&inode->i_lock);
6071 lo = NFS_I(inode)->layout;
6072 if (!lo || list_empty(&lo->plh_segs)) {
6073 spin_unlock(&inode->i_lock);
6074 /* If the open stateid was bad, then recover it. */
6075 state = lgp->args.ctx->state;
6076 } else {
6077 LIST_HEAD(head);
6078
6079 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6080 spin_unlock(&inode->i_lock);
6081 /* Mark the bad layout state as invalid, then
6082 * retry using the open stateid. */
6083 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006084 }
6085 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006086 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6087 rpc_restart_call_prepare(task);
6088out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006089 dprintk("<-- %s\n", __func__);
6090}
6091
Idan Kedar85541162012-08-02 11:47:10 +03006092static size_t max_response_pages(struct nfs_server *server)
6093{
6094 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6095 return nfs_page_array_len(0, max_resp_sz);
6096}
6097
6098static void nfs4_free_pages(struct page **pages, size_t size)
6099{
6100 int i;
6101
6102 if (!pages)
6103 return;
6104
6105 for (i = 0; i < size; i++) {
6106 if (!pages[i])
6107 break;
6108 __free_page(pages[i]);
6109 }
6110 kfree(pages);
6111}
6112
6113static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6114{
6115 struct page **pages;
6116 int i;
6117
6118 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6119 if (!pages) {
6120 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6121 return NULL;
6122 }
6123
6124 for (i = 0; i < size; i++) {
6125 pages[i] = alloc_page(gfp_flags);
6126 if (!pages[i]) {
6127 dprintk("%s: failed to allocate page\n", __func__);
6128 nfs4_free_pages(pages, size);
6129 return NULL;
6130 }
6131 }
6132
6133 return pages;
6134}
6135
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006136static void nfs4_layoutget_release(void *calldata)
6137{
6138 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006139 struct inode *inode = lgp->args.inode;
6140 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006141 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006142
6143 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006144 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006145 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006146 put_nfs_open_context(lgp->args.ctx);
6147 kfree(calldata);
6148 dprintk("<-- %s\n", __func__);
6149}
6150
6151static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6152 .rpc_call_prepare = nfs4_layoutget_prepare,
6153 .rpc_call_done = nfs4_layoutget_done,
6154 .rpc_release = nfs4_layoutget_release,
6155};
6156
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006157struct pnfs_layout_segment *
6158nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006159{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006160 struct inode *inode = lgp->args.inode;
6161 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006162 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006163 struct rpc_task *task;
6164 struct rpc_message msg = {
6165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6166 .rpc_argp = &lgp->args,
6167 .rpc_resp = &lgp->res,
6168 };
6169 struct rpc_task_setup task_setup_data = {
6170 .rpc_client = server->client,
6171 .rpc_message = &msg,
6172 .callback_ops = &nfs4_layoutget_call_ops,
6173 .callback_data = lgp,
6174 .flags = RPC_TASK_ASYNC,
6175 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006176 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006177 int status = 0;
6178
6179 dprintk("--> %s\n", __func__);
6180
Idan Kedar85541162012-08-02 11:47:10 +03006181 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6182 if (!lgp->args.layout.pages) {
6183 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006184 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006185 }
6186 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006187 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006188
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006189 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006190 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006191 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006192
6193 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6194 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6195
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006196 task = rpc_run_task(&task_setup_data);
6197 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006198 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006199 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006200 if (status == 0)
6201 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006202 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6203 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006204 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006205 rpc_put_task(task);
6206 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006207 if (status)
6208 return ERR_PTR(status);
6209 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006210}
6211
Benny Halevycbe82602011-05-22 19:52:37 +03006212static void
6213nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6214{
6215 struct nfs4_layoutreturn *lrp = calldata;
6216
6217 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006218 nfs41_setup_sequence(lrp->clp->cl_session,
6219 &lrp->args.seq_args,
6220 &lrp->res.seq_res,
6221 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006222}
6223
6224static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6225{
6226 struct nfs4_layoutreturn *lrp = calldata;
6227 struct nfs_server *server;
6228
6229 dprintk("--> %s\n", __func__);
6230
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006231 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006232 return;
6233
6234 server = NFS_SERVER(lrp->args.inode);
6235 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006236 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006237 return;
6238 }
Benny Halevycbe82602011-05-22 19:52:37 +03006239 dprintk("<-- %s\n", __func__);
6240}
6241
6242static void nfs4_layoutreturn_release(void *calldata)
6243{
6244 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006245 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006246
6247 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006248 spin_lock(&lo->plh_inode->i_lock);
6249 if (lrp->res.lrs_present)
6250 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6251 lo->plh_block_lgets--;
6252 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006253 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006254 kfree(calldata);
6255 dprintk("<-- %s\n", __func__);
6256}
6257
6258static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6259 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6260 .rpc_call_done = nfs4_layoutreturn_done,
6261 .rpc_release = nfs4_layoutreturn_release,
6262};
6263
6264int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6265{
6266 struct rpc_task *task;
6267 struct rpc_message msg = {
6268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6269 .rpc_argp = &lrp->args,
6270 .rpc_resp = &lrp->res,
6271 };
6272 struct rpc_task_setup task_setup_data = {
6273 .rpc_client = lrp->clp->cl_rpcclient,
6274 .rpc_message = &msg,
6275 .callback_ops = &nfs4_layoutreturn_call_ops,
6276 .callback_data = lrp,
6277 };
6278 int status;
6279
6280 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006281 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006282 task = rpc_run_task(&task_setup_data);
6283 if (IS_ERR(task))
6284 return PTR_ERR(task);
6285 status = task->tk_status;
6286 dprintk("<-- %s status=%d\n", __func__, status);
6287 rpc_put_task(task);
6288 return status;
6289}
6290
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006291/*
6292 * Retrieve the list of Data Server devices from the MDS.
6293 */
6294static int _nfs4_getdevicelist(struct nfs_server *server,
6295 const struct nfs_fh *fh,
6296 struct pnfs_devicelist *devlist)
6297{
6298 struct nfs4_getdevicelist_args args = {
6299 .fh = fh,
6300 .layoutclass = server->pnfs_curr_ld->id,
6301 };
6302 struct nfs4_getdevicelist_res res = {
6303 .devlist = devlist,
6304 };
6305 struct rpc_message msg = {
6306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6307 .rpc_argp = &args,
6308 .rpc_resp = &res,
6309 };
6310 int status;
6311
6312 dprintk("--> %s\n", __func__);
6313 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6314 &res.seq_res, 0);
6315 dprintk("<-- %s status=%d\n", __func__, status);
6316 return status;
6317}
6318
6319int nfs4_proc_getdevicelist(struct nfs_server *server,
6320 const struct nfs_fh *fh,
6321 struct pnfs_devicelist *devlist)
6322{
6323 struct nfs4_exception exception = { };
6324 int err;
6325
6326 do {
6327 err = nfs4_handle_exception(server,
6328 _nfs4_getdevicelist(server, fh, devlist),
6329 &exception);
6330 } while (exception.retry);
6331
6332 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6333 err, devlist->num_devs);
6334
6335 return err;
6336}
6337EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6338
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006339static int
6340_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6341{
6342 struct nfs4_getdeviceinfo_args args = {
6343 .pdev = pdev,
6344 };
6345 struct nfs4_getdeviceinfo_res res = {
6346 .pdev = pdev,
6347 };
6348 struct rpc_message msg = {
6349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6350 .rpc_argp = &args,
6351 .rpc_resp = &res,
6352 };
6353 int status;
6354
6355 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006356 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006357 dprintk("<-- %s status=%d\n", __func__, status);
6358
6359 return status;
6360}
6361
6362int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6363{
6364 struct nfs4_exception exception = { };
6365 int err;
6366
6367 do {
6368 err = nfs4_handle_exception(server,
6369 _nfs4_proc_getdeviceinfo(server, pdev),
6370 &exception);
6371 } while (exception.retry);
6372 return err;
6373}
6374EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6375
Andy Adamson863a3c62011-03-23 13:27:54 +00006376static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6377{
6378 struct nfs4_layoutcommit_data *data = calldata;
6379 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006380 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006381
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006382 nfs41_setup_sequence(session,
6383 &data->args.seq_args,
6384 &data->res.seq_res,
6385 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006386}
6387
6388static void
6389nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6390{
6391 struct nfs4_layoutcommit_data *data = calldata;
6392 struct nfs_server *server = NFS_SERVER(data->args.inode);
6393
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006394 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006395 return;
6396
6397 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006398 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6399 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6400 case -NFS4ERR_BADLAYOUT: /* no layout */
6401 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006402 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006403 break;
6404 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006405 nfs_post_op_update_inode_force_wcc(data->args.inode,
6406 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006407 break;
6408 default:
6409 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6410 rpc_restart_call_prepare(task);
6411 return;
6412 }
6413 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006414}
6415
6416static void nfs4_layoutcommit_release(void *calldata)
6417{
6418 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006419 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006420 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006421
Andy Adamsondb29c082011-07-30 20:52:38 -04006422 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006423 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006424 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6425 list_del_init(&lseg->pls_lc_list);
6426 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6427 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006428 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006429 }
Peng Tao92407e72011-10-23 20:21:17 -07006430
6431 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6432 smp_mb__after_clear_bit();
6433 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6434
Andy Adamson863a3c62011-03-23 13:27:54 +00006435 put_rpccred(data->cred);
6436 kfree(data);
6437}
6438
6439static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6440 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6441 .rpc_call_done = nfs4_layoutcommit_done,
6442 .rpc_release = nfs4_layoutcommit_release,
6443};
6444
6445int
Andy Adamsonef311532011-03-12 02:58:10 -05006446nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006447{
6448 struct rpc_message msg = {
6449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6450 .rpc_argp = &data->args,
6451 .rpc_resp = &data->res,
6452 .rpc_cred = data->cred,
6453 };
6454 struct rpc_task_setup task_setup_data = {
6455 .task = &data->task,
6456 .rpc_client = NFS_CLIENT(data->args.inode),
6457 .rpc_message = &msg,
6458 .callback_ops = &nfs4_layoutcommit_ops,
6459 .callback_data = data,
6460 .flags = RPC_TASK_ASYNC,
6461 };
6462 struct rpc_task *task;
6463 int status = 0;
6464
6465 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6466 "lbw: %llu inode %lu\n",
6467 data->task.tk_pid, sync,
6468 data->args.lastbytewritten,
6469 data->args.inode->i_ino);
6470
Trond Myklebust9d12b212012-01-17 22:04:25 -05006471 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006472 task = rpc_run_task(&task_setup_data);
6473 if (IS_ERR(task))
6474 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006475 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006476 goto out;
6477 status = nfs4_wait_for_completion_rpc_task(task);
6478 if (status != 0)
6479 goto out;
6480 status = task->tk_status;
6481out:
6482 dprintk("%s: status %d\n", __func__, status);
6483 rpc_put_task(task);
6484 return status;
6485}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006486
6487static int
6488_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6489 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6490{
6491 struct nfs41_secinfo_no_name_args args = {
6492 .style = SECINFO_STYLE_CURRENT_FH,
6493 };
6494 struct nfs4_secinfo_res res = {
6495 .flavors = flavors,
6496 };
6497 struct rpc_message msg = {
6498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6499 .rpc_argp = &args,
6500 .rpc_resp = &res,
6501 };
6502 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6503}
6504
6505static int
6506nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6507 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6508{
6509 struct nfs4_exception exception = { };
6510 int err;
6511 do {
6512 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6513 switch (err) {
6514 case 0:
6515 case -NFS4ERR_WRONGSEC:
6516 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006517 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006518 default:
6519 err = nfs4_handle_exception(server, err, &exception);
6520 }
6521 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006522out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006523 return err;
6524}
6525
6526static int
6527nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6528 struct nfs_fsinfo *info)
6529{
6530 int err;
6531 struct page *page;
6532 rpc_authflavor_t flavor;
6533 struct nfs4_secinfo_flavors *flavors;
6534
6535 page = alloc_page(GFP_KERNEL);
6536 if (!page) {
6537 err = -ENOMEM;
6538 goto out;
6539 }
6540
6541 flavors = page_address(page);
6542 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6543
6544 /*
6545 * Fall back on "guess and check" method if
6546 * the server doesn't support SECINFO_NO_NAME
6547 */
6548 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6549 err = nfs4_find_root_sec(server, fhandle, info);
6550 goto out_freepage;
6551 }
6552 if (err)
6553 goto out_freepage;
6554
6555 flavor = nfs_find_best_sec(flavors);
6556 if (err == 0)
6557 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6558
6559out_freepage:
6560 put_page(page);
6561 if (err == -EACCES)
6562 return -EPERM;
6563out:
6564 return err;
6565}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006566
6567static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006568{
6569 int status;
6570 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006571 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006572 };
6573 struct nfs41_test_stateid_res res;
6574 struct rpc_message msg = {
6575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6576 .rpc_argp = &args,
6577 .rpc_resp = &res,
6578 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006579
Chuck Lever38527b12012-07-11 16:30:23 -04006580 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006581 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006582 nfs4_set_sequence_privileged(&args.seq_args);
6583 status = nfs4_call_sync_sequence(server->client, server, &msg,
6584 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006585 if (status != NFS_OK) {
6586 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006587 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006588 }
6589 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006590 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006591}
6592
Chuck Lever38527b12012-07-11 16:30:23 -04006593/**
6594 * nfs41_test_stateid - perform a TEST_STATEID operation
6595 *
6596 * @server: server / transport on which to perform the operation
6597 * @stateid: state ID to test
6598 *
6599 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6600 * Otherwise a negative NFS4ERR value is returned if the operation
6601 * failed or the state ID is not currently valid.
6602 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006603static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006604{
6605 struct nfs4_exception exception = { };
6606 int err;
6607 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006608 err = _nfs41_test_stateid(server, stateid);
6609 if (err != -NFS4ERR_DELAY)
6610 break;
6611 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006612 } while (exception.retry);
6613 return err;
6614}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006615
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006616static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006617{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006618 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006619 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006620 };
6621 struct nfs41_free_stateid_res res;
6622 struct rpc_message msg = {
6623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6624 .rpc_argp = &args,
6625 .rpc_resp = &res,
6626 };
Chuck Lever38527b12012-07-11 16:30:23 -04006627 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006628
Chuck Lever38527b12012-07-11 16:30:23 -04006629 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006630 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006631 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006632 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006633 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006634 dprintk("NFS reply free_stateid: %d\n", status);
6635 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006636}
6637
Chuck Lever38527b12012-07-11 16:30:23 -04006638/**
6639 * nfs41_free_stateid - perform a FREE_STATEID operation
6640 *
6641 * @server: server / transport on which to perform the operation
6642 * @stateid: state ID to release
6643 *
6644 * Returns NFS_OK if the server freed "stateid". Otherwise a
6645 * negative NFS4ERR value is returned.
6646 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006647static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006648{
6649 struct nfs4_exception exception = { };
6650 int err;
6651 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006652 err = _nfs4_free_stateid(server, stateid);
6653 if (err != -NFS4ERR_DELAY)
6654 break;
6655 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006656 } while (exception.retry);
6657 return err;
6658}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006659
6660static bool nfs41_match_stateid(const nfs4_stateid *s1,
6661 const nfs4_stateid *s2)
6662{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006663 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006664 return false;
6665
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006666 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006667 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006668 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006669 return true;
6670
6671 return false;
6672}
6673
Andy Adamson557134a2009-04-01 09:21:53 -04006674#endif /* CONFIG_NFS_V4_1 */
6675
Trond Myklebust36281ca2012-03-04 18:13:56 -05006676static bool nfs4_match_stateid(const nfs4_stateid *s1,
6677 const nfs4_stateid *s2)
6678{
Trond Myklebustf597c532012-03-04 18:13:56 -05006679 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006680}
6681
6682
Trond Myklebust17280172012-03-11 13:11:00 -04006683static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006684 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006685 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 .recover_open = nfs4_open_reclaim,
6687 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006688 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006689 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006690 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691};
6692
Andy Adamson591d71c2009-04-01 09:22:47 -04006693#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006694static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006695 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6696 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6697 .recover_open = nfs4_open_reclaim,
6698 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006699 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006700 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006701 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006702 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006703};
6704#endif /* CONFIG_NFS_V4_1 */
6705
Trond Myklebust17280172012-03-11 13:11:00 -04006706static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006707 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006708 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 .recover_open = nfs4_open_expired,
6710 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006711 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006712 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713};
6714
Andy Adamson591d71c2009-04-01 09:22:47 -04006715#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006716static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006717 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6718 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006719 .recover_open = nfs41_open_expired,
6720 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006721 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006722 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006723};
6724#endif /* CONFIG_NFS_V4_1 */
6725
Trond Myklebust17280172012-03-11 13:11:00 -04006726static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006727 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006728 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006729 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006730};
6731
6732#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006733static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006734 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006735 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006736 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006737};
6738#endif
6739
Trond Myklebust97dc1352010-06-16 09:52:26 -04006740static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6741 .minor_version = 0,
6742 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006743 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006744 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006745 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6746 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6747 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006748};
6749
6750#if defined(CONFIG_NFS_V4_1)
6751static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6752 .minor_version = 1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006753 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006754 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006755 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006756 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6757 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6758 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006759};
6760#endif
6761
Trond Myklebust97dc1352010-06-16 09:52:26 -04006762const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6763 [0] = &nfs_v4_0_minor_ops,
6764#if defined(CONFIG_NFS_V4_1)
6765 [1] = &nfs_v4_1_minor_ops,
6766#endif
6767};
6768
Bryan Schumaker73a79702012-07-16 16:39:12 -04006769const struct inode_operations nfs4_dir_inode_operations = {
6770 .create = nfs_create,
6771 .lookup = nfs_lookup,
6772 .atomic_open = nfs_atomic_open,
6773 .link = nfs_link,
6774 .unlink = nfs_unlink,
6775 .symlink = nfs_symlink,
6776 .mkdir = nfs_mkdir,
6777 .rmdir = nfs_rmdir,
6778 .mknod = nfs_mknod,
6779 .rename = nfs_rename,
6780 .permission = nfs_permission,
6781 .getattr = nfs_getattr,
6782 .setattr = nfs_setattr,
6783 .getxattr = generic_getxattr,
6784 .setxattr = generic_setxattr,
6785 .listxattr = generic_listxattr,
6786 .removexattr = generic_removexattr,
6787};
6788
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006789static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006790 .permission = nfs_permission,
6791 .getattr = nfs_getattr,
6792 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006793 .getxattr = generic_getxattr,
6794 .setxattr = generic_setxattr,
6795 .listxattr = generic_listxattr,
6796 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006797};
6798
David Howells509de812006-08-22 20:06:11 -04006799const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 .version = 4, /* protocol version */
6801 .dentry_ops = &nfs4_dentry_operations,
6802 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006803 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006804 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006806 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006807 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 .getattr = nfs4_proc_getattr,
6809 .setattr = nfs4_proc_setattr,
6810 .lookup = nfs4_proc_lookup,
6811 .access = nfs4_proc_access,
6812 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 .create = nfs4_proc_create,
6814 .remove = nfs4_proc_remove,
6815 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006816 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 .unlink_done = nfs4_proc_unlink_done,
6818 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006819 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006820 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006821 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 .link = nfs4_proc_link,
6823 .symlink = nfs4_proc_symlink,
6824 .mkdir = nfs4_proc_mkdir,
6825 .rmdir = nfs4_proc_remove,
6826 .readdir = nfs4_proc_readdir,
6827 .mknod = nfs4_proc_mknod,
6828 .statfs = nfs4_proc_statfs,
6829 .fsinfo = nfs4_proc_fsinfo,
6830 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006831 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 .decode_dirent = nfs4_decode_dirent,
6833 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006834 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006835 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006836 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006838 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006839 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006840 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006842 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006843 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006845 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006846 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006847 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006848 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006849 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006850 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006851 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006852 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006853 .create_server = nfs4_create_server,
6854 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855};
6856
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006857static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6858 .prefix = XATTR_NAME_NFSV4_ACL,
6859 .list = nfs4_xattr_list_nfs4_acl,
6860 .get = nfs4_xattr_get_nfs4_acl,
6861 .set = nfs4_xattr_set_nfs4_acl,
6862};
6863
6864const struct xattr_handler *nfs4_xattr_handlers[] = {
6865 &nfs4_xattr_nfs4_acl_handler,
6866 NULL
6867};
6868
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869/*
6870 * Local variables:
6871 * c-basic-offset: 8
6872 * End:
6873 */