blob: 1465364501baae8b182af5ca1eac23883f114770 [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>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010058#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebust4ce79712005-06-22 17:16:21 +000060#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050062#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050063#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040064#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040065#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040066#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustcdd4e682006-01-03 09:55:12 +010075struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010076static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080077static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050079static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050080static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040081static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040082static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040083static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050091static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Trond Myklebust52567b02009-10-23 14:46:42 -040093 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
97 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000098 case -NFS4ERR_WRONGSEC:
99 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
102 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400103 case -NFS4ERR_SHARE_DENIED:
104 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400107 case -NFS4ERR_ACCESS:
108 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700111 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400114 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/*
118 * This is our standard bitmap for GETATTR requests.
119 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400120const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 FATTR4_WORD0_TYPE
122 | FATTR4_WORD0_CHANGE
123 | FATTR4_WORD0_SIZE
124 | FATTR4_WORD0_FSID
125 | FATTR4_WORD0_FILEID,
126 FATTR4_WORD1_MODE
127 | FATTR4_WORD1_NUMLINKS
128 | FATTR4_WORD1_OWNER
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
135};
136
Trond Myklebust1549210f2012-06-05 09:16:47 -0400137static const u32 nfs4_pnfs_open_bitmap[3] = {
138 FATTR4_WORD0_TYPE
139 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_SIZE
141 | FATTR4_WORD0_FSID
142 | FATTR4_WORD0_FILEID,
143 FATTR4_WORD1_MODE
144 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
153};
154
Andy Adamsone23008e2012-10-02 21:07:32 -0400155static const u32 nfs4_open_noattr_bitmap[3] = {
156 FATTR4_WORD0_TYPE
157 | FATTR4_WORD0_CHANGE
158 | FATTR4_WORD0_FILEID,
159};
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161const u32 nfs4_statfs_bitmap[2] = {
162 FATTR4_WORD0_FILES_AVAIL
163 | FATTR4_WORD0_FILES_FREE
164 | FATTR4_WORD0_FILES_TOTAL,
165 FATTR4_WORD1_SPACE_AVAIL
166 | FATTR4_WORD1_SPACE_FREE
167 | FATTR4_WORD1_SPACE_TOTAL
168};
169
Trond Myklebust4ce79712005-06-22 17:16:21 +0000170const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 FATTR4_WORD0_MAXLINK
172 | FATTR4_WORD0_MAXNAME,
173 0
174};
175
Fred Isamandae100c2011-07-30 20:52:37 -0400176const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 | FATTR4_WORD0_MAXREAD
178 | FATTR4_WORD0_MAXWRITE
179 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700180 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400181 | FATTR4_WORD1_FS_LAYOUT_TYPES,
182 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184
Manoj Naik830b8e32006-06-09 09:34:25 -0400185const u32 nfs4_fs_locations_bitmap[2] = {
186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID
191 | FATTR4_WORD0_FS_LOCATIONS,
192 FATTR4_WORD1_MODE
193 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER
195 | FATTR4_WORD1_OWNER_GROUP
196 | FATTR4_WORD1_RAWDEV
197 | FATTR4_WORD1_SPACE_USED
198 | FATTR4_WORD1_TIME_ACCESS
199 | FATTR4_WORD1_TIME_METADATA
200 | FATTR4_WORD1_TIME_MODIFY
201 | FATTR4_WORD1_MOUNTED_ON_FILEID
202};
203
Al Virobc4785c2006-10-19 23:28:51 -0700204static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct nfs4_readdir_arg *readdir)
206{
Al Viro0dbb4c62006-10-19 23:28:49 -0700207 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 BUG_ON(readdir->count < 80);
210 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_wait_clnt_recover(struct nfs_client *clp)
260{
261 int res;
262
263 might_sleep();
264
Trond Myklebuste005e802008-12-23 15:21:48 -0500265 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400266 nfs_wait_bit_killable, TASK_KILLABLE);
Chuck Lever46a87b82012-07-11 16:30:41 -0400267 if (res)
268 return res;
269
270 if (clp->cl_cons_state < 0)
271 return clp->cl_cons_state;
272 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500273}
274
275static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
276{
277 int res = 0;
278
279 might_sleep();
280
281 if (*timeout <= 0)
282 *timeout = NFS4_POLL_RETRY_MIN;
283 if (*timeout > NFS4_POLL_RETRY_MAX)
284 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100285 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 if (fatal_signal_pending(current))
287 res = -ERESTARTSYS;
288 *timeout <<= 1;
289 return res;
290}
291
292/* This is the error handling routine for processes that are allowed
293 * to sleep.
294 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800295static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500296{
297 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500298 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500299 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500300 int ret = errorcode;
301
302 exception->retry = 0;
303 switch(errorcode) {
304 case 0:
305 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500306 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400307 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400308 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500309 exception->retry = 1;
310 return 0;
311 }
312 if (state == NULL)
313 break;
314 nfs4_schedule_stateid_recovery(server, state);
315 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500316 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500317 case -NFS4ERR_ADMIN_REVOKED:
318 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500319 if (state == NULL)
320 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400321 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500322 nfs4_schedule_stateid_recovery(server, state);
323 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400324 case -NFS4ERR_EXPIRED:
325 if (state != NULL)
326 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500327 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500328 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500329 nfs4_schedule_lease_recovery(clp);
330 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500331#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400332 case -NFS4ERR_BADSESSION:
333 case -NFS4ERR_BADSLOT:
334 case -NFS4ERR_BAD_HIGH_SLOT:
335 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
336 case -NFS4ERR_DEADSESSION:
337 case -NFS4ERR_SEQ_FALSE_RETRY:
338 case -NFS4ERR_SEQ_MISORDERED:
339 dprintk("%s ERROR: %d Reset session\n", __func__,
340 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400341 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400342 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500343#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500344 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500345 if (exception->timeout > HZ) {
346 /* We have retried a decent amount, time to
347 * fail
348 */
349 ret = -EBUSY;
350 break;
351 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500352 case -NFS4ERR_GRACE:
353 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500354 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 ret = nfs4_delay(server->client, &exception->timeout);
356 if (ret != 0)
357 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400358 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500359 case -NFS4ERR_OLD_STATEID:
360 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800361 break;
362 case -NFS4ERR_BADOWNER:
363 /* The following works around a Linux server bug! */
364 case -NFS4ERR_BADNAME:
365 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
366 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
367 exception->retry = 1;
368 printk(KERN_WARNING "NFS: v4 server %s "
369 "does not accept raw "
370 "uid/gids. "
371 "Reenabling the idmapper.\n",
372 server->nfs_client->cl_hostname);
373 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500374 }
375 /* We failed to handle the error */
376 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500377wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500378 ret = nfs4_wait_clnt_recover(clp);
379 if (ret == 0)
380 exception->retry = 1;
381 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500382}
383
384
Trond Myklebust452e9352010-07-31 14:29:06 -0400385static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 spin_lock(&clp->cl_lock);
388 if (time_before(clp->cl_last_renewal,timestamp))
389 clp->cl_last_renewal = timestamp;
390 spin_unlock(&clp->cl_lock);
391}
392
Trond Myklebust452e9352010-07-31 14:29:06 -0400393static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
394{
395 do_renew_lease(server->nfs_client, timestamp);
396}
397
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400398#if defined(CONFIG_NFS_V4_1)
399
Benny Halevy510b8172009-04-01 09:22:14 -0400400/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400401 * nfs4_free_slot - free a slot and efficiently update slot table.
402 *
403 * freeing a slot is trivially done by clearing its respective bit
404 * in the bitmap.
405 * If the freed slotid equals highest_used_slotid we want to update it
406 * so that the server would be able to size down the slot table if needed,
407 * otherwise we know that the highest_used_slotid is still in use.
408 * When updating highest_used_slotid there may be "holes" in the bitmap
409 * so we need to scan down from highest_used_slotid to 0 looking for the now
410 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500411 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500412 *
413 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400414 */
415static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500416nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400417{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500418 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400419 /* clear used bit in bitmap */
420 __clear_bit(slotid, tbl->used_slots);
421
422 /* update highest_used_slotid when it is freed */
423 if (slotid == tbl->highest_used_slotid) {
424 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500425 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400426 tbl->highest_used_slotid = slotid;
427 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500428 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400429 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500430 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
431 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400432}
433
Trond Myklebust961a8282012-01-17 22:57:37 -0500434bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
435{
436 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
437 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400438}
439
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800440/*
Andy Adamson42acd022011-01-06 02:04:34 +0000441 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800442 */
Andy Adamson42acd022011-01-06 02:04:34 +0000443static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800444{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400445 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500446 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
447 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800448 return;
449 }
450
Trond Myklebust45d43c22012-02-06 19:38:51 -0500451 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800452 return;
453
Andy Adamson42acd022011-01-06 02:04:34 +0000454 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
455 complete(&ses->fc_slot_table.complete);
456}
457
458/*
459 * Signal state manager thread if session back channel is drained
460 */
461void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
462{
463 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500464 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000465 return;
466 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
467 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800468}
469
Trond Myklebustd185a332010-06-16 09:52:25 -0400470static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400471{
472 struct nfs4_slot_table *tbl;
473
Trond Myklebustd185a332010-06-16 09:52:25 -0400474 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400475 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400476 /* just wake up the next guy waiting since
477 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500478 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500479 return;
Andy Adamson13615872009-04-01 09:22:17 -0400480 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500481
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500482 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500483 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000484 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500485 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400486 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400487}
488
Trond Myklebust14516c32010-07-31 14:29:06 -0400489static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400490{
491 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400492 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400493
494 /*
495 * sr_status remains 1 if an RPC level error occurred. The server
496 * may or may not have processed the sequence operation..
497 * Proceed as if the server received and processed the sequence
498 * operation.
499 */
500 if (res->sr_status == 1)
501 res->sr_status = NFS_OK;
502
Bryan Schumaker468f8612011-04-18 15:57:32 -0400503 /* don't increment the sequence number if the task wasn't sent */
504 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400505 goto out;
506
Andy Adamson691daf32009-12-04 15:55:39 -0500507 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400508 switch (res->sr_status) {
509 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400510 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400511 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400512 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400514 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500515 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500516 if (res->sr_status_flags != 0)
517 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400518 break;
519 case -NFS4ERR_DELAY:
520 /* The server detected a resend of the RPC call and
521 * returned NFS4ERR_DELAY as per Section 2.10.6.2
522 * of RFC5661.
523 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200524 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400525 __func__,
526 res->sr_slot - res->sr_session->fc_slot_table.slots,
527 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400528 goto out_retry;
529 default:
530 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400531 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400532 }
533out:
534 /* The session may be reset by one of the error handlers. */
535 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400536 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400537 return 1;
538out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400539 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400540 goto out;
541 rpc_delay(task, NFS4_POLL_RETRY_MAX);
542 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400543}
544
Trond Myklebust14516c32010-07-31 14:29:06 -0400545static int nfs4_sequence_done(struct rpc_task *task,
546 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400547{
Trond Myklebust14516c32010-07-31 14:29:06 -0400548 if (res->sr_session == NULL)
549 return 1;
550 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400551}
552
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400553/*
Benny Halevy510b8172009-04-01 09:22:14 -0400554 * nfs4_find_slot - efficiently look for a free slot
555 *
556 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
557 * If found, we mark the slot as used, update the highest_used_slotid,
558 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500559 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400560 *
561 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400562 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500563static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800564nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400565{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500566 u32 slotid;
567 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400568
Trond Myklebust45d43c22012-02-06 19:38:51 -0500569 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400570 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
571 tbl->max_slots);
572 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
573 if (slotid >= tbl->max_slots)
574 goto out;
575 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500576 if (slotid > tbl->highest_used_slotid ||
577 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400578 tbl->highest_used_slotid = slotid;
579 ret_id = slotid;
580out:
581 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
582 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400583 return ret_id;
584}
585
Trond Myklebust9d12b212012-01-17 22:04:25 -0500586static void nfs41_init_sequence(struct nfs4_sequence_args *args,
587 struct nfs4_sequence_res *res, int cache_reply)
588{
589 args->sa_session = NULL;
590 args->sa_cache_this = 0;
591 if (cache_reply)
592 args->sa_cache_this = 1;
593 res->sr_session = NULL;
594 res->sr_slot = NULL;
595}
596
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000597int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400598 struct nfs4_sequence_args *args,
599 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600 struct rpc_task *task)
601{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400602 struct nfs4_slot *slot;
603 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500604 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400605
606 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400608 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400609 return 0;
610
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400611 tbl = &session->fc_slot_table;
612
613 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400614 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800615 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500616 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500617 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400618 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500619 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500620 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400621 }
622
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800623 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
624 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
625 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
626 spin_unlock(&tbl->slot_tbl_lock);
627 dprintk("%s enforce FIFO order\n", __func__);
628 return -EAGAIN;
629 }
630
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800631 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500632 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400633 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
634 spin_unlock(&tbl->slot_tbl_lock);
635 dprintk("<-- %s: no free slots\n", __func__);
636 return -EAGAIN;
637 }
638 spin_unlock(&tbl->slot_tbl_lock);
639
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800640 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400641 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400642 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400643 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400644
645 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
646
Benny Halevy99fe60d2009-04-01 09:22:29 -0400647 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400648 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400649 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400650 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400651 /*
652 * sr_status is only set in decode_sequence, and so will remain
653 * set to 1 if an rpc level failure occurs.
654 */
655 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 return 0;
657}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000658EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659
Trond Myklebust035168ab2010-06-16 09:52:26 -0400660int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 struct nfs4_sequence_args *args,
662 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663 struct rpc_task *task)
664{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400665 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666 int ret = 0;
667
Trond Myklebust9d12b212012-01-17 22:04:25 -0500668 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669 goto out;
Trond Myklebust035168ab2010-06-16 09:52:26 -0400670
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200671 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400672 __func__, session->clp, session, res->sr_slot ?
673 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400674
Trond Myklebust9d12b212012-01-17 22:04:25 -0500675 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676out:
677 dprintk("<-- %s status=%d\n", __func__, ret);
678 return ret;
679}
680
681struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400682 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 struct nfs4_sequence_args *seq_args;
684 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400685};
686
687static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
688{
689 struct nfs41_call_sync_data *data = calldata;
690
Trond Myklebust035168ab2010-06-16 09:52:26 -0400691 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
692
693 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500694 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400695 return;
696 rpc_call_start(task);
697}
698
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800699static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
700{
701 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
702 nfs41_call_sync_prepare(task, calldata);
703}
704
Andy Adamson69ab40c2009-04-01 09:22:19 -0400705static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
706{
707 struct nfs41_call_sync_data *data = calldata;
708
Trond Myklebust14516c32010-07-31 14:29:06 -0400709 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400710}
711
Trond Myklebust17280172012-03-11 13:11:00 -0400712static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400714 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400715};
716
Trond Myklebust17280172012-03-11 13:11:00 -0400717static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800718 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
719 .rpc_call_done = nfs41_call_sync_done,
720};
721
Bryan Schumaker7c513052011-03-24 17:12:24 +0000722static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
723 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400724 struct rpc_message *msg,
725 struct nfs4_sequence_args *args,
726 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800727 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400728{
729 int ret;
730 struct rpc_task *task;
731 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400732 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400733 .seq_args = args,
734 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400735 };
736 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000737 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400738 .rpc_message = msg,
739 .callback_ops = &nfs41_call_sync_ops,
740 .callback_data = &data
741 };
742
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800743 if (privileged)
744 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400745 task = rpc_run_task(&task_setup);
746 if (IS_ERR(task))
747 ret = PTR_ERR(task);
748 else {
749 ret = task->tk_status;
750 rpc_put_task(task);
751 }
752 return ret;
753}
754
Bryan Schumaker7c513052011-03-24 17:12:24 +0000755int _nfs4_call_sync_session(struct rpc_clnt *clnt,
756 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400757 struct rpc_message *msg,
758 struct nfs4_sequence_args *args,
759 struct nfs4_sequence_res *res,
760 int cache_reply)
761{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500762 nfs41_init_sequence(args, res, cache_reply);
763 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400764}
765
Trond Myklebustdf896452010-06-16 09:52:26 -0400766#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500767static inline
768void nfs41_init_sequence(struct nfs4_sequence_args *args,
769 struct nfs4_sequence_res *res, int cache_reply)
770{
771}
772
Trond Myklebust14516c32010-07-31 14:29:06 -0400773static int nfs4_sequence_done(struct rpc_task *task,
774 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400775{
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400777}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400778#endif /* CONFIG_NFS_V4_1 */
779
Bryan Schumaker7c513052011-03-24 17:12:24 +0000780int _nfs4_call_sync(struct rpc_clnt *clnt,
781 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400782 struct rpc_message *msg,
783 struct nfs4_sequence_args *args,
784 struct nfs4_sequence_res *res,
785 int cache_reply)
786{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500787 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000788 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400789}
790
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000791static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000792int nfs4_call_sync(struct rpc_clnt *clnt,
793 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000794 struct rpc_message *msg,
795 struct nfs4_sequence_args *args,
796 struct nfs4_sequence_res *res,
797 int cache_reply)
798{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000799 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
800 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000801}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400802
Trond Myklebust38478b22006-05-25 01:40:57 -0400803static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Trond Myklebust38478b22006-05-25 01:40:57 -0400805 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Trond Myklebust38478b22006-05-25 01:40:57 -0400807 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400808 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700809 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400810 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700811 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400812 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400816 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817 struct nfs_openargs o_arg;
818 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100819 struct nfs_open_confirmargs c_arg;
820 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500821 struct nfs4_string owner_name;
822 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400825 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400827 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100829 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400830 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100831 int rpc_status;
832 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100833};
834
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400835
836static void nfs4_init_opendata_res(struct nfs4_opendata *p)
837{
838 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400839 p->o_res.seqid = p->o_arg.seqid;
840 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400841 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400842 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400843 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500844 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400845}
846
Al Viro82a2c1b2011-06-22 18:30:55 -0400847static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500848 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400849 const struct iattr *attrs,
850 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100851{
Al Viro82a2c1b2011-06-22 18:30:55 -0400852 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 struct inode *dir = parent->d_inode;
854 struct nfs_server *server = NFS_SERVER(dir);
855 struct nfs4_opendata *p;
856
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400857 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858 if (p == NULL)
859 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400860 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100861 if (p->o_arg.seqid == NULL)
862 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400863 nfs_sb_active(dentry->d_sb);
864 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100865 p->dir = parent;
866 p->owner = sp;
867 atomic_inc(&sp->so_count);
868 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869 p->o_arg.open_flags = flags;
870 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700871 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
872 * will return permission denied for all bits until close */
873 if (!(flags & O_EXCL)) {
874 /* ask server to check for all possible rights as results
875 * are cached */
876 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
877 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700878 }
David Howells7539bba2006-08-22 20:06:09 -0400879 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400880 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
881 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400882 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100883 p->o_arg.server = server;
884 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400885 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500887 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500888 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400889
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100890 p->o_arg.u.attrs = &p->attrs;
891 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500892
893 verf[0] = jiffies;
894 verf[1] = current->pid;
895 memcpy(p->o_arg.u.verifier.data, verf,
896 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100897 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100898 p->c_arg.fh = &p->o_res.fh;
899 p->c_arg.stateid = &p->o_res.stateid;
900 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400901 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400902 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100903 return p;
904err_free:
905 kfree(p);
906err:
907 dput(parent);
908 return NULL;
909}
910
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400911static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100912{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400913 struct nfs4_opendata *p = container_of(kref,
914 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400915 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400916
917 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400918 if (p->state != NULL)
919 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400920 nfs4_put_state_owner(p->owner);
921 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400922 dput(p->dentry);
923 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500924 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400925 kfree(p);
926}
927
928static void nfs4_opendata_put(struct nfs4_opendata *p)
929{
930 if (p != NULL)
931 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100932}
933
Trond Myklebust06f814a2006-01-03 09:55:07 +0100934static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
935{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100936 int ret;
937
Trond Myklebust06f814a2006-01-03 09:55:07 +0100938 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100939 return ret;
940}
941
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400943{
944 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945
Trond Myklebust536e43d2012-01-17 22:04:26 -0500946 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947 goto out;
948 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400949 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500950 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
951 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400952 break;
953 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500954 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
955 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400956 break;
957 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500958 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
959 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400960 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400962 return ret;
963}
964
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400966{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500967 if (delegation == NULL)
968 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400970 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500971 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400972 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500973 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974 return 1;
975}
976
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100978{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100980 case FMODE_WRITE:
981 state->n_wronly++;
982 break;
983 case FMODE_READ:
984 state->n_rdonly++;
985 break;
986 case FMODE_READ|FMODE_WRITE:
987 state->n_rdwr++;
988 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100990}
991
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400993{
994 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500995 nfs4_stateid_copy(&state->stateid, stateid);
996 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400998 case FMODE_READ:
999 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1000 break;
1001 case FMODE_WRITE:
1002 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1003 break;
1004 case FMODE_READ|FMODE_WRITE:
1005 set_bit(NFS_O_RDWR_STATE, &state->flags);
1006 }
1007}
1008
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001010{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001011 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001013 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001014}
1015
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001016static 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 -07001017{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001018 /*
1019 * Protect the call to nfs4_state_set_mode_locked and
1020 * serialise the stateid update
1021 */
1022 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001023 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001024 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001025 set_bit(NFS_DELEGATED_STATE, &state->flags);
1026 }
1027 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001028 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001029 write_sequnlock(&state->seqlock);
1030 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001032 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035static 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 -05001036{
1037 struct nfs_inode *nfsi = NFS_I(state->inode);
1038 struct nfs_delegation *deleg_cur;
1039 int ret = 0;
1040
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001041 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001042
1043 rcu_read_lock();
1044 deleg_cur = rcu_dereference(nfsi->delegation);
1045 if (deleg_cur == NULL)
1046 goto no_delegation;
1047
1048 spin_lock(&deleg_cur->lock);
1049 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001050 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001051 goto no_delegation_unlock;
1052
1053 if (delegation == NULL)
1054 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001055 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001056 goto no_delegation_unlock;
1057
Trond Myklebustb7391f42008-12-23 15:21:52 -05001058 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001060 ret = 1;
1061no_delegation_unlock:
1062 spin_unlock(&deleg_cur->lock);
1063no_delegation:
1064 rcu_read_unlock();
1065
1066 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001067 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001068 ret = 1;
1069 }
1070
1071 return ret;
1072}
1073
1074
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001075static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076{
1077 struct nfs_delegation *delegation;
1078
1079 rcu_read_lock();
1080 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001082 rcu_read_unlock();
1083 return;
1084 }
1085 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001086 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001087}
1088
Trond Myklebust6ee41262007-07-08 14:11:36 -04001089static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001090{
1091 struct nfs4_state *state = opendata->state;
1092 struct nfs_inode *nfsi = NFS_I(state->inode);
1093 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001094 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001096 nfs4_stateid stateid;
1097 int ret = -EAGAIN;
1098
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001100 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001101 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102 if (can_open_cached(state, fmode, open_mode)) {
1103 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001104 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001105 goto out_return_state;
1106 }
1107 spin_unlock(&state->owner->so_lock);
1108 }
Trond Myklebust34310432008-12-23 15:21:38 -05001109 rcu_read_lock();
1110 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001111 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001112 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001113 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001114 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001115 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001116 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001118 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001119 if (ret != 0)
1120 goto out;
1121 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001122
1123 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001124 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001125 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001126 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127out:
1128 return ERR_PTR(ret);
1129out_return_state:
1130 atomic_inc(&state->count);
1131 return state;
1132}
1133
Andy Adamsone23008e2012-10-02 21:07:32 -04001134static void
1135nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1136{
1137 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1138 struct nfs_delegation *delegation;
1139 int delegation_flags = 0;
1140
1141 rcu_read_lock();
1142 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1143 if (delegation)
1144 delegation_flags = delegation->flags;
1145 rcu_read_unlock();
1146 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1147 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1148 "returning a delegation for "
1149 "OPEN(CLAIM_DELEGATE_CUR)\n",
1150 clp->cl_hostname);
1151 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1152 nfs_inode_set_delegation(state->inode,
1153 data->owner->so_cred,
1154 &data->o_res);
1155 else
1156 nfs_inode_reclaim_delegation(state->inode,
1157 data->owner->so_cred,
1158 &data->o_res);
1159}
1160
1161/*
1162 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1163 * and update the nfs4_state.
1164 */
1165static struct nfs4_state *
1166_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1167{
1168 struct inode *inode = data->state->inode;
1169 struct nfs4_state *state = data->state;
1170 int ret;
1171
1172 if (!data->rpc_done) {
1173 ret = data->rpc_status;
1174 goto err;
1175 }
1176
1177 ret = -ESTALE;
1178 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1179 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1180 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1181 goto err;
1182
1183 ret = -ENOMEM;
1184 state = nfs4_get_open_state(inode, data->owner);
1185 if (state == NULL)
1186 goto err;
1187
1188 ret = nfs_refresh_inode(inode, &data->f_attr);
1189 if (ret)
1190 goto err;
1191
1192 if (data->o_res.delegation_type != 0)
1193 nfs4_opendata_check_deleg(data, state);
1194 update_open_stateid(state, &data->o_res.stateid, NULL,
1195 data->o_arg.fmode);
1196
1197 return state;
1198err:
1199 return ERR_PTR(ret);
1200
1201}
1202
1203static struct nfs4_state *
1204_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001205{
1206 struct inode *inode;
1207 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001208 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001209
Trond Myklebustaac00a82007-07-05 19:02:21 -04001210 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001211 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001212 goto out;
1213 }
1214
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001215 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001216 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001217 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001218 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001219 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001220 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001221 goto err;
1222 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001223 state = nfs4_get_open_state(inode, data->owner);
1224 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001225 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001226 if (data->o_res.delegation_type != 0)
1227 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001228 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001229 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001230 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001231out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001232 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001233err_put_inode:
1234 iput(inode);
1235err:
1236 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001237}
1238
Andy Adamsone23008e2012-10-02 21:07:32 -04001239static struct nfs4_state *
1240nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1241{
1242 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1243 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1244 return _nfs4_opendata_to_nfs4_state(data);
1245}
1246
Trond Myklebust864472e2006-01-03 09:55:15 +01001247static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1248{
1249 struct nfs_inode *nfsi = NFS_I(state->inode);
1250 struct nfs_open_context *ctx;
1251
1252 spin_lock(&state->inode->i_lock);
1253 list_for_each_entry(ctx, &nfsi->open_files, list) {
1254 if (ctx->state != state)
1255 continue;
1256 get_nfs_open_context(ctx);
1257 spin_unlock(&state->inode->i_lock);
1258 return ctx;
1259 }
1260 spin_unlock(&state->inode->i_lock);
1261 return ERR_PTR(-ENOENT);
1262}
1263
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001264static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1265{
1266 struct nfs4_opendata *opendata;
1267
Al Viro3d4ff432011-06-22 18:40:12 -04001268 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001269 if (opendata == NULL)
1270 return ERR_PTR(-ENOMEM);
1271 opendata->state = state;
1272 atomic_inc(&state->count);
1273 return opendata;
1274}
1275
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001276static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001277{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001278 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 int ret;
1280
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001281 opendata->o_arg.open_flags = 0;
1282 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001283 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1284 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1285 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001286 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 if (ret != 0)
1288 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001289 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001290 if (IS_ERR(newstate))
1291 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001292 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001293 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001294 return 0;
1295}
1296
1297static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1298{
Trond Myklebust864472e2006-01-03 09:55:15 +01001299 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 int ret;
1301
1302 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001303 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 smp_rmb();
1305 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001306 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001307 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001308 if (ret != 0)
1309 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001310 if (newstate != state)
1311 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001312 }
1313 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001314 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001315 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001316 if (ret != 0)
1317 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001318 if (newstate != state)
1319 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001320 }
1321 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001322 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001323 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001324 if (ret != 0)
1325 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001326 if (newstate != state)
1327 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001328 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001329 /*
1330 * We may have performed cached opens for all three recoveries.
1331 * Check if we need to update the current stateid.
1332 */
1333 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001334 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001335 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001336 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001337 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001338 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001339 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001340 return 0;
1341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/*
1344 * OPEN_RECLAIM:
1345 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001347static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001349 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001350 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001351 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 int status;
1353
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001354 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1355 if (IS_ERR(opendata))
1356 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001357 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1358 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001359 rcu_read_lock();
1360 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001361 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001362 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001363 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001364 opendata->o_arg.u.delegation_type = delegation_type;
1365 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001366 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return status;
1368}
1369
Trond Myklebust539cd032007-06-05 11:46:42 -04001370static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 struct nfs_server *server = NFS_SERVER(state->inode);
1373 struct nfs4_exception exception = { };
1374 int err;
1375 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001376 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001377 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001378 break;
1379 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 } while (exception.retry);
1381 return err;
1382}
1383
Trond Myklebust864472e2006-01-03 09:55:15 +01001384static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1385{
1386 struct nfs_open_context *ctx;
1387 int ret;
1388
1389 ctx = nfs4_state_find_open_context(state);
1390 if (IS_ERR(ctx))
1391 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001392 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001393 put_nfs_open_context(ctx);
1394 return ret;
1395}
1396
Trond Myklebust13437e12007-07-06 15:10:43 -04001397static 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 -07001398{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001399 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001400 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001402 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1403 if (IS_ERR(opendata))
1404 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001405 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001406 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001407 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001408 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001409 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Trond Myklebust13437e12007-07-06 15:10:43 -04001412int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001415 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 int err;
1417 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001418 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 switch (err) {
1420 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001421 case -ENOENT:
1422 case -ESTALE:
1423 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001424 case -NFS4ERR_BADSESSION:
1425 case -NFS4ERR_BADSLOT:
1426 case -NFS4ERR_BAD_HIGH_SLOT:
1427 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1428 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001429 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001430 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 case -NFS4ERR_STALE_CLIENTID:
1432 case -NFS4ERR_STALE_STATEID:
1433 case -NFS4ERR_EXPIRED:
1434 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001435 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001436 goto out;
1437 case -ERESTARTSYS:
1438 /*
1439 * The show must go on: exit, but mark the
1440 * stateid as needing recovery.
1441 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001442 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001443 case -NFS4ERR_ADMIN_REVOKED:
1444 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001445 nfs_inode_find_state_and_recover(state->inode,
1446 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001447 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001448 case -EKEYEXPIRED:
1449 /*
1450 * User RPCSEC_GSS context has expired.
1451 * We cannot recover this stateid now, so
1452 * skip it and allow recovery thread to
1453 * proceed.
1454 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001455 case -ENOMEM:
1456 err = 0;
1457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 err = nfs4_handle_exception(server, err, &exception);
1460 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001461out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return err;
1463}
1464
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001465static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1466{
1467 struct nfs4_opendata *data = calldata;
1468
1469 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001470 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001471 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001472 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001473 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001474 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001475 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001476}
1477
1478static void nfs4_open_confirm_release(void *calldata)
1479{
1480 struct nfs4_opendata *data = calldata;
1481 struct nfs4_state *state = NULL;
1482
1483 /* If this request hasn't been cancelled, do nothing */
1484 if (data->cancelled == 0)
1485 goto out_free;
1486 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001487 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001488 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001489 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001490 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001491 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001492out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001493 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001494}
1495
1496static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001497 .rpc_call_done = nfs4_open_confirm_done,
1498 .rpc_release = nfs4_open_confirm_release,
1499};
1500
1501/*
1502 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1503 */
1504static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1505{
1506 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1507 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001508 struct rpc_message msg = {
1509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1510 .rpc_argp = &data->c_arg,
1511 .rpc_resp = &data->c_res,
1512 .rpc_cred = data->owner->so_cred,
1513 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001514 struct rpc_task_setup task_setup_data = {
1515 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001516 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001517 .callback_ops = &nfs4_open_confirm_ops,
1518 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001519 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001520 .flags = RPC_TASK_ASYNC,
1521 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 int status;
1523
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001524 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001525 data->rpc_done = 0;
1526 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001527 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001528 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001529 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001530 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001531 status = nfs4_wait_for_completion_rpc_task(task);
1532 if (status != 0) {
1533 data->cancelled = 1;
1534 smp_wmb();
1535 } else
1536 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001537 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return status;
1539}
1540
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001541static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543 struct nfs4_opendata *data = calldata;
1544 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001545
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001546 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1547 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548 /*
1549 * Check if we still need to send an OPEN call, or if we can use
1550 * a delegation instead.
1551 */
1552 if (data->state != NULL) {
1553 struct nfs_delegation *delegation;
1554
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001555 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001556 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001557 rcu_read_lock();
1558 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001559 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1560 can_open_delegated(delegation, data->o_arg.fmode))
1561 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001562 rcu_read_unlock();
1563 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001564 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001565 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001566 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001567 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001568 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001569 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1570 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001571 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001572 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001573 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001574 &data->o_res.seq_res,
1575 task) != 0)
1576 nfs_release_seqid(data->o_arg.seqid);
1577 else
1578 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001579 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001580unlock_no_action:
1581 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001582out_no_action:
1583 task->tk_action = NULL;
1584
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001587static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1588{
1589 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1590 nfs4_open_prepare(task, calldata);
1591}
1592
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001593static void nfs4_open_done(struct rpc_task *task, void *calldata)
1594{
1595 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001597 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001598
Trond Myklebust14516c32010-07-31 14:29:06 -04001599 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1600 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001601
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001602 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001603 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1604 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001605 case S_IFREG:
1606 break;
1607 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001609 break;
1610 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001611 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001612 break;
1613 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001614 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001615 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001616 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001617 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001618 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1619 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001620 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001621 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001623
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001624static void nfs4_open_release(void *calldata)
1625{
1626 struct nfs4_opendata *data = calldata;
1627 struct nfs4_state *state = NULL;
1628
1629 /* If this request hasn't been cancelled, do nothing */
1630 if (data->cancelled == 0)
1631 goto out_free;
1632 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001633 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001634 goto out_free;
1635 /* In case we need an open_confirm, no cleanup! */
1636 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1637 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001638 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001639 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001640 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001642 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001643}
1644
1645static const struct rpc_call_ops nfs4_open_ops = {
1646 .rpc_call_prepare = nfs4_open_prepare,
1647 .rpc_call_done = nfs4_open_done,
1648 .rpc_release = nfs4_open_release,
1649};
1650
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001651static const struct rpc_call_ops nfs4_recover_open_ops = {
1652 .rpc_call_prepare = nfs4_recover_open_prepare,
1653 .rpc_call_done = nfs4_open_done,
1654 .rpc_release = nfs4_open_release,
1655};
1656
1657static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001658{
1659 struct inode *dir = data->dir->d_inode;
1660 struct nfs_server *server = NFS_SERVER(dir);
1661 struct nfs_openargs *o_arg = &data->o_arg;
1662 struct nfs_openres *o_res = &data->o_res;
1663 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001664 struct rpc_message msg = {
1665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1666 .rpc_argp = o_arg,
1667 .rpc_resp = o_res,
1668 .rpc_cred = data->owner->so_cred,
1669 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001670 struct rpc_task_setup task_setup_data = {
1671 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001672 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001673 .callback_ops = &nfs4_open_ops,
1674 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001675 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001676 .flags = RPC_TASK_ASYNC,
1677 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678 int status;
1679
Trond Myklebust9d12b212012-01-17 22:04:25 -05001680 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001681 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001682 data->rpc_done = 0;
1683 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001684 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001685 if (isrecover)
1686 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001687 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001688 if (IS_ERR(task))
1689 return PTR_ERR(task);
1690 status = nfs4_wait_for_completion_rpc_task(task);
1691 if (status != 0) {
1692 data->cancelled = 1;
1693 smp_wmb();
1694 } else
1695 status = data->rpc_status;
1696 rpc_put_task(task);
1697
1698 return status;
1699}
1700
1701static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1702{
1703 struct inode *dir = data->dir->d_inode;
1704 struct nfs_openres *o_res = &data->o_res;
1705 int status;
1706
1707 status = nfs4_run_open_task(data, 1);
1708 if (status != 0 || !data->rpc_done)
1709 return status;
1710
Trond Myklebust6926afd2012-01-07 13:22:46 -05001711 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1712
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001713 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1714 status = _nfs4_proc_open_confirm(data);
1715 if (status != 0)
1716 return status;
1717 }
1718
1719 return status;
1720}
1721
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001722static int nfs4_opendata_access(struct rpc_cred *cred,
1723 struct nfs4_opendata *opendata,
1724 struct nfs4_state *state, fmode_t fmode)
1725{
1726 struct nfs_access_entry cache;
1727 u32 mask;
1728
1729 /* access call failed or for some reason the server doesn't
1730 * support any access modes -- defer access call until later */
1731 if (opendata->o_res.access_supported == 0)
1732 return 0;
1733
1734 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001735 /* don't check MAY_WRITE - a newly created file may not have
1736 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001737 if (fmode & FMODE_READ)
1738 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001739 if (fmode & FMODE_EXEC)
1740 mask |= MAY_EXEC;
1741
1742 cache.cred = cred;
1743 cache.jiffies = jiffies;
1744 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1745 nfs_access_add_cache(state->inode, &cache);
1746
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001747 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001748 return 0;
1749
1750 /* even though OPEN succeeded, access is denied. Close the file */
1751 nfs4_close_state(state, fmode);
1752 return -NFS4ERR_ACCESS;
1753}
1754
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001755/*
1756 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1757 */
1758static int _nfs4_proc_open(struct nfs4_opendata *data)
1759{
1760 struct inode *dir = data->dir->d_inode;
1761 struct nfs_server *server = NFS_SERVER(dir);
1762 struct nfs_openargs *o_arg = &data->o_arg;
1763 struct nfs_openres *o_res = &data->o_res;
1764 int status;
1765
1766 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001767 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001768 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001769 if (status != 0) {
1770 if (status == -NFS4ERR_BADNAME &&
1771 !(o_arg->open_flags & O_CREAT))
1772 return -ENOENT;
1773 return status;
1774 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001775
Trond Myklebust6926afd2012-01-07 13:22:46 -05001776 nfs_fattr_map_and_free_names(server, &data->f_attr);
1777
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001778 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001779 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001780 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1781 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001783 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001785 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001788 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Andy Adamsond83217c2011-03-01 01:34:17 +00001792static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001793{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001794 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001795 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001796
Trond Myklebusta78cb572009-08-09 15:06:19 -04001797 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001798 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001799 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001800 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001801 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1802 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001803 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001804 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001805 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001806 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001807 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001808}
1809
Andy Adamsond83217c2011-03-01 01:34:17 +00001810static int nfs4_recover_expired_lease(struct nfs_server *server)
1811{
1812 return nfs4_client_recover_expired_lease(server->nfs_client);
1813}
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815/*
1816 * OPEN_EXPIRED:
1817 * reclaim state on the server after a network partition.
1818 * Assumes caller holds the appropriate lock
1819 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001820static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001822 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001823 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001825 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1826 if (IS_ERR(opendata))
1827 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001828 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001829 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001830 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001831 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001832 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001835static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001836{
Trond Myklebust539cd032007-06-05 11:46:42 -04001837 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001838 struct nfs4_exception exception = { };
1839 int err;
1840
1841 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001842 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001843 switch (err) {
1844 default:
1845 goto out;
1846 case -NFS4ERR_GRACE:
1847 case -NFS4ERR_DELAY:
1848 nfs4_handle_exception(server, err, &exception);
1849 err = 0;
1850 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001851 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001852out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001853 return err;
1854}
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1857{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Trond Myklebust864472e2006-01-03 09:55:15 +01001861 ctx = nfs4_state_find_open_context(state);
1862 if (IS_ERR(ctx))
1863 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001864 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001865 put_nfs_open_context(ctx);
1866 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001869#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001870static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001871{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001872 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001873 nfs4_stateid *stateid = &state->stateid;
1874 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001875
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001876 /* If a state reset has been done, test_stateid is unneeded */
1877 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1878 return;
1879
1880 status = nfs41_test_stateid(server, stateid);
1881 if (status != NFS_OK) {
1882 /* Free the stateid unless the server explicitly
1883 * informs us the stateid is unrecognized. */
1884 if (status != -NFS4ERR_BAD_STATEID)
1885 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001886 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001887
Bryan Schumaker69388672012-09-26 15:25:52 -04001888 write_seqlock(&state->seqlock);
1889 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1890 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001891 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1892 }
1893}
1894
1895/**
1896 * nfs41_check_open_stateid - possibly free an open stateid
1897 *
1898 * @state: NFSv4 state for an inode
1899 *
1900 * Returns NFS_OK if recovery for this stateid is now finished.
1901 * Otherwise a negative NFS4ERR value is returned.
1902 */
1903static int nfs41_check_open_stateid(struct nfs4_state *state)
1904{
1905 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001906 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001907 int status;
1908
1909 /* If a state reset has been done, test_stateid is unneeded */
1910 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1911 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1912 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1913 return -NFS4ERR_BAD_STATEID;
1914
1915 status = nfs41_test_stateid(server, stateid);
1916 if (status != NFS_OK) {
1917 /* Free the stateid unless the server explicitly
1918 * informs us the stateid is unrecognized. */
1919 if (status != -NFS4ERR_BAD_STATEID)
1920 nfs41_free_stateid(server, stateid);
1921
1922 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1923 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1924 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001925 }
1926 return status;
1927}
1928
1929static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1930{
Chuck Levereb64cf92012-07-11 16:30:05 -04001931 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001932
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001933 nfs41_clear_delegation_stateid(state);
1934 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001935 if (status != NFS_OK)
1936 status = nfs4_open_expired(sp, state);
1937 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001938}
1939#endif
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001942 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1943 * fields corresponding to attributes that were used to store the verifier.
1944 * Make sure we clobber those fields in the later setattr call
1945 */
1946static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1947{
1948 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1949 !(sattr->ia_valid & ATTR_ATIME_SET))
1950 sattr->ia_valid |= ATTR_ATIME;
1951
1952 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1953 !(sattr->ia_valid & ATTR_MTIME_SET))
1954 sattr->ia_valid |= ATTR_MTIME;
1955}
1956
1957/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001958 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 */
Andy Adamson82be4172012-05-23 05:02:35 -04001960static int _nfs4_do_open(struct inode *dir,
1961 struct dentry *dentry,
1962 fmode_t fmode,
1963 int flags,
1964 struct iattr *sattr,
1965 struct rpc_cred *cred,
1966 struct nfs4_state **res,
1967 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 struct nfs4_state_owner *sp;
1970 struct nfs4_state *state = NULL;
1971 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001972 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001973 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001977 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1978 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1980 goto out_err;
1981 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001982 status = nfs4_recover_expired_lease(server);
1983 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001984 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001985 if (dentry->d_inode != NULL)
1986 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001987 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001988 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001989 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001990 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Andy Adamson82be4172012-05-23 05:02:35 -04001992 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1993 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1994 if (!opendata->f_attr.mdsthreshold)
1995 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001996 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001997 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001998 if (dentry->d_inode != NULL)
1999 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002000
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002001 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002003 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002005 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002006 status = PTR_ERR(state);
2007 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002008 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002009 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002010 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002011
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002012 status = nfs4_opendata_access(cred, opendata, state, fmode);
2013 if (status != 0)
2014 goto err_opendata_put;
2015
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002016 if (opendata->o_arg.open_flags & O_EXCL) {
2017 nfs4_exclusive_attrset(opendata, sattr);
2018
2019 nfs_fattr_init(opendata->o_res.f_attr);
2020 status = nfs4_do_setattr(state->inode, cred,
2021 opendata->o_res.f_attr, sattr,
2022 state);
2023 if (status == 0)
2024 nfs_setattr_update_inode(state->inode, sattr);
2025 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2026 }
Andy Adamson82be4172012-05-23 05:02:35 -04002027
2028 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2029 *ctx_th = opendata->f_attr.mdsthreshold;
2030 else
2031 kfree(opendata->f_attr.mdsthreshold);
2032 opendata->f_attr.mdsthreshold = NULL;
2033
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002034 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 *res = state;
2037 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002038err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002039 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002040 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002041err_put_state_owner:
2042 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 *res = NULL;
2045 return status;
2046}
2047
2048
Andy Adamson82be4172012-05-23 05:02:35 -04002049static struct nfs4_state *nfs4_do_open(struct inode *dir,
2050 struct dentry *dentry,
2051 fmode_t fmode,
2052 int flags,
2053 struct iattr *sattr,
2054 struct rpc_cred *cred,
2055 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 struct nfs4_exception exception = { };
2058 struct nfs4_state *res;
2059 int status;
2060
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002061 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002063 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2064 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (status == 0)
2066 break;
2067 /* NOTE: BAD_SEQID means the server and client disagree about the
2068 * book-keeping w.r.t. state-changing operations
2069 * (OPEN/CLOSE/LOCK/LOCKU...)
2070 * It is actually a sign of a bug on the client or on the server.
2071 *
2072 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002073 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 * have unhashed the old state_owner for us, and that we can
2075 * therefore safely retry using a new one. We should still warn
2076 * the user though...
2077 */
2078 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002079 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002080 " returned a bad sequence-id error!\n",
2081 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 exception.retry = 1;
2083 continue;
2084 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002085 /*
2086 * BAD_STATEID on OPEN means that the server cancelled our
2087 * state before it received the OPEN_CONFIRM.
2088 * Recover by retrying the request as per the discussion
2089 * on Page 181 of RFC3530.
2090 */
2091 if (status == -NFS4ERR_BAD_STATEID) {
2092 exception.retry = 1;
2093 continue;
2094 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002095 if (status == -EAGAIN) {
2096 /* We must have found a delegation */
2097 exception.retry = 1;
2098 continue;
2099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2101 status, &exception));
2102 } while (exception.retry);
2103 return res;
2104}
2105
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002106static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2107 struct nfs_fattr *fattr, struct iattr *sattr,
2108 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002110 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002112 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 .iap = sattr,
2114 .server = server,
2115 .bitmask = server->attr_bitmask,
2116 };
2117 struct nfs_setattrres res = {
2118 .fattr = fattr,
2119 .server = server,
2120 };
2121 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2123 .rpc_argp = &arg,
2124 .rpc_resp = &res,
2125 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002127 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002128 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Trond Myklebust0e574af2005-10-27 22:12:38 -04002130 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Trond Myklebust4fc87962012-03-08 17:42:01 -05002132 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002133 struct nfs_lockowner lockowner = {
2134 .l_owner = current->files,
2135 .l_pid = current->tgid,
2136 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002137 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002138 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002139 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2140 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002141 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002142 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002143 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Bryan Schumaker7c513052011-03-24 17:12:24 +00002145 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002146 if (status == 0 && state != NULL)
2147 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002148 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002151static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2152 struct nfs_fattr *fattr, struct iattr *sattr,
2153 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002155 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002156 struct nfs4_exception exception = {
2157 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002158 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002159 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 int err;
2161 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002162 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2163 switch (err) {
2164 case -NFS4ERR_OPENMODE:
2165 if (state && !(state->state & FMODE_WRITE)) {
2166 err = -EBADF;
2167 if (sattr->ia_valid & ATTR_OPEN)
2168 err = -EACCES;
2169 goto out;
2170 }
2171 }
2172 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002174out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return err;
2176}
2177
2178struct nfs4_closedata {
2179 struct inode *inode;
2180 struct nfs4_state *state;
2181 struct nfs_closeargs arg;
2182 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002183 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002184 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002185 bool roc;
2186 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187};
2188
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002189static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002190{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002191 struct nfs4_closedata *calldata = data;
2192 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002193 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002194
Fred Isamanf7e89172011-01-06 11:36:32 +00002195 if (calldata->roc)
2196 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002197 nfs4_put_open_state(calldata->state);
2198 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002199 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002200 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002201 kfree(calldata);
2202}
2203
Trond Myklebust88069f72009-12-08 08:33:16 -05002204static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2205 fmode_t fmode)
2206{
2207 spin_lock(&state->owner->so_lock);
2208 if (!(fmode & FMODE_READ))
2209 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2210 if (!(fmode & FMODE_WRITE))
2211 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2212 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2213 spin_unlock(&state->owner->so_lock);
2214}
2215
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002216static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002218 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 struct nfs_server *server = NFS_SERVER(calldata->inode);
2221
Chuck Levera3ca5652012-03-01 17:00:40 -05002222 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002223 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2224 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 /* hmm. we are done with the inode, and in the process of freeing
2226 * the state_owner. we keep this around to process errors
2227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 switch (task->tk_status) {
2229 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002230 if (calldata->roc)
2231 pnfs_roc_set_barrier(state->inode,
2232 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002233 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002234 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002235 nfs4_close_clear_stateid_flags(state,
2236 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 break;
2238 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002239 case -NFS4ERR_OLD_STATEID:
2240 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002242 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002245 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2246 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002248 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002249 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002250 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002253static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002255 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002256 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002257 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002258 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002259
Chuck Levera3ca5652012-03-01 17:00:40 -05002260 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002261 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002262 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002263
Trond Myklebust88069f72009-12-08 08:33:16 -05002264 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2265 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002266 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002267 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002268 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002269 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002270 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2271 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2272 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002273 }
2274 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002275 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2276 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2277 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002278 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002279 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002280 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002281
2282 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002283 /* Note: exit _without_ calling nfs4_close_done */
2284 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002285 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002286 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002287
Fred Isamanf7e89172011-01-06 11:36:32 +00002288 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002289 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002290 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002291 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002292 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002293 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002294
Trond Myklebust516a6af2005-10-27 22:12:41 -04002295 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002296 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002297 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002298 &calldata->arg.seq_args,
2299 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002300 task) != 0)
2301 nfs_release_seqid(calldata->arg.seqid);
2302 else
2303 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002304out:
2305 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002308static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002309 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002310 .rpc_call_done = nfs4_close_done,
2311 .rpc_release = nfs4_free_closedata,
2312};
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314/*
2315 * It is possible for data to be read/written from a mem-mapped file
2316 * after the sys_close call (which hits the vfs layer as a flush).
2317 * This means that we can't safely call nfsv4 close on a file until
2318 * the inode is cleared. This in turn means that we are not good
2319 * NFSv4 citizens - we do not indicate to the server to update the file's
2320 * share state even when we are done with one of the three share
2321 * stateid's in the inode.
2322 *
2323 * NOTE: Caller must be holding the sp->so_owner semaphore!
2324 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002325int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002327 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002329 struct nfs4_state_owner *sp = state->owner;
2330 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002331 struct rpc_message msg = {
2332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2333 .rpc_cred = state->owner->so_cred,
2334 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002335 struct rpc_task_setup task_setup_data = {
2336 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002337 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002338 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002339 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002340 .flags = RPC_TASK_ASYNC,
2341 };
Trond Myklebust95121352005-10-18 14:20:12 -07002342 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002344 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002346 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002347 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002348 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002350 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002351 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002353 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002354 if (calldata->arg.seqid == NULL)
2355 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002356 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002357 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002358 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002359 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002360 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002361 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002362 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002363
Trond Myklebust1174dd12010-12-21 10:52:24 -05002364 msg.rpc_argp = &calldata->arg;
2365 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002366 task_setup_data.callback_data = calldata;
2367 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002368 if (IS_ERR(task))
2369 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002370 status = 0;
2371 if (wait)
2372 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002373 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002374 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002375out_free_calldata:
2376 kfree(calldata);
2377out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002378 nfs4_put_open_state(state);
2379 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002380 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
Trond Myklebust2b484292010-09-17 10:56:51 -04002383static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002384nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 struct nfs4_state *state;
2387
Trond Myklebust565277f2007-10-15 18:17:53 -04002388 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002389 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2390 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002391 if (IS_ERR(state))
2392 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002393 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002394 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
Trond Myklebust1185a552009-12-03 15:54:02 -05002397static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002398{
2399 if (ctx->state == NULL)
2400 return;
2401 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002402 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002403 else
Al Viro643168c2011-06-22 18:20:23 -04002404 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002405}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2408{
Benny Halevy43652ad2009-04-01 09:21:54 -04002409 struct nfs4_server_caps_arg args = {
2410 .fhandle = fhandle,
2411 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 struct nfs4_server_caps_res res = {};
2413 struct rpc_message msg = {
2414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002415 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 .rpc_resp = &res,
2417 };
2418 int status;
2419
Bryan Schumaker7c513052011-03-24 17:12:24 +00002420 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (status == 0) {
2422 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002423 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2424 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2425 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2426 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2427 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2429 server->caps |= NFS_CAP_ACLS;
2430 if (res.has_links != 0)
2431 server->caps |= NFS_CAP_HARDLINKS;
2432 if (res.has_symlinks != 0)
2433 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002434 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2435 server->caps |= NFS_CAP_FILEID;
2436 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2437 server->caps |= NFS_CAP_MODE;
2438 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2439 server->caps |= NFS_CAP_NLINK;
2440 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2441 server->caps |= NFS_CAP_OWNER;
2442 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2443 server->caps |= NFS_CAP_OWNER_GROUP;
2444 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2445 server->caps |= NFS_CAP_ATIME;
2446 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2447 server->caps |= NFS_CAP_CTIME;
2448 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2449 server->caps |= NFS_CAP_MTIME;
2450
Trond Myklebusta65318b2009-03-11 14:10:28 -04002451 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2452 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2453 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002455 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return status;
2459}
2460
Trond Myklebust55a97592006-06-09 09:34:19 -04002461int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 struct nfs4_exception exception = { };
2464 int err;
2465 do {
2466 err = nfs4_handle_exception(server,
2467 _nfs4_server_capabilities(server, fhandle),
2468 &exception);
2469 } while (exception.retry);
2470 return err;
2471}
2472
2473static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2474 struct nfs_fsinfo *info)
2475{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 struct nfs4_lookup_root_arg args = {
2477 .bitmask = nfs4_fattr_bitmap,
2478 };
2479 struct nfs4_lookup_res res = {
2480 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002481 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 .fh = fhandle,
2483 };
2484 struct rpc_message msg = {
2485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2486 .rpc_argp = &args,
2487 .rpc_resp = &res,
2488 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002489
Trond Myklebust0e574af2005-10-27 22:12:38 -04002490 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002491 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
2494static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2495 struct nfs_fsinfo *info)
2496{
2497 struct nfs4_exception exception = { };
2498 int err;
2499 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002500 err = _nfs4_lookup_root(server, fhandle, info);
2501 switch (err) {
2502 case 0:
2503 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002504 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002505 default:
2506 err = nfs4_handle_exception(server, err, &exception);
2507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002509out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return err;
2511}
2512
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002513static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2514 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2515{
2516 struct rpc_auth *auth;
2517 int ret;
2518
2519 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002520 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002521 ret = -EIO;
2522 goto out;
2523 }
2524 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002525out:
2526 return ret;
2527}
2528
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002529static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002530 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002532 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002533 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002535 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2536 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002537
2538 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002539 /* AUTH_UNIX is the default flavor if none was specified,
2540 * thus has already been tried. */
2541 if (flav_array[i] == RPC_AUTH_UNIX)
2542 continue;
2543
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002544 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002545 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002546 continue;
2547 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002548 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002549 /*
2550 * -EACCESS could mean that the user doesn't have correct permissions
2551 * to access the mount. It could also mean that we tried to mount
2552 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2553 * existing mount programs don't handle -EACCES very well so it should
2554 * be mapped to -EPERM instead.
2555 */
2556 if (status == -EACCES)
2557 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002558 return status;
2559}
2560
2561/*
2562 * get the file handle for the "/" directory on the server
2563 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002564int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2565 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002566{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002567 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002568 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002569 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2570 /*
2571 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2572 * by nfs4_map_errors() as this function exits.
2573 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002574 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 if (status == 0)
2576 status = nfs4_server_capabilities(server, fhandle);
2577 if (status == 0)
2578 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002579 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Bryan Schumakerbae36242012-05-10 15:07:31 -04002582static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2583 struct nfs_fsinfo *info)
2584{
2585 int error;
2586 struct nfs_fattr *fattr = info->fattr;
2587
2588 error = nfs4_server_capabilities(server, mntfh);
2589 if (error < 0) {
2590 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2591 return error;
2592 }
2593
2594 error = nfs4_proc_getattr(server, mntfh, fattr);
2595 if (error < 0) {
2596 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2597 return error;
2598 }
2599
2600 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2601 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2602 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2603
2604 return error;
2605}
2606
Manoj Naik6b97fd32006-06-09 09:34:29 -04002607/*
2608 * Get locations and (maybe) other attributes of a referral.
2609 * Note that we'll actually follow the referral later when
2610 * we detect fsid mismatch in inode revalidation
2611 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002612static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2613 const struct qstr *name, struct nfs_fattr *fattr,
2614 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002615{
2616 int status = -ENOMEM;
2617 struct page *page = NULL;
2618 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002619
2620 page = alloc_page(GFP_KERNEL);
2621 if (page == NULL)
2622 goto out;
2623 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2624 if (locations == NULL)
2625 goto out;
2626
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002627 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002628 if (status != 0)
2629 goto out;
2630 /* Make sure server returned a different fsid for the referral */
2631 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002632 dprintk("%s: server did not return a different fsid for"
2633 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002634 status = -EIO;
2635 goto out;
2636 }
Andy Adamson533eb462011-06-13 18:25:56 -04002637 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2638 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002639
Andy Adamson533eb462011-06-13 18:25:56 -04002640 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002641 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002642 memset(fhandle, 0, sizeof(struct nfs_fh));
2643out:
2644 if (page)
2645 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002646 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002647 return status;
2648}
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2651{
2652 struct nfs4_getattr_arg args = {
2653 .fh = fhandle,
2654 .bitmask = server->attr_bitmask,
2655 };
2656 struct nfs4_getattr_res res = {
2657 .fattr = fattr,
2658 .server = server,
2659 };
2660 struct rpc_message msg = {
2661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2662 .rpc_argp = &args,
2663 .rpc_resp = &res,
2664 };
2665
Trond Myklebust0e574af2005-10-27 22:12:38 -04002666 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002667 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
2669
2670static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2671{
2672 struct nfs4_exception exception = { };
2673 int err;
2674 do {
2675 err = nfs4_handle_exception(server,
2676 _nfs4_proc_getattr(server, fhandle, fattr),
2677 &exception);
2678 } while (exception.retry);
2679 return err;
2680}
2681
2682/*
2683 * The file is not closed if it is opened due to the a request to change
2684 * the size of the file. The open call will not be needed once the
2685 * VFS layer lookup-intents are implemented.
2686 *
2687 * Close is called when the inode is destroyed.
2688 * If we haven't opened the file for O_WRONLY, we
2689 * need to in the size_change case to obtain a stateid.
2690 *
2691 * Got race?
2692 * Because OPEN is always done by name in nfsv4, it is
2693 * possible that we opened a different file by the same
2694 * name. We can recognize this race condition, but we
2695 * can't do anything about it besides returning an error.
2696 *
2697 * This will be fixed with VFS changes (lookup-intent).
2698 */
2699static int
2700nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2701 struct iattr *sattr)
2702{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002703 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002704 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002705 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 int status;
2707
Benny Halevy8a1636c2010-07-14 15:43:57 -04002708 if (pnfs_ld_layoutret_on_setattr(inode))
2709 pnfs_return_layout(inode);
2710
Trond Myklebust0e574af2005-10-27 22:12:38 -04002711 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Andy Adamson26699402012-05-30 16:12:24 -04002713 /* Deal with open(O_TRUNC) */
2714 if (sattr->ia_valid & ATTR_OPEN)
2715 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2716
2717 /* Optimization: if the end result is no change, don't RPC */
2718 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2719 return 0;
2720
Trond Myklebustd5308382005-11-04 15:33:38 -05002721 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002722 if (sattr->ia_valid & ATTR_FILE) {
2723 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002724
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002725 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002726 if (ctx) {
2727 cred = ctx->cred;
2728 state = ctx->state;
2729 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002730 }
2731
2732 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002733 if (status == 0)
2734 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return status;
2736}
2737
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002738static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2739 const struct qstr *name, struct nfs_fh *fhandle,
2740 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002741{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002742 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002743 int status;
2744 struct nfs4_lookup_arg args = {
2745 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002746 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002747 .name = name,
2748 };
2749 struct nfs4_lookup_res res = {
2750 .server = server,
2751 .fattr = fattr,
2752 .fh = fhandle,
2753 };
2754 struct rpc_message msg = {
2755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2756 .rpc_argp = &args,
2757 .rpc_resp = &res,
2758 };
2759
2760 nfs_fattr_init(fattr);
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002763 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 dprintk("NFS reply lookup: %d\n", status);
2765 return status;
2766}
2767
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002768static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002769{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002770 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002771 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002772 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2773 fattr->nlink = 2;
2774}
2775
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002776static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2777 struct qstr *name, struct nfs_fh *fhandle,
2778 struct nfs_fattr *fattr)
2779{
2780 struct nfs4_exception exception = { };
2781 struct rpc_clnt *client = *clnt;
2782 int err;
2783 do {
2784 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2785 switch (err) {
2786 case -NFS4ERR_BADNAME:
2787 err = -ENOENT;
2788 goto out;
2789 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002790 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002791 goto out;
2792 case -NFS4ERR_WRONGSEC:
2793 err = -EPERM;
2794 if (client != *clnt)
2795 goto out;
2796
2797 client = nfs4_create_sec_client(client, dir, name);
2798 if (IS_ERR(client))
2799 return PTR_ERR(client);
2800
2801 exception.retry = 1;
2802 break;
2803 default:
2804 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2805 }
2806 } while (exception.retry);
2807
2808out:
2809 if (err == 0)
2810 *clnt = client;
2811 else if (client != *clnt)
2812 rpc_shutdown_client(client);
2813
2814 return err;
2815}
2816
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002817static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002818 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002820 int status;
2821 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002822
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002823 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2824 if (client != NFS_CLIENT(dir)) {
2825 rpc_shutdown_client(client);
2826 nfs_fixup_secinfo_attributes(fattr);
2827 }
2828 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002831struct rpc_clnt *
2832nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2833 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2834{
2835 int status;
2836 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2837
2838 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2839 if (status < 0) {
2840 rpc_shutdown_client(client);
2841 return ERR_PTR(status);
2842 }
2843 return client;
2844}
2845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2847{
Trond Myklebust76b32992007-08-10 17:45:11 -04002848 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 struct nfs4_accessargs args = {
2850 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002851 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002853 struct nfs4_accessres res = {
2854 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002855 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 struct rpc_message msg = {
2857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2858 .rpc_argp = &args,
2859 .rpc_resp = &res,
2860 .rpc_cred = entry->cred,
2861 };
2862 int mode = entry->mask;
2863 int status;
2864
2865 /*
2866 * Determine which access bits we want to ask for...
2867 */
2868 if (mode & MAY_READ)
2869 args.access |= NFS4_ACCESS_READ;
2870 if (S_ISDIR(inode->i_mode)) {
2871 if (mode & MAY_WRITE)
2872 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2873 if (mode & MAY_EXEC)
2874 args.access |= NFS4_ACCESS_LOOKUP;
2875 } else {
2876 if (mode & MAY_WRITE)
2877 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2878 if (mode & MAY_EXEC)
2879 args.access |= NFS4_ACCESS_EXECUTE;
2880 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002881
2882 res.fattr = nfs_alloc_fattr();
2883 if (res.fattr == NULL)
2884 return -ENOMEM;
2885
Bryan Schumaker7c513052011-03-24 17:12:24 +00002886 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002888 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002889 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002891 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return status;
2893}
2894
2895static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2896{
2897 struct nfs4_exception exception = { };
2898 int err;
2899 do {
2900 err = nfs4_handle_exception(NFS_SERVER(inode),
2901 _nfs4_proc_access(inode, entry),
2902 &exception);
2903 } while (exception.retry);
2904 return err;
2905}
2906
2907/*
2908 * TODO: For the time being, we don't try to get any attributes
2909 * along with any of the zero-copy operations READ, READDIR,
2910 * READLINK, WRITE.
2911 *
2912 * In the case of the first three, we want to put the GETATTR
2913 * after the read-type operation -- this is because it is hard
2914 * to predict the length of a GETATTR response in v4, and thus
2915 * align the READ data correctly. This means that the GETATTR
2916 * may end up partially falling into the page cache, and we should
2917 * shift it into the 'tail' of the xdr_buf before processing.
2918 * To do this efficiently, we need to know the total length
2919 * of data received, which doesn't seem to be available outside
2920 * of the RPC layer.
2921 *
2922 * In the case of WRITE, we also want to put the GETATTR after
2923 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002924 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 *
2926 * Both of these changes to the XDR layer would in fact be quite
2927 * minor, but I decided to leave them for a subsequent patch.
2928 */
2929static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2930 unsigned int pgbase, unsigned int pglen)
2931{
2932 struct nfs4_readlink args = {
2933 .fh = NFS_FH(inode),
2934 .pgbase = pgbase,
2935 .pglen = pglen,
2936 .pages = &page,
2937 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002938 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 struct rpc_message msg = {
2940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2941 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002942 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 };
2944
Bryan Schumaker7c513052011-03-24 17:12:24 +00002945 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 -07002946}
2947
2948static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2949 unsigned int pgbase, unsigned int pglen)
2950{
2951 struct nfs4_exception exception = { };
2952 int err;
2953 do {
2954 err = nfs4_handle_exception(NFS_SERVER(inode),
2955 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2956 &exception);
2957 } while (exception.retry);
2958 return err;
2959}
2960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002962 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964static int
2965nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002966 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002968 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 int status = 0;
2971
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002972 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2973 if (IS_ERR(ctx))
2974 return PTR_ERR(ctx);
2975
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002976 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002977 state = nfs4_do_open(dir, dentry, ctx->mode,
2978 flags, sattr, ctx->cred,
2979 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002980 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (IS_ERR(state)) {
2982 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002983 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002985 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002986 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002987 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002989 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 return status;
2991}
2992
2993static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2994{
Trond Myklebust16e42952005-10-27 22:12:44 -04002995 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002996 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002998 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003000 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003001 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003002 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3005 .rpc_argp = &args,
3006 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003008 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003009
Bryan Schumaker7c513052011-03-24 17:12:24 +00003010 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003011 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003012 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return status;
3014}
3015
3016static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3017{
3018 struct nfs4_exception exception = { };
3019 int err;
3020 do {
3021 err = nfs4_handle_exception(NFS_SERVER(dir),
3022 _nfs4_proc_remove(dir, name),
3023 &exception);
3024 } while (exception.retry);
3025 return err;
3026}
3027
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003028static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003030 struct nfs_server *server = NFS_SERVER(dir);
3031 struct nfs_removeargs *args = msg->rpc_argp;
3032 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003034 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003036 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003039static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3040{
3041 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3042 &data->args.seq_args,
3043 &data->res.seq_res,
3044 task))
3045 return;
3046 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003049static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003051 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3052
Trond Myklebust14516c32010-07-31 14:29:06 -04003053 if (!nfs4_sequence_done(task, &res->seq_res))
3054 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003055 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003056 return 0;
3057 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003058 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
Jeff Laytond3d41522010-09-17 17:31:57 -04003061static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3062{
3063 struct nfs_server *server = NFS_SERVER(dir);
3064 struct nfs_renameargs *arg = msg->rpc_argp;
3065 struct nfs_renameres *res = msg->rpc_resp;
3066
3067 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003068 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003069 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003070}
3071
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003072static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3073{
3074 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3075 &data->args.seq_args,
3076 &data->res.seq_res,
3077 task))
3078 return;
3079 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003080}
3081
3082static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3083 struct inode *new_dir)
3084{
3085 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3086
3087 if (!nfs4_sequence_done(task, &res->seq_res))
3088 return 0;
3089 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3090 return 0;
3091
3092 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003093 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003094 return 1;
3095}
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3098 struct inode *new_dir, struct qstr *new_name)
3099{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003100 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003101 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 .old_dir = NFS_FH(old_dir),
3103 .new_dir = NFS_FH(new_dir),
3104 .old_name = old_name,
3105 .new_name = new_name,
3106 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003107 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003108 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003109 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 struct rpc_message msg = {
3111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3112 .rpc_argp = &arg,
3113 .rpc_resp = &res,
3114 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003115 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Bryan Schumaker7c513052011-03-24 17:12:24 +00003117 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (!status) {
3119 update_changeattr(old_dir, &res.old_cinfo);
3120 update_changeattr(new_dir, &res.new_cinfo);
3121 }
3122 return status;
3123}
3124
3125static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3126 struct inode *new_dir, struct qstr *new_name)
3127{
3128 struct nfs4_exception exception = { };
3129 int err;
3130 do {
3131 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3132 _nfs4_proc_rename(old_dir, old_name,
3133 new_dir, new_name),
3134 &exception);
3135 } while (exception.retry);
3136 return err;
3137}
3138
3139static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3140{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003141 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 struct nfs4_link_arg arg = {
3143 .fh = NFS_FH(inode),
3144 .dir_fh = NFS_FH(dir),
3145 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003146 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003148 struct nfs4_link_res res = {
3149 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003150 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 struct rpc_message msg = {
3152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3153 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003154 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003156 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Trond Myklebust136f2622010-04-16 16:22:49 -04003158 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003159 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003160 goto out;
3161
Bryan Schumaker7c513052011-03-24 17:12:24 +00003162 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003163 if (!status) {
3164 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003165 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003166 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003167out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003168 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return status;
3170}
3171
3172static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3173{
3174 struct nfs4_exception exception = { };
3175 int err;
3176 do {
3177 err = nfs4_handle_exception(NFS_SERVER(inode),
3178 _nfs4_proc_link(inode, dir, name),
3179 &exception);
3180 } while (exception.retry);
3181 return err;
3182}
3183
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003184struct nfs4_createdata {
3185 struct rpc_message msg;
3186 struct nfs4_create_arg arg;
3187 struct nfs4_create_res res;
3188 struct nfs_fh fh;
3189 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003190};
3191
3192static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3193 struct qstr *name, struct iattr *sattr, u32 ftype)
3194{
3195 struct nfs4_createdata *data;
3196
3197 data = kzalloc(sizeof(*data), GFP_KERNEL);
3198 if (data != NULL) {
3199 struct nfs_server *server = NFS_SERVER(dir);
3200
3201 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3202 data->msg.rpc_argp = &data->arg;
3203 data->msg.rpc_resp = &data->res;
3204 data->arg.dir_fh = NFS_FH(dir);
3205 data->arg.server = server;
3206 data->arg.name = name;
3207 data->arg.attrs = sattr;
3208 data->arg.ftype = ftype;
3209 data->arg.bitmask = server->attr_bitmask;
3210 data->res.server = server;
3211 data->res.fh = &data->fh;
3212 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003213 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003214 }
3215 return data;
3216}
3217
3218static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3219{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003220 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003221 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003222 if (status == 0) {
3223 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003224 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3225 }
3226 return status;
3227}
3228
3229static void nfs4_free_createdata(struct nfs4_createdata *data)
3230{
3231 kfree(data);
3232}
3233
Chuck Lever4f390c12006-08-22 20:06:22 -04003234static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003235 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003237 struct nfs4_createdata *data;
3238 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Chuck Lever94a6d752006-08-22 20:06:23 -04003240 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003241 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003242
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003243 status = -ENOMEM;
3244 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3245 if (data == NULL)
3246 goto out;
3247
3248 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3249 data->arg.u.symlink.pages = &page;
3250 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003252 status = nfs4_do_create(dir, dentry, data);
3253
3254 nfs4_free_createdata(data);
3255out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return status;
3257}
3258
Chuck Lever4f390c12006-08-22 20:06:22 -04003259static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003260 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
3262 struct nfs4_exception exception = { };
3263 int err;
3264 do {
3265 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003266 _nfs4_proc_symlink(dir, dentry, page,
3267 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 &exception);
3269 } while (exception.retry);
3270 return err;
3271}
3272
3273static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3274 struct iattr *sattr)
3275{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003276 struct nfs4_createdata *data;
3277 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003279 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3280 if (data == NULL)
3281 goto out;
3282
3283 status = nfs4_do_create(dir, dentry, data);
3284
3285 nfs4_free_createdata(data);
3286out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 return status;
3288}
3289
3290static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3291 struct iattr *sattr)
3292{
3293 struct nfs4_exception exception = { };
3294 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003295
3296 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 do {
3298 err = nfs4_handle_exception(NFS_SERVER(dir),
3299 _nfs4_proc_mkdir(dir, dentry, sattr),
3300 &exception);
3301 } while (exception.retry);
3302 return err;
3303}
3304
3305static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003306 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
3308 struct inode *dir = dentry->d_inode;
3309 struct nfs4_readdir_arg args = {
3310 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003311 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 .pgbase = 0,
3313 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003314 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003315 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 };
3317 struct nfs4_readdir_res res;
3318 struct rpc_message msg = {
3319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3320 .rpc_argp = &args,
3321 .rpc_resp = &res,
3322 .rpc_cred = cred,
3323 };
3324 int status;
3325
Harvey Harrison3110ff82008-05-02 13:42:44 -07003326 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003327 dentry->d_parent->d_name.name,
3328 dentry->d_name.name,
3329 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003330 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003332 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 -05003333 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003334 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003335 status += args.pgbase;
3336 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003337
3338 nfs_invalidate_atime(dir);
3339
Harvey Harrison3110ff82008-05-02 13:42:44 -07003340 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 return status;
3342}
3343
3344static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003345 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
3347 struct nfs4_exception exception = { };
3348 int err;
3349 do {
3350 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3351 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003352 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 &exception);
3354 } while (exception.retry);
3355 return err;
3356}
3357
3358static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3359 struct iattr *sattr, dev_t rdev)
3360{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003361 struct nfs4_createdata *data;
3362 int mode = sattr->ia_mode;
3363 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
3365 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3366 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003367
3368 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3369 if (data == NULL)
3370 goto out;
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003373 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003375 data->arg.ftype = NF4BLK;
3376 data->arg.u.device.specdata1 = MAJOR(rdev);
3377 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 }
3379 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003380 data->arg.ftype = NF4CHR;
3381 data->arg.u.device.specdata1 = MAJOR(rdev);
3382 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003385 status = nfs4_do_create(dir, dentry, data);
3386
3387 nfs4_free_createdata(data);
3388out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 return status;
3390}
3391
3392static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3393 struct iattr *sattr, dev_t rdev)
3394{
3395 struct nfs4_exception exception = { };
3396 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003397
3398 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 do {
3400 err = nfs4_handle_exception(NFS_SERVER(dir),
3401 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3402 &exception);
3403 } while (exception.retry);
3404 return err;
3405}
3406
3407static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3408 struct nfs_fsstat *fsstat)
3409{
3410 struct nfs4_statfs_arg args = {
3411 .fh = fhandle,
3412 .bitmask = server->attr_bitmask,
3413 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003414 struct nfs4_statfs_res res = {
3415 .fsstat = fsstat,
3416 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 struct rpc_message msg = {
3418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3419 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003420 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 };
3422
Trond Myklebust0e574af2005-10-27 22:12:38 -04003423 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003424 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425}
3426
3427static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3428{
3429 struct nfs4_exception exception = { };
3430 int err;
3431 do {
3432 err = nfs4_handle_exception(server,
3433 _nfs4_proc_statfs(server, fhandle, fsstat),
3434 &exception);
3435 } while (exception.retry);
3436 return err;
3437}
3438
3439static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3440 struct nfs_fsinfo *fsinfo)
3441{
3442 struct nfs4_fsinfo_arg args = {
3443 .fh = fhandle,
3444 .bitmask = server->attr_bitmask,
3445 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003446 struct nfs4_fsinfo_res res = {
3447 .fsinfo = fsinfo,
3448 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 struct rpc_message msg = {
3450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3451 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003452 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 };
3454
Bryan Schumaker7c513052011-03-24 17:12:24 +00003455 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456}
3457
3458static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3459{
3460 struct nfs4_exception exception = { };
3461 int err;
3462
3463 do {
3464 err = nfs4_handle_exception(server,
3465 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3466 &exception);
3467 } while (exception.retry);
3468 return err;
3469}
3470
3471static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3472{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003473 int error;
3474
Trond Myklebust0e574af2005-10-27 22:12:38 -04003475 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003476 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003477 if (error == 0) {
3478 /* block layout checks this! */
3479 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003480 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003481 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003482
3483 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
3486static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3487 struct nfs_pathconf *pathconf)
3488{
3489 struct nfs4_pathconf_arg args = {
3490 .fh = fhandle,
3491 .bitmask = server->attr_bitmask,
3492 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003493 struct nfs4_pathconf_res res = {
3494 .pathconf = pathconf,
3495 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 struct rpc_message msg = {
3497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3498 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003499 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 };
3501
3502 /* None of the pathconf attributes are mandatory to implement */
3503 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3504 memset(pathconf, 0, sizeof(*pathconf));
3505 return 0;
3506 }
3507
Trond Myklebust0e574af2005-10-27 22:12:38 -04003508 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003509 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
3512static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3513 struct nfs_pathconf *pathconf)
3514{
3515 struct nfs4_exception exception = { };
3516 int err;
3517
3518 do {
3519 err = nfs4_handle_exception(server,
3520 _nfs4_proc_pathconf(server, fhandle, pathconf),
3521 &exception);
3522 } while (exception.retry);
3523 return err;
3524}
3525
Benny Halevyd20581a2011-05-22 19:52:03 +03003526void __nfs4_read_done_cb(struct nfs_read_data *data)
3527{
Fred Isamancd841602012-04-20 14:47:44 -04003528 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003529}
3530
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003531static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
Fred Isamancd841602012-04-20 14:47:44 -04003533 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003535 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003536 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003537 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003539
Benny Halevyd20581a2011-05-22 19:52:03 +03003540 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003542 renew_lease(server, data->timestamp);
3543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003546static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3547{
3548
3549 dprintk("--> %s\n", __func__);
3550
3551 if (!nfs4_sequence_done(task, &data->res.seq_res))
3552 return -EAGAIN;
3553
Benny Halevyd20581a2011-05-22 19:52:03 +03003554 return data->read_done_cb ? data->read_done_cb(task, data) :
3555 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003556}
3557
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003558static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003561 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003562 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003563 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003566static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3567{
Fred Isamancd841602012-04-20 14:47:44 -04003568 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003569 &data->args.seq_args,
3570 &data->res.seq_res,
3571 task))
3572 return;
3573 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Fred Isamanb029bc92011-03-03 15:13:42 +00003576static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577{
Fred Isamancd841602012-04-20 14:47:44 -04003578 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003580 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003581 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003582 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003584 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003586 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003587 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Fred Isamanb029bc92011-03-03 15:13:42 +00003591static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3592{
3593 if (!nfs4_sequence_done(task, &data->res.seq_res))
3594 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003595 return data->write_done_cb ? data->write_done_cb(task, data) :
3596 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003597}
3598
Trond Myklebust5a37f852012-04-28 14:55:16 -04003599static
3600bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003601{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003602 const struct nfs_pgio_header *hdr = data->header;
3603
3604 /* Don't request attributes for pNFS or O_DIRECT writes */
3605 if (data->ds_clp != NULL || hdr->dreq != NULL)
3606 return false;
3607 /* Otherwise, request attributes if and only if we don't hold
3608 * a delegation
3609 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003610 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003611}
Fred Isamana69aef12011-03-03 15:13:47 +00003612
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003613static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614{
Fred Isamancd841602012-04-20 14:47:44 -04003615 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003616
Trond Myklebust5a37f852012-04-28 14:55:16 -04003617 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003618 data->args.bitmask = NULL;
3619 data->res.fattr = NULL;
3620 } else
3621 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003622
Fred Isamanb029bc92011-03-03 15:13:42 +00003623 if (!data->write_done_cb)
3624 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003625 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 data->timestamp = jiffies;
3627
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003628 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003629 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003632static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3633{
Fred Isamancd841602012-04-20 14:47:44 -04003634 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003635 &data->args.seq_args,
3636 &data->res.seq_res,
3637 task))
3638 return;
3639 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640}
3641
Fred Isaman0b7c0152012-04-20 14:47:39 -04003642static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3643{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3645 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003646 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 return;
3649 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003650}
3651
Fred Isaman0b7c0152012-04-20 14:47:39 -04003652static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
3654 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003655
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003656 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003657 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003658 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661}
3662
Fred Isaman0b7c0152012-04-20 14:47:39 -04003663static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003664{
3665 if (!nfs4_sequence_done(task, &data->res.seq_res))
3666 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003667 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003668}
3669
Fred Isaman0b7c0152012-04-20 14:47:39 -04003670static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003672 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003673
Fred Isaman0b7c0152012-04-20 14:47:39 -04003674 if (data->commit_done_cb == NULL)
3675 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003676 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003677 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003678 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
3680
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003681struct nfs4_renewdata {
3682 struct nfs_client *client;
3683 unsigned long timestamp;
3684};
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686/*
3687 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3688 * standalone procedure for queueing an asynchronous RENEW.
3689 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003690static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003691{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003692 struct nfs4_renewdata *data = calldata;
3693 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003694
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003695 if (atomic_read(&clp->cl_count) > 1)
3696 nfs4_schedule_state_renewal(clp);
3697 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003698 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003699}
3700
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003701static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003703 struct nfs4_renewdata *data = calldata;
3704 struct nfs_client *clp = data->client;
3705 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
3707 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003708 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003709 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3710 return;
3711 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003712 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003713 return;
3714 }
3715 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003717 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718}
3719
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003720static const struct rpc_call_ops nfs4_renew_ops = {
3721 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003722 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003723};
3724
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003725static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
3727 struct rpc_message msg = {
3728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3729 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003730 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003732 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003734 if (renew_flags == 0)
3735 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003736 if (!atomic_inc_not_zero(&clp->cl_count))
3737 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003738 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003739 if (data == NULL)
3740 return -ENOMEM;
3741 data->client = clp;
3742 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003744 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003747static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
3749 struct rpc_message msg = {
3750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3751 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003752 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 };
3754 unsigned long now = jiffies;
3755 int status;
3756
3757 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3758 if (status < 0)
3759 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003760 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return 0;
3762}
3763
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003764static inline int nfs4_server_supports_acls(struct nfs_server *server)
3765{
3766 return (server->caps & NFS_CAP_ACLS)
3767 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3768 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3769}
3770
Trond Myklebust21f498c2012-08-24 10:59:25 -04003771/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3772 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003773 * the stack.
3774 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003775#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003776
Neil Hormane9e3d722011-03-04 19:26:03 -05003777static int buf_to_pages_noslab(const void *buf, size_t buflen,
3778 struct page **pages, unsigned int *pgbase)
3779{
3780 struct page *newpage, **spages;
3781 int rc = 0;
3782 size_t len;
3783 spages = pages;
3784
3785 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003786 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003787 newpage = alloc_page(GFP_KERNEL);
3788
3789 if (newpage == NULL)
3790 goto unwind;
3791 memcpy(page_address(newpage), buf, len);
3792 buf += len;
3793 buflen -= len;
3794 *pages++ = newpage;
3795 rc++;
3796 } while (buflen != 0);
3797
3798 return rc;
3799
3800unwind:
3801 for(; rc > 0; rc--)
3802 __free_page(spages[rc-1]);
3803 return -ENOMEM;
3804}
3805
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003806struct nfs4_cached_acl {
3807 int cached;
3808 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003809 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003810};
3811
3812static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003813{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003814 struct nfs_inode *nfsi = NFS_I(inode);
3815
3816 spin_lock(&inode->i_lock);
3817 kfree(nfsi->nfs4_acl);
3818 nfsi->nfs4_acl = acl;
3819 spin_unlock(&inode->i_lock);
3820}
3821
3822static void nfs4_zap_acl_attr(struct inode *inode)
3823{
3824 nfs4_set_cached_acl(inode, NULL);
3825}
3826
3827static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3828{
3829 struct nfs_inode *nfsi = NFS_I(inode);
3830 struct nfs4_cached_acl *acl;
3831 int ret = -ENOENT;
3832
3833 spin_lock(&inode->i_lock);
3834 acl = nfsi->nfs4_acl;
3835 if (acl == NULL)
3836 goto out;
3837 if (buf == NULL) /* user is just asking for length */
3838 goto out_len;
3839 if (acl->cached == 0)
3840 goto out;
3841 ret = -ERANGE; /* see getxattr(2) man page */
3842 if (acl->len > buflen)
3843 goto out;
3844 memcpy(buf, acl->data, acl->len);
3845out_len:
3846 ret = acl->len;
3847out:
3848 spin_unlock(&inode->i_lock);
3849 return ret;
3850}
3851
Sachin Prabhu5794d212012-04-17 14:36:40 +01003852static 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 +00003853{
3854 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003855 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003856
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003857 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003858 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003859 if (acl == NULL)
3860 goto out;
3861 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003862 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003863 } else {
3864 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3865 if (acl == NULL)
3866 goto out;
3867 acl->cached = 0;
3868 }
3869 acl->len = acl_len;
3870out:
3871 nfs4_set_cached_acl(inode, acl);
3872}
3873
Andy Adamsonbf118a32011-12-07 11:55:27 -05003874/*
3875 * The getxattr API returns the required buffer length when called with a
3876 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3877 * the required buf. On a NULL buf, we send a page of data to the server
3878 * guessing that the ACL request can be serviced by a page. If so, we cache
3879 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3880 * the cache. If not so, we throw away the page, and cache the required
3881 * length. The next getxattr call will then produce another round trip to
3882 * the server, this time with the input buf of the required size.
3883 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003884static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003885{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003886 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003887 struct nfs_getaclargs args = {
3888 .fh = NFS_FH(inode),
3889 .acl_pages = pages,
3890 .acl_len = buflen,
3891 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003892 struct nfs_getaclres res = {
3893 .acl_len = buflen,
3894 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003895 struct rpc_message msg = {
3896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3897 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003898 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003899 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003900 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3901 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003902
Andy Adamsonbf118a32011-12-07 11:55:27 -05003903 /* As long as we're doing a round trip to the server anyway,
3904 * let's be prepared for a page of acl data. */
3905 if (npages == 0)
3906 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003907 if (npages > ARRAY_SIZE(pages))
3908 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003909
Andy Adamsonbf118a32011-12-07 11:55:27 -05003910 for (i = 0; i < npages; i++) {
3911 pages[i] = alloc_page(GFP_KERNEL);
3912 if (!pages[i])
3913 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003914 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003915
3916 /* for decoding across pages */
3917 res.acl_scratch = alloc_page(GFP_KERNEL);
3918 if (!res.acl_scratch)
3919 goto out_free;
3920
Andy Adamsonbf118a32011-12-07 11:55:27 -05003921 args.acl_len = npages * PAGE_SIZE;
3922 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003923
Peng Taode040be2012-01-10 22:42:47 +08003924 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003925 __func__, buf, buflen, npages, args.acl_len);
3926 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3927 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003928 if (ret)
3929 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003930
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003931 /* Handle the case where the passed-in buffer is too short */
3932 if (res.acl_flags & NFS4_ACL_TRUNC) {
3933 /* Did the user only issue a request for the acl length? */
3934 if (buf == NULL)
3935 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003936 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003937 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003938 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003939 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3940 if (buf)
3941 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3942out_ok:
3943 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003944out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003945 for (i = 0; i < npages; i++)
3946 if (pages[i])
3947 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003948 if (res.acl_scratch)
3949 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003950 return ret;
3951}
3952
Trond Myklebust16b42892006-08-24 12:27:15 -04003953static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3954{
3955 struct nfs4_exception exception = { };
3956 ssize_t ret;
3957 do {
3958 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3959 if (ret >= 0)
3960 break;
3961 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3962 } while (exception.retry);
3963 return ret;
3964}
3965
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003966static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3967{
3968 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003969 int ret;
3970
3971 if (!nfs4_server_supports_acls(server))
3972 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003973 ret = nfs_revalidate_inode(server, inode);
3974 if (ret < 0)
3975 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003976 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3977 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003978 ret = nfs4_read_cached_acl(inode, buf, buflen);
3979 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003980 /* -ENOENT is returned if there is no ACL or if there is an ACL
3981 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003982 return ret;
3983 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003984}
3985
Trond Myklebust16b42892006-08-24 12:27:15 -04003986static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003987{
3988 struct nfs_server *server = NFS_SERVER(inode);
3989 struct page *pages[NFS4ACL_MAXPAGES];
3990 struct nfs_setaclargs arg = {
3991 .fh = NFS_FH(inode),
3992 .acl_pages = pages,
3993 .acl_len = buflen,
3994 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003995 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003996 struct rpc_message msg = {
3997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3998 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003999 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004000 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004001 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004002 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004003
4004 if (!nfs4_server_supports_acls(server))
4005 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004006 if (npages > ARRAY_SIZE(pages))
4007 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004008 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4009 if (i < 0)
4010 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004011 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004012 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004013
4014 /*
4015 * Free each page after tx, so the only ref left is
4016 * held by the network stack
4017 */
4018 for (; i > 0; i--)
4019 put_page(pages[i-1]);
4020
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004021 /*
4022 * Acl update can result in inode attribute update.
4023 * so mark the attribute cache invalid.
4024 */
4025 spin_lock(&inode->i_lock);
4026 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4027 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004028 nfs_access_zap_cache(inode);
4029 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004030 return ret;
4031}
4032
Trond Myklebust16b42892006-08-24 12:27:15 -04004033static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4034{
4035 struct nfs4_exception exception = { };
4036 int err;
4037 do {
4038 err = nfs4_handle_exception(NFS_SERVER(inode),
4039 __nfs4_proc_set_acl(inode, buf, buflen),
4040 &exception);
4041 } while (exception.retry);
4042 return err;
4043}
4044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004046nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004048 struct nfs_client *clp = server->nfs_client;
4049
4050 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 return 0;
4052 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004053 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004054 case -NFS4ERR_ADMIN_REVOKED:
4055 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004056 if (state == NULL)
4057 break;
4058 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004059 case -NFS4ERR_OPENMODE:
4060 if (state == NULL)
4061 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004062 nfs4_schedule_stateid_recovery(server, state);
4063 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004064 case -NFS4ERR_EXPIRED:
4065 if (state != NULL)
4066 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004068 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004069 nfs4_schedule_lease_recovery(clp);
4070 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004071#if defined(CONFIG_NFS_V4_1)
4072 case -NFS4ERR_BADSESSION:
4073 case -NFS4ERR_BADSLOT:
4074 case -NFS4ERR_BAD_HIGH_SLOT:
4075 case -NFS4ERR_DEADSESSION:
4076 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4077 case -NFS4ERR_SEQ_FALSE_RETRY:
4078 case -NFS4ERR_SEQ_MISORDERED:
4079 dprintk("%s ERROR %d, Reset session\n", __func__,
4080 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004081 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004082 task->tk_status = 0;
4083 return -EAGAIN;
4084#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004086 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004087 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004088 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4090 task->tk_status = 0;
4091 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004092 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 case -NFS4ERR_OLD_STATEID:
4094 task->tk_status = 0;
4095 return -EAGAIN;
4096 }
4097 task->tk_status = nfs4_map_errors(task->tk_status);
4098 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004099wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004100 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004101 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4102 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4103 task->tk_status = 0;
4104 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105}
4106
Chuck Leverf0920752012-05-21 22:45:41 -04004107static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4108 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004109{
4110 __be32 verf[2];
4111
Chuck Lever2c820d92012-05-21 22:45:33 -04004112 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4113 /* An impossible timestamp guarantees this value
4114 * will never match a generated boot time. */
4115 verf[0] = 0;
4116 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4117 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004118 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4119 verf[0] = (__be32)nn->boot_time.tv_sec;
4120 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004121 }
Chuck Levercd937102012-03-02 17:14:31 -05004122 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4123}
4124
Chuck Levere984a552012-09-14 17:24:21 -04004125static unsigned int
4126nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4127 char *buf, size_t len)
4128{
4129 unsigned int result;
4130
4131 rcu_read_lock();
4132 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4133 clp->cl_ipaddr,
4134 rpc_peeraddr2str(clp->cl_rpcclient,
4135 RPC_DISPLAY_ADDR),
4136 rpc_peeraddr2str(clp->cl_rpcclient,
4137 RPC_DISPLAY_PROTO));
4138 rcu_read_unlock();
4139 return result;
4140}
4141
4142static unsigned int
4143nfs4_init_uniform_client_string(const struct nfs_client *clp,
4144 char *buf, size_t len)
4145{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004146 char *nodename = clp->cl_rpcclient->cl_nodename;
4147
4148 if (nfs4_client_id_uniquifier[0] != '\0')
4149 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004150 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4151 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004152 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004153}
4154
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004155/**
4156 * nfs4_proc_setclientid - Negotiate client ID
4157 * @clp: state data structure
4158 * @program: RPC program for NFSv4 callback service
4159 * @port: IP port number for NFS4 callback service
4160 * @cred: RPC credential to use for this call
4161 * @res: where to place the result
4162 *
4163 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4164 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004165int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4166 unsigned short port, struct rpc_cred *cred,
4167 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
4169 nfs4_verifier sc_verifier;
4170 struct nfs4_setclientid setclientid = {
4171 .sc_verifier = &sc_verifier,
4172 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004173 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 };
4175 struct rpc_message msg = {
4176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4177 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004178 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004179 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004181 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
Chuck Leverde734832012-07-11 16:30:50 -04004183 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004184 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004185 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4186 setclientid.sc_name_len =
4187 nfs4_init_uniform_client_string(clp,
4188 setclientid.sc_name,
4189 sizeof(setclientid.sc_name));
4190 else
4191 setclientid.sc_name_len =
4192 nfs4_init_nonuniform_client_string(clp,
4193 setclientid.sc_name,
4194 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004195 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004196 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004197 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004198 sizeof(setclientid.sc_netid),
4199 rpc_peeraddr2str(clp->cl_rpcclient,
4200 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004201 rcu_read_unlock();
4202 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004203 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 clp->cl_ipaddr, port >> 8, port & 255);
4205
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004206 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4207 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4208 setclientid.sc_name_len, setclientid.sc_name);
4209 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4210 dprintk("NFS reply setclientid: %d\n", status);
4211 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004214/**
4215 * nfs4_proc_setclientid_confirm - Confirm client ID
4216 * @clp: state data structure
4217 * @res: result of a previous SETCLIENTID
4218 * @cred: RPC credential to use for this call
4219 *
4220 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4221 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004222int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004223 struct nfs4_setclientid_res *arg,
4224 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
4226 struct nfs_fsinfo fsinfo;
4227 struct rpc_message msg = {
4228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004229 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004231 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 };
4233 unsigned long now;
4234 int status;
4235
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004236 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4237 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4238 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004240 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 if (status == 0) {
4242 spin_lock(&clp->cl_lock);
4243 clp->cl_lease_time = fsinfo.lease_time * HZ;
4244 clp->cl_last_renewal = now;
4245 spin_unlock(&clp->cl_lock);
4246 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004247 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 return status;
4249}
4250
Trond Myklebustfe650402006-01-03 09:55:18 +01004251struct nfs4_delegreturndata {
4252 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004253 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004254 struct nfs_fh fh;
4255 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004256 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004257 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004258 int rpc_status;
4259};
4260
Trond Myklebustfe650402006-01-03 09:55:18 +01004261static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4262{
4263 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004264
Trond Myklebust14516c32010-07-31 14:29:06 -04004265 if (!nfs4_sequence_done(task, &data->res.seq_res))
4266 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004267
Ricardo Labiaga79708862009-12-07 09:23:21 -05004268 switch (task->tk_status) {
4269 case -NFS4ERR_STALE_STATEID:
4270 case -NFS4ERR_EXPIRED:
4271 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004272 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004273 break;
4274 default:
4275 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4276 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004277 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004278 return;
4279 }
4280 }
4281 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004282}
4283
4284static void nfs4_delegreturn_release(void *calldata)
4285{
Trond Myklebustfe650402006-01-03 09:55:18 +01004286 kfree(calldata);
4287}
4288
Andy Adamson938e1012009-04-01 09:22:28 -04004289#if defined(CONFIG_NFS_V4_1)
4290static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4291{
4292 struct nfs4_delegreturndata *d_data;
4293
4294 d_data = (struct nfs4_delegreturndata *)data;
4295
Trond Myklebust035168ab2010-06-16 09:52:26 -04004296 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004297 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004298 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004299 return;
4300 rpc_call_start(task);
4301}
4302#endif /* CONFIG_NFS_V4_1 */
4303
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004304static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004305#if defined(CONFIG_NFS_V4_1)
4306 .rpc_call_prepare = nfs4_delegreturn_prepare,
4307#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004308 .rpc_call_done = nfs4_delegreturn_done,
4309 .rpc_release = nfs4_delegreturn_release,
4310};
4311
Trond Myklebuste6f81072008-01-24 18:14:34 -05004312static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004313{
4314 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004315 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004316 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004317 struct rpc_message msg = {
4318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4319 .rpc_cred = cred,
4320 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004321 struct rpc_task_setup task_setup_data = {
4322 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004323 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004324 .callback_ops = &nfs4_delegreturn_ops,
4325 .flags = RPC_TASK_ASYNC,
4326 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004327 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004328
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004329 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004330 if (data == NULL)
4331 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004332 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004333 data->args.fhandle = &data->fh;
4334 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004335 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004336 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004337 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004338 data->res.fattr = &data->fattr;
4339 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004340 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004341 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004342 data->rpc_status = 0;
4343
Trond Myklebustc970aa82007-07-14 15:39:59 -04004344 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004345 msg.rpc_argp = &data->args;
4346 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004347 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004348 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004349 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004350 if (!issync)
4351 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004352 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004353 if (status != 0)
4354 goto out;
4355 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004356 if (status == 0)
4357 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4358 else
4359 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004360out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004361 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004362 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363}
4364
Trond Myklebuste6f81072008-01-24 18:14:34 -05004365int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
4367 struct nfs_server *server = NFS_SERVER(inode);
4368 struct nfs4_exception exception = { };
4369 int err;
4370 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004371 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 switch (err) {
4373 case -NFS4ERR_STALE_STATEID:
4374 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 case 0:
4376 return 0;
4377 }
4378 err = nfs4_handle_exception(server, err, &exception);
4379 } while (exception.retry);
4380 return err;
4381}
4382
4383#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4384#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4385
4386/*
4387 * sleep, with exponential backoff, and retry the LOCK operation.
4388 */
4389static unsigned long
4390nfs4_set_lock_task_retry(unsigned long timeout)
4391{
Jeff Laytond3103102011-12-01 22:44:39 +01004392 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 timeout <<= 1;
4394 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4395 return NFS4_LOCK_MAXTIMEOUT;
4396 return timeout;
4397}
4398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4400{
4401 struct inode *inode = state->inode;
4402 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004403 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004404 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004406 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004408 struct nfs_lockt_res res = {
4409 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 };
4411 struct rpc_message msg = {
4412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4413 .rpc_argp = &arg,
4414 .rpc_resp = &res,
4415 .rpc_cred = state->owner->so_cred,
4416 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 struct nfs4_lock_state *lsp;
4418 int status;
4419
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004420 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004421 status = nfs4_set_lock_state(state, request);
4422 if (status != 0)
4423 goto out;
4424 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004425 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004426 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004427 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004428 switch (status) {
4429 case 0:
4430 request->fl_type = F_UNLCK;
4431 break;
4432 case -NFS4ERR_DENIED:
4433 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004435 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004436out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 return status;
4438}
4439
4440static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4441{
4442 struct nfs4_exception exception = { };
4443 int err;
4444
4445 do {
4446 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4447 _nfs4_proc_getlk(state, cmd, request),
4448 &exception);
4449 } while (exception.retry);
4450 return err;
4451}
4452
4453static int do_vfs_lock(struct file *file, struct file_lock *fl)
4454{
4455 int res = 0;
4456 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4457 case FL_POSIX:
4458 res = posix_lock_file_wait(file, fl);
4459 break;
4460 case FL_FLOCK:
4461 res = flock_lock_file_wait(file, fl);
4462 break;
4463 default:
4464 BUG();
4465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return res;
4467}
4468
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004469struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004470 struct nfs_locku_args arg;
4471 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004472 struct nfs4_lock_state *lsp;
4473 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004474 struct file_lock fl;
4475 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004476 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004477};
4478
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004479static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4480 struct nfs_open_context *ctx,
4481 struct nfs4_lock_state *lsp,
4482 struct nfs_seqid *seqid)
4483{
4484 struct nfs4_unlockdata *p;
4485 struct inode *inode = lsp->ls_state->inode;
4486
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004487 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004488 if (p == NULL)
4489 return NULL;
4490 p->arg.fh = NFS_FH(inode);
4491 p->arg.fl = &p->fl;
4492 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004493 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004494 p->arg.stateid = &lsp->ls_stateid;
4495 p->lsp = lsp;
4496 atomic_inc(&lsp->ls_count);
4497 /* Ensure we don't close file until we're done freeing locks! */
4498 p->ctx = get_nfs_open_context(ctx);
4499 memcpy(&p->fl, fl, sizeof(p->fl));
4500 p->server = NFS_SERVER(inode);
4501 return p;
4502}
4503
Trond Myklebust06f814a2006-01-03 09:55:07 +01004504static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004505{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004506 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004507 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004508 nfs4_put_lock_state(calldata->lsp);
4509 put_nfs_open_context(calldata->ctx);
4510 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004511}
4512
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004513static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004514{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004515 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004516
Trond Myklebust14516c32010-07-31 14:29:06 -04004517 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4518 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004519 switch (task->tk_status) {
4520 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004521 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4522 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004523 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004524 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004525 case -NFS4ERR_BAD_STATEID:
4526 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004527 case -NFS4ERR_STALE_STATEID:
4528 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004529 break;
4530 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004531 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004532 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004533 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004534 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004535}
4536
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004537static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004538{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004539 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004541 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004542 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004543 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004544 /* Note: exit _without_ running nfs4_locku_done */
4545 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004546 return;
4547 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004548 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004549 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004550 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004551 &calldata->res.seq_res,
4552 task) != 0)
4553 nfs_release_seqid(calldata->arg.seqid);
4554 else
4555 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556}
4557
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004558static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004559 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004560 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004561 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004562};
4563
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004564static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4565 struct nfs_open_context *ctx,
4566 struct nfs4_lock_state *lsp,
4567 struct nfs_seqid *seqid)
4568{
4569 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004570 struct rpc_message msg = {
4571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4572 .rpc_cred = ctx->cred,
4573 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004574 struct rpc_task_setup task_setup_data = {
4575 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004576 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004577 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004578 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004579 .flags = RPC_TASK_ASYNC,
4580 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004581
Frank Filz137d6ac2007-07-09 15:32:29 -07004582 /* Ensure this is an unlock - when canceling a lock, the
4583 * canceled lock is passed in, and it won't be an unlock.
4584 */
4585 fl->fl_type = F_UNLCK;
4586
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004587 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4588 if (data == NULL) {
4589 nfs_free_seqid(seqid);
4590 return ERR_PTR(-ENOMEM);
4591 }
4592
Trond Myklebust9d12b212012-01-17 22:04:25 -05004593 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004594 msg.rpc_argp = &data->arg;
4595 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004596 task_setup_data.callback_data = data;
4597 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004598}
4599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4601{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004602 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004603 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004604 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004605 struct rpc_task *task;
4606 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004607 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Trond Myklebust9b073572006-06-29 16:38:34 -04004609 status = nfs4_set_lock_state(state, request);
4610 /* Unlock _before_ we do the RPC call */
4611 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004612 down_read(&nfsi->rwsem);
4613 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4614 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004615 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004616 }
4617 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004618 if (status != 0)
4619 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004620 /* Is this a delegated lock? */
4621 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004622 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004623 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004624 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004625 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004626 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004627 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004628 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004629 status = PTR_ERR(task);
4630 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004631 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004632 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004633 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004634out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004635 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004636 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004639struct nfs4_lockdata {
4640 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004641 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004642 struct nfs4_lock_state *lsp;
4643 struct nfs_open_context *ctx;
4644 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004645 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004646 int rpc_status;
4647 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004648 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004649};
4650
4651static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004652 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4653 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004654{
4655 struct nfs4_lockdata *p;
4656 struct inode *inode = lsp->ls_state->inode;
4657 struct nfs_server *server = NFS_SERVER(inode);
4658
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004659 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004660 if (p == NULL)
4661 return NULL;
4662
4663 p->arg.fh = NFS_FH(inode);
4664 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004665 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004666 if (p->arg.open_seqid == NULL)
4667 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004668 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004669 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004670 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004671 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004672 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004673 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004674 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004675 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004676 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004677 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004678 atomic_inc(&lsp->ls_count);
4679 p->ctx = get_nfs_open_context(ctx);
4680 memcpy(&p->fl, fl, sizeof(p->fl));
4681 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004682out_free_seqid:
4683 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004684out_free:
4685 kfree(p);
4686 return NULL;
4687}
4688
4689static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4690{
4691 struct nfs4_lockdata *data = calldata;
4692 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
Harvey Harrison3110ff82008-05-02 13:42:44 -07004694 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004695 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4696 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004697 /* Do we need to do an open_to_lock_owner? */
4698 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004699 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004700 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004701 data->arg.open_stateid = &state->stateid;
4702 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004703 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004704 } else
4705 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004706 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004707 if (nfs4_setup_sequence(data->server,
4708 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004709 &data->res.seq_res,
4710 task) == 0) {
4711 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004712 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004713 }
4714 nfs_release_seqid(data->arg.open_seqid);
4715out_release_lock_seqid:
4716 nfs_release_seqid(data->arg.lock_seqid);
4717 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004718}
4719
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004720static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4721{
4722 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4723 nfs4_lock_prepare(task, calldata);
4724}
4725
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004726static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4727{
4728 struct nfs4_lockdata *data = calldata;
4729
Harvey Harrison3110ff82008-05-02 13:42:44 -07004730 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004731
Trond Myklebust14516c32010-07-31 14:29:06 -04004732 if (!nfs4_sequence_done(task, &data->res.seq_res))
4733 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004734
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004735 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004736 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004737 if (data->rpc_status == 0)
4738 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4739 else
4740 goto out;
4741 }
4742 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004743 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004744 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004745 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004746 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004747out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004748 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004749}
4750
4751static void nfs4_lock_release(void *calldata)
4752{
4753 struct nfs4_lockdata *data = calldata;
4754
Harvey Harrison3110ff82008-05-02 13:42:44 -07004755 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004756 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004757 if (data->cancelled != 0) {
4758 struct rpc_task *task;
4759 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4760 data->arg.lock_seqid);
4761 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004762 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004763 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004764 } else
4765 nfs_free_seqid(data->arg.lock_seqid);
4766 nfs4_put_lock_state(data->lsp);
4767 put_nfs_open_context(data->ctx);
4768 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004769 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004770}
4771
4772static const struct rpc_call_ops nfs4_lock_ops = {
4773 .rpc_call_prepare = nfs4_lock_prepare,
4774 .rpc_call_done = nfs4_lock_done,
4775 .rpc_release = nfs4_lock_release,
4776};
4777
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004778static const struct rpc_call_ops nfs4_recover_lock_ops = {
4779 .rpc_call_prepare = nfs4_recover_lock_prepare,
4780 .rpc_call_done = nfs4_lock_done,
4781 .rpc_release = nfs4_lock_release,
4782};
4783
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004784static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4785{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004786 switch (error) {
4787 case -NFS4ERR_ADMIN_REVOKED:
4788 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004789 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004790 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004791 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004792 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004793 break;
4794 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004795 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004796 case -NFS4ERR_EXPIRED:
4797 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004798 };
4799}
4800
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004801static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004802{
4803 struct nfs4_lockdata *data;
4804 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004805 struct rpc_message msg = {
4806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4807 .rpc_cred = state->owner->so_cred,
4808 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004809 struct rpc_task_setup task_setup_data = {
4810 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004811 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004812 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004813 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004814 .flags = RPC_TASK_ASYNC,
4815 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004816 int ret;
4817
Harvey Harrison3110ff82008-05-02 13:42:44 -07004818 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004819 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004820 fl->fl_u.nfs4_fl.owner,
4821 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004822 if (data == NULL)
4823 return -ENOMEM;
4824 if (IS_SETLKW(cmd))
4825 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004826 if (recovery_type > NFS_LOCK_NEW) {
4827 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004828 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004829 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4830 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004831 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004832 msg.rpc_argp = &data->arg;
4833 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004834 task_setup_data.callback_data = data;
4835 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004836 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004837 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004838 ret = nfs4_wait_for_completion_rpc_task(task);
4839 if (ret == 0) {
4840 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004841 if (ret)
4842 nfs4_handle_setlk_error(data->server, data->lsp,
4843 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004844 } else
4845 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004846 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004847 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004848 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849}
4850
4851static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4852{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004853 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004854 struct nfs4_exception exception = {
4855 .inode = state->inode,
4856 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004857 int err;
4858
4859 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004860 /* Cache the lock if possible... */
4861 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4862 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004863 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004864 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004865 break;
4866 nfs4_handle_exception(server, err, &exception);
4867 } while (exception.retry);
4868 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869}
4870
4871static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4872{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004873 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004874 struct nfs4_exception exception = {
4875 .inode = state->inode,
4876 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004877 int err;
4878
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004879 err = nfs4_set_lock_state(state, request);
4880 if (err != 0)
4881 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004882 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004883 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4884 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004885 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004886 switch (err) {
4887 default:
4888 goto out;
4889 case -NFS4ERR_GRACE:
4890 case -NFS4ERR_DELAY:
4891 nfs4_handle_exception(server, err, &exception);
4892 err = 0;
4893 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004894 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004895out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004896 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897}
4898
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004899#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004900/**
4901 * nfs41_check_expired_locks - possibly free a lock stateid
4902 *
4903 * @state: NFSv4 state for an inode
4904 *
4905 * Returns NFS_OK if recovery for this stateid is now finished.
4906 * Otherwise a negative NFS4ERR value is returned.
4907 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004908static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004909{
Chuck Levereb64cf92012-07-11 16:30:05 -04004910 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004911 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004912 struct nfs_server *server = NFS_SERVER(state->inode);
4913
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004914 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004915 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004916 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4917 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004918 /* Free the stateid unless the server
4919 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004920 if (status != -NFS4ERR_BAD_STATEID)
4921 nfs41_free_stateid(server,
4922 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004923 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004924 ret = status;
4925 }
4926 }
4927 };
4928
4929 return ret;
4930}
4931
4932static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4933{
4934 int status = NFS_OK;
4935
4936 if (test_bit(LK_STATE_IN_USE, &state->flags))
4937 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004938 if (status != NFS_OK)
4939 status = nfs4_lock_expired(state, request);
4940 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004941}
4942#endif
4943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4945{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004946 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004947 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004948 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004950 if ((fl_flags & FL_POSIX) &&
4951 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4952 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004953 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004954 status = nfs4_set_lock_state(state, request);
4955 if (status != 0)
4956 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004957 request->fl_flags |= FL_ACCESS;
4958 status = do_vfs_lock(request->fl_file, request);
4959 if (status < 0)
4960 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004961 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004962 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004963 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004964 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004965 request->fl_flags = fl_flags & ~FL_SLEEP;
4966 status = do_vfs_lock(request->fl_file, request);
4967 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004968 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004969 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004970 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004971 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004972 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004973 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004974 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004975 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4976 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004977out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004978 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004979out:
4980 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 return status;
4982}
4983
4984static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4985{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004986 struct nfs4_exception exception = {
4987 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004988 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004989 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 int err;
4991
4992 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004993 err = _nfs4_proc_setlk(state, cmd, request);
4994 if (err == -NFS4ERR_DENIED)
4995 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004997 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 } while (exception.retry);
4999 return err;
5000}
5001
5002static int
5003nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5004{
5005 struct nfs_open_context *ctx;
5006 struct nfs4_state *state;
5007 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5008 int status;
5009
5010 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005011 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 state = ctx->state;
5013
5014 if (request->fl_start < 0 || request->fl_end < 0)
5015 return -EINVAL;
5016
Trond Myklebustd9531262009-07-21 19:22:38 -04005017 if (IS_GETLK(cmd)) {
5018 if (state != NULL)
5019 return nfs4_proc_getlk(state, F_GETLK, request);
5020 return 0;
5021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
5023 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5024 return -EINVAL;
5025
Trond Myklebustd9531262009-07-21 19:22:38 -04005026 if (request->fl_type == F_UNLCK) {
5027 if (state != NULL)
5028 return nfs4_proc_unlck(state, cmd, request);
5029 return 0;
5030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
Trond Myklebustd9531262009-07-21 19:22:38 -04005032 if (state == NULL)
5033 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005034 /*
5035 * Don't rely on the VFS having checked the file open mode,
5036 * since it won't do this for flock() locks.
5037 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005038 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005039 case F_RDLCK:
5040 if (!(filp->f_mode & FMODE_READ))
5041 return -EBADF;
5042 break;
5043 case F_WRLCK:
5044 if (!(filp->f_mode & FMODE_WRITE))
5045 return -EBADF;
5046 }
5047
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 do {
5049 status = nfs4_proc_setlk(state, cmd, request);
5050 if ((status != -EAGAIN) || IS_SETLK(cmd))
5051 break;
5052 timeout = nfs4_set_lock_task_retry(timeout);
5053 status = -ERESTARTSYS;
5054 if (signalled())
5055 break;
5056 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 return status;
5058}
5059
Trond Myklebust888e6942005-11-04 15:38:11 -05005060int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5061{
5062 struct nfs_server *server = NFS_SERVER(state->inode);
5063 struct nfs4_exception exception = { };
5064 int err;
5065
5066 err = nfs4_set_lock_state(state, fl);
5067 if (err != 0)
5068 goto out;
5069 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005070 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005071 switch (err) {
5072 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005073 printk(KERN_ERR "NFS: %s: unhandled error "
5074 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005075 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005076 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005077 goto out;
5078 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005079 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005080 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005081 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005082 nfs4_schedule_lease_recovery(server->nfs_client);
5083 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005084 case -NFS4ERR_BADSESSION:
5085 case -NFS4ERR_BADSLOT:
5086 case -NFS4ERR_BAD_HIGH_SLOT:
5087 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5088 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005089 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005090 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005091 case -ERESTARTSYS:
5092 /*
5093 * The show must go on: exit, but mark the
5094 * stateid as needing recovery.
5095 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005096 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005097 case -NFS4ERR_ADMIN_REVOKED:
5098 case -NFS4ERR_BAD_STATEID:
5099 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005100 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005101 err = 0;
5102 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005103 case -EKEYEXPIRED:
5104 /*
5105 * User RPCSEC_GSS context has expired.
5106 * We cannot recover this stateid now, so
5107 * skip it and allow recovery thread to
5108 * proceed.
5109 */
5110 err = 0;
5111 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005112 case -ENOMEM:
5113 case -NFS4ERR_DENIED:
5114 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5115 err = 0;
5116 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005117 case -NFS4ERR_DELAY:
5118 break;
5119 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005120 err = nfs4_handle_exception(server, err, &exception);
5121 } while (exception.retry);
5122out:
5123 return err;
5124}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005125
Trond Myklebustcf470c32012-03-07 13:49:12 -05005126struct nfs_release_lockowner_data {
5127 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005128 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005129 struct nfs_release_lockowner_args args;
5130};
5131
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005132static void nfs4_release_lockowner_release(void *calldata)
5133{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005134 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005135 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005136 kfree(calldata);
5137}
5138
Trond Myklebust17280172012-03-11 13:11:00 -04005139static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005140 .rpc_release = nfs4_release_lockowner_release,
5141};
5142
Trond Myklebustcf470c32012-03-07 13:49:12 -05005143int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005144{
5145 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005146 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005147 struct rpc_message msg = {
5148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5149 };
5150
5151 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005152 return -EINVAL;
5153 data = kmalloc(sizeof(*data), GFP_NOFS);
5154 if (!data)
5155 return -ENOMEM;
5156 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005157 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005158 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5159 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5160 data->args.lock_owner.s_dev = server->s_dev;
5161 msg.rpc_argp = &data->args;
5162 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5163 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005164}
5165
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005166#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5167
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005168static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5169 const void *buf, size_t buflen,
5170 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005171{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005172 if (strcmp(key, "") != 0)
5173 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005174
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005175 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005176}
5177
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005178static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5179 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005180{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005181 if (strcmp(key, "") != 0)
5182 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005183
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005184 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005185}
5186
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005187static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5188 size_t list_len, const char *name,
5189 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005190{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005191 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005192
J. Bruce Fields096455a2006-03-20 23:23:42 -05005193 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5194 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005195
5196 if (list && len <= list_len)
5197 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005198 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005199}
5200
Andy Adamson533eb462011-06-13 18:25:56 -04005201/*
5202 * nfs_fhget will use either the mounted_on_fileid or the fileid
5203 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005204static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5205{
Andy Adamson533eb462011-06-13 18:25:56 -04005206 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5207 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5208 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005209 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005210 return;
5211
5212 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005213 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005214 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5215 fattr->nlink = 2;
5216}
5217
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005218static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5219 const struct qstr *name,
5220 struct nfs4_fs_locations *fs_locations,
5221 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005222{
5223 struct nfs_server *server = NFS_SERVER(dir);
5224 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005225 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005226 };
5227 struct nfs4_fs_locations_arg args = {
5228 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005229 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005230 .page = page,
5231 .bitmask = bitmask,
5232 };
Benny Halevy22958462009-04-01 09:22:02 -04005233 struct nfs4_fs_locations_res res = {
5234 .fs_locations = fs_locations,
5235 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005236 struct rpc_message msg = {
5237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5238 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005239 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005240 };
5241 int status;
5242
Harvey Harrison3110ff82008-05-02 13:42:44 -07005243 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005244
5245 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5246 * is not supported */
5247 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5248 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5249 else
5250 bitmask[0] |= FATTR4_WORD0_FILEID;
5251
Trond Myklebustc228fd32007-01-13 02:28:11 -05005252 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005253 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005254 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005255 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005256 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005257 return status;
5258}
5259
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005260int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5261 const struct qstr *name,
5262 struct nfs4_fs_locations *fs_locations,
5263 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005264{
5265 struct nfs4_exception exception = { };
5266 int err;
5267 do {
5268 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005269 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005270 &exception);
5271 } while (exception.retry);
5272 return err;
5273}
5274
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005275static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5276{
5277 int status;
5278 struct nfs4_secinfo_arg args = {
5279 .dir_fh = NFS_FH(dir),
5280 .name = name,
5281 };
5282 struct nfs4_secinfo_res res = {
5283 .flavors = flavors,
5284 };
5285 struct rpc_message msg = {
5286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5287 .rpc_argp = &args,
5288 .rpc_resp = &res,
5289 };
5290
5291 dprintk("NFS call secinfo %s\n", name->name);
5292 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5293 dprintk("NFS reply secinfo: %d\n", status);
5294 return status;
5295}
5296
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005297int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5298 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005299{
5300 struct nfs4_exception exception = { };
5301 int err;
5302 do {
5303 err = nfs4_handle_exception(NFS_SERVER(dir),
5304 _nfs4_proc_secinfo(dir, name, flavors),
5305 &exception);
5306 } while (exception.retry);
5307 return err;
5308}
5309
Andy Adamson557134a2009-04-01 09:21:53 -04005310#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005311/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005312 * Check the exchange flags returned by the server for invalid flags, having
5313 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5314 * DS flags set.
5315 */
5316static int nfs4_check_cl_exchange_flags(u32 flags)
5317{
5318 if (flags & ~EXCHGID4_FLAG_MASK_R)
5319 goto out_inval;
5320 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5321 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5322 goto out_inval;
5323 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5324 goto out_inval;
5325 return NFS_OK;
5326out_inval:
5327 return -NFS4ERR_INVAL;
5328}
5329
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005330static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005331nfs41_same_server_scope(struct nfs41_server_scope *a,
5332 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005333{
5334 if (a->server_scope_sz == b->server_scope_sz &&
5335 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5336 return true;
5337
5338 return false;
5339}
5340
Andy Adamson357f54d2010-12-14 10:11:57 -05005341/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005342 * nfs4_proc_bind_conn_to_session()
5343 *
5344 * The 4.1 client currently uses the same TCP connection for the
5345 * fore and backchannel.
5346 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005347int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005348{
5349 int status;
5350 struct nfs41_bind_conn_to_session_res res;
5351 struct rpc_message msg = {
5352 .rpc_proc =
5353 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5354 .rpc_argp = clp,
5355 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005356 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005357 };
5358
5359 dprintk("--> %s\n", __func__);
5360 BUG_ON(clp == NULL);
5361
5362 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5363 if (unlikely(res.session == NULL)) {
5364 status = -ENOMEM;
5365 goto out;
5366 }
5367
5368 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5369 if (status == 0) {
5370 if (memcmp(res.session->sess_id.data,
5371 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5372 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5373 status = -EIO;
5374 goto out_session;
5375 }
5376 if (res.dir != NFS4_CDFS4_BOTH) {
5377 dprintk("NFS: %s: Unexpected direction from server\n",
5378 __func__);
5379 status = -EIO;
5380 goto out_session;
5381 }
5382 if (res.use_conn_in_rdma_mode) {
5383 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5384 __func__);
5385 status = -EIO;
5386 goto out_session;
5387 }
5388 }
5389out_session:
5390 kfree(res.session);
5391out:
5392 dprintk("<-- %s status= %d\n", __func__, status);
5393 return status;
5394}
5395
5396/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005397 * nfs4_proc_exchange_id()
5398 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005399 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5400 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005401 * Since the clientid has expired, all compounds using sessions
5402 * associated with the stale clientid will be returning
5403 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5404 * be in some phase of session reset.
5405 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005406int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005407{
5408 nfs4_verifier verifier;
5409 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005410 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005411 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005412 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005413 };
5414 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005415 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005416 };
5417 int status;
5418 struct rpc_message msg = {
5419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5420 .rpc_argp = &args,
5421 .rpc_resp = &res,
5422 .rpc_cred = cred,
5423 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005424
Chuck Leverf0920752012-05-21 22:45:41 -04005425 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005426 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5427 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005428 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5429 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5430 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005431
Chuck Leveracdeb692012-05-21 22:46:16 -04005432 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005433 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005434 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005435 status = -ENOMEM;
5436 goto out;
5437 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005438
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005439 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005440 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005441 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005442 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005443 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005444 }
5445
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005446 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005447 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005448 status = -ENOMEM;
5449 goto out_server_scope;
5450 }
5451
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005452 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005453 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005454 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005455
Chuck Lever177313f2012-05-21 22:44:58 -04005456 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005457 clp->cl_clientid = res.clientid;
5458 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5459 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5460 clp->cl_seqid = res.seqid;
5461
Chuck Leveracdeb692012-05-21 22:46:16 -04005462 kfree(clp->cl_serverowner);
5463 clp->cl_serverowner = res.server_owner;
5464 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005465
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005466 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005467 kfree(clp->cl_implid);
5468 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005469
Chuck Lever177313f2012-05-21 22:44:58 -04005470 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005471 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005472 res.server_scope)) {
5473 dprintk("%s: server_scope mismatch detected\n",
5474 __func__);
5475 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005476 kfree(clp->cl_serverscope);
5477 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005478 }
5479
Chuck Lever177313f2012-05-21 22:44:58 -04005480 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005481 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005482 goto out;
5483 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005484 } else
5485 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005486
Chuck Leveracdeb692012-05-21 22:46:16 -04005487out_server_owner:
5488 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005489out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005490 kfree(res.server_scope);
5491out:
Chuck Lever177313f2012-05-21 22:44:58 -04005492 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005493 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005494 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005495 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005496 clp->cl_implid->date.seconds,
5497 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005498 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005499 return status;
5500}
5501
Trond Myklebust66245532012-05-25 17:18:09 -04005502static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5503 struct rpc_cred *cred)
5504{
5505 struct rpc_message msg = {
5506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5507 .rpc_argp = clp,
5508 .rpc_cred = cred,
5509 };
5510 int status;
5511
5512 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5513 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005514 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005515 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5516 return status;
5517}
5518
5519static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5520 struct rpc_cred *cred)
5521{
5522 unsigned int loop;
5523 int ret;
5524
5525 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5526 ret = _nfs4_proc_destroy_clientid(clp, cred);
5527 switch (ret) {
5528 case -NFS4ERR_DELAY:
5529 case -NFS4ERR_CLIENTID_BUSY:
5530 ssleep(1);
5531 break;
5532 default:
5533 return ret;
5534 }
5535 }
5536 return 0;
5537}
5538
5539int nfs4_destroy_clientid(struct nfs_client *clp)
5540{
5541 struct rpc_cred *cred;
5542 int ret = 0;
5543
5544 if (clp->cl_mvops->minor_version < 1)
5545 goto out;
5546 if (clp->cl_exchange_flags == 0)
5547 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005548 if (clp->cl_preserve_clid)
5549 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005550 cred = nfs4_get_exchange_id_cred(clp);
5551 ret = nfs4_proc_destroy_clientid(clp, cred);
5552 if (cred)
5553 put_rpccred(cred);
5554 switch (ret) {
5555 case 0:
5556 case -NFS4ERR_STALE_CLIENTID:
5557 clp->cl_exchange_flags = 0;
5558 }
5559out:
5560 return ret;
5561}
5562
Andy Adamson2050f0c2009-04-01 09:22:30 -04005563struct nfs4_get_lease_time_data {
5564 struct nfs4_get_lease_time_args *args;
5565 struct nfs4_get_lease_time_res *res;
5566 struct nfs_client *clp;
5567};
5568
5569static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5570 void *calldata)
5571{
5572 int ret;
5573 struct nfs4_get_lease_time_data *data =
5574 (struct nfs4_get_lease_time_data *)calldata;
5575
5576 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005577 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005578 /* just setup sequence, do not trigger session recovery
5579 since we're invoked within one */
5580 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005581 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005582 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005583
5584 BUG_ON(ret == -EAGAIN);
5585 rpc_call_start(task);
5586 dprintk("<-- %s\n", __func__);
5587}
5588
5589/*
5590 * Called from nfs4_state_manager thread for session setup, so don't recover
5591 * from sequence operation or clientid errors.
5592 */
5593static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5594{
5595 struct nfs4_get_lease_time_data *data =
5596 (struct nfs4_get_lease_time_data *)calldata;
5597
5598 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005599 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5600 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005601 switch (task->tk_status) {
5602 case -NFS4ERR_DELAY:
5603 case -NFS4ERR_GRACE:
5604 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5605 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5606 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005607 /* fall through */
5608 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005609 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005610 return;
5611 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005612 dprintk("<-- %s\n", __func__);
5613}
5614
Trond Myklebust17280172012-03-11 13:11:00 -04005615static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005616 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5617 .rpc_call_done = nfs4_get_lease_time_done,
5618};
5619
5620int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5621{
5622 struct rpc_task *task;
5623 struct nfs4_get_lease_time_args args;
5624 struct nfs4_get_lease_time_res res = {
5625 .lr_fsinfo = fsinfo,
5626 };
5627 struct nfs4_get_lease_time_data data = {
5628 .args = &args,
5629 .res = &res,
5630 .clp = clp,
5631 };
5632 struct rpc_message msg = {
5633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5634 .rpc_argp = &args,
5635 .rpc_resp = &res,
5636 };
5637 struct rpc_task_setup task_setup = {
5638 .rpc_client = clp->cl_rpcclient,
5639 .rpc_message = &msg,
5640 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005641 .callback_data = &data,
5642 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005643 };
5644 int status;
5645
Trond Myklebust9d12b212012-01-17 22:04:25 -05005646 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005647 dprintk("--> %s\n", __func__);
5648 task = rpc_run_task(&task_setup);
5649
5650 if (IS_ERR(task))
5651 status = PTR_ERR(task);
5652 else {
5653 status = task->tk_status;
5654 rpc_put_task(task);
5655 }
5656 dprintk("<-- %s return %d\n", __func__, status);
5657
5658 return status;
5659}
5660
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005661static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5662{
5663 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5664}
5665
5666static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5667 struct nfs4_slot *new,
5668 u32 max_slots,
5669 u32 ivalue)
5670{
5671 struct nfs4_slot *old = NULL;
5672 u32 i;
5673
5674 spin_lock(&tbl->slot_tbl_lock);
5675 if (new) {
5676 old = tbl->slots;
5677 tbl->slots = new;
5678 tbl->max_slots = max_slots;
5679 }
5680 tbl->highest_used_slotid = -1; /* no slot is currently used */
5681 for (i = 0; i < tbl->max_slots; i++)
5682 tbl->slots[i].seq_nr = ivalue;
5683 spin_unlock(&tbl->slot_tbl_lock);
5684 kfree(old);
5685}
5686
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005687/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005688 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005689 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005690static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5691 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005692{
Andy Adamson104aeba2010-01-14 17:45:10 -05005693 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005694 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005695
Andy Adamson104aeba2010-01-14 17:45:10 -05005696 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5697 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005698
Andy Adamson104aeba2010-01-14 17:45:10 -05005699 /* Does the newly negotiated max_reqs match the existing slot table? */
5700 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005701 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005702 if (!new)
5703 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005704 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005705 ret = 0;
5706
5707 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005708 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5709 tbl, tbl->slots, tbl->max_slots);
5710out:
5711 dprintk("<-- %s: return %d\n", __func__, ret);
5712 return ret;
5713}
5714
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005715/* Destroy the slot table */
5716static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5717{
5718 if (session->fc_slot_table.slots != NULL) {
5719 kfree(session->fc_slot_table.slots);
5720 session->fc_slot_table.slots = NULL;
5721 }
5722 if (session->bc_slot_table.slots != NULL) {
5723 kfree(session->bc_slot_table.slots);
5724 session->bc_slot_table.slots = NULL;
5725 }
5726 return;
5727}
5728
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005729/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005730 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005731 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005732static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005733{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005734 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005735 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005736
Andy Adamsonaacd5532011-11-09 13:58:21 -05005737 dprintk("--> %s\n", __func__);
5738 /* Fore channel */
5739 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005740 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5741 if (status) /* -ENOMEM */
5742 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005743 /* Back channel */
5744 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005745 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5746 if (status && tbl->slots == NULL)
5747 /* Fore and back channel share a connection so get
5748 * both slot tables or neither */
5749 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005750 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005751}
5752
5753struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5754{
5755 struct nfs4_session *session;
5756 struct nfs4_slot_table *tbl;
5757
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005758 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005759 if (!session)
5760 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005761
Andy Adamson557134a2009-04-01 09:21:53 -04005762 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005763 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005764 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005765 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005766 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005767
5768 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005769 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005770 spin_lock_init(&tbl->slot_tbl_lock);
5771 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005772 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005773
Trond Myklebust1055d762010-06-16 09:52:27 -04005774 session->session_state = 1<<NFS4_SESSION_INITING;
5775
Andy Adamson557134a2009-04-01 09:21:53 -04005776 session->clp = clp;
5777 return session;
5778}
5779
5780void nfs4_destroy_session(struct nfs4_session *session)
5781{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005782 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005783 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005784
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005785 cred = nfs4_get_exchange_id_cred(session->clp);
5786 nfs4_proc_destroy_session(session, cred);
5787 if (cred)
5788 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005789
5790 rcu_read_lock();
5791 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5792 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005793 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005794 __func__, xprt);
5795 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005796 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005797 kfree(session);
5798}
5799
Andy Adamsonfc931582009-04-01 09:22:31 -04005800/*
5801 * Initialize the values to be used by the client in CREATE_SESSION
5802 * If nfs4_init_session set the fore channel request and response sizes,
5803 * use them.
5804 *
5805 * Set the back channel max_resp_sz_cached to zero to force the client to
5806 * always set csa_cachethis to FALSE because the current implementation
5807 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5808 */
5809static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5810{
5811 struct nfs4_session *session = args->client->cl_session;
5812 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5813 mxresp_sz = session->fc_attrs.max_resp_sz;
5814
5815 if (mxrqst_sz == 0)
5816 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5817 if (mxresp_sz == 0)
5818 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5819 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005820 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5821 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005822 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005823 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005824
5825 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005826 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005827 __func__,
5828 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005829 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005830
5831 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005832 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5833 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5834 args->bc_attrs.max_resp_sz_cached = 0;
5835 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5836 args->bc_attrs.max_reqs = 1;
5837
5838 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5839 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5840 __func__,
5841 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5842 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5843 args->bc_attrs.max_reqs);
5844}
5845
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005846static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005847{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005848 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5849 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5850
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005851 if (rcvd->max_resp_sz > sent->max_resp_sz)
5852 return -EINVAL;
5853 /*
5854 * Our requested max_ops is the minimum we need; we're not
5855 * prepared to break up compounds into smaller pieces than that.
5856 * So, no point even trying to continue if the server won't
5857 * cooperate:
5858 */
5859 if (rcvd->max_ops < sent->max_ops)
5860 return -EINVAL;
5861 if (rcvd->max_reqs == 0)
5862 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005863 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5864 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005865 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005866}
5867
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005868static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5869{
5870 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5871 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005872
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005873 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5874 return -EINVAL;
5875 if (rcvd->max_resp_sz < sent->max_resp_sz)
5876 return -EINVAL;
5877 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5878 return -EINVAL;
5879 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005880 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005881 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005882 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005883 return -EINVAL;
5884 return 0;
5885}
Andy Adamson8d353012009-04-01 09:22:32 -04005886
Andy Adamson8d353012009-04-01 09:22:32 -04005887static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5888 struct nfs4_session *session)
5889{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005890 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005891
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005892 ret = nfs4_verify_fore_channel_attrs(args, session);
5893 if (ret)
5894 return ret;
5895 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005896}
5897
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005898static int _nfs4_proc_create_session(struct nfs_client *clp,
5899 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005900{
5901 struct nfs4_session *session = clp->cl_session;
5902 struct nfs41_create_session_args args = {
5903 .client = clp,
5904 .cb_program = NFS4_CALLBACK,
5905 };
5906 struct nfs41_create_session_res res = {
5907 .client = clp,
5908 };
5909 struct rpc_message msg = {
5910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5911 .rpc_argp = &args,
5912 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005913 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005914 };
5915 int status;
5916
5917 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005918 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005919
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005920 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005921
Andy Adamson8d353012009-04-01 09:22:32 -04005922 if (!status)
5923 /* Verify the session's negotiated channel_attrs values */
5924 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005925 if (!status) {
5926 /* Increment the clientid slot sequence id */
5927 clp->cl_seqid++;
5928 }
5929
5930 return status;
5931}
5932
5933/*
5934 * Issues a CREATE_SESSION operation to the server.
5935 * It is the responsibility of the caller to verify the session is
5936 * expired before calling this routine.
5937 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005938int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005939{
5940 int status;
5941 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005942 struct nfs4_session *session = clp->cl_session;
5943
5944 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5945
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005946 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005947 if (status)
5948 goto out;
5949
Andy Adamsonaacd5532011-11-09 13:58:21 -05005950 /* Init or reset the session slot tables */
5951 status = nfs4_setup_session_slot_tables(session);
5952 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005953 if (status)
5954 goto out;
5955
5956 ptr = (unsigned *)&session->sess_id.data[0];
5957 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5958 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005959out:
5960 dprintk("<-- %s\n", __func__);
5961 return status;
5962}
5963
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005964/*
5965 * Issue the over-the-wire RPC DESTROY_SESSION.
5966 * The caller must serialize access to this routine.
5967 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005968int nfs4_proc_destroy_session(struct nfs4_session *session,
5969 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005970{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005971 struct rpc_message msg = {
5972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5973 .rpc_argp = session,
5974 .rpc_cred = cred,
5975 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005976 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005977
5978 dprintk("--> nfs4_proc_destroy_session\n");
5979
5980 /* session is still being setup */
5981 if (session->clp->cl_cons_state != NFS_CS_READY)
5982 return status;
5983
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005984 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005985
5986 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005987 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005988 "Session has been destroyed regardless...\n", status);
5989
5990 dprintk("<-- nfs4_proc_destroy_session\n");
5991 return status;
5992}
5993
Trond Myklebust7b38c362012-05-23 13:23:31 -04005994/*
5995 * With sessions, the client is not marked ready until after a
5996 * successful EXCHANGE_ID and CREATE_SESSION.
5997 *
5998 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5999 * other versions of NFS can be tried.
6000 */
6001static int nfs41_check_session_ready(struct nfs_client *clp)
6002{
6003 int ret;
6004
6005 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6006 ret = nfs4_client_recover_expired_lease(clp);
6007 if (ret)
6008 return ret;
6009 }
6010 if (clp->cl_cons_state < NFS_CS_READY)
6011 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006012 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006013 return 0;
6014}
6015
Trond Myklebustfccba802009-07-21 16:48:07 -04006016int nfs4_init_session(struct nfs_server *server)
6017{
6018 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006019 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05006020 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04006021
6022 if (!nfs4_has_session(clp))
6023 return 0;
6024
Trond Myklebust1055d762010-06-16 09:52:27 -04006025 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006026 spin_lock(&clp->cl_lock);
6027 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04006028
Trond Myklebust7b38c362012-05-23 13:23:31 -04006029 rsize = server->rsize;
6030 if (rsize == 0)
6031 rsize = NFS_MAX_FILE_IO_SIZE;
6032 wsize = server->wsize;
6033 if (wsize == 0)
6034 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05006035
Trond Myklebust7b38c362012-05-23 13:23:31 -04006036 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
6037 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
6038 }
6039 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006040
Trond Myklebust7b38c362012-05-23 13:23:31 -04006041 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006042}
6043
Trond Myklebust7b38c362012-05-23 13:23:31 -04006044int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006045{
6046 struct nfs4_session *session = clp->cl_session;
6047 int ret;
6048
Trond Myklebust7b38c362012-05-23 13:23:31 -04006049 spin_lock(&clp->cl_lock);
6050 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6051 /*
6052 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6053 * DS lease to be equal to the MDS lease.
6054 */
6055 clp->cl_lease_time = lease_time;
6056 clp->cl_last_renewal = jiffies;
6057 }
6058 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006059
Trond Myklebust7b38c362012-05-23 13:23:31 -04006060 ret = nfs41_check_session_ready(clp);
6061 if (ret)
6062 return ret;
6063 /* Test for the DS role */
6064 if (!is_ds_client(clp))
6065 return -ENODEV;
6066 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006067}
6068EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6069
6070
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006071/*
6072 * Renew the cl_session lease.
6073 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006074struct nfs4_sequence_data {
6075 struct nfs_client *clp;
6076 struct nfs4_sequence_args args;
6077 struct nfs4_sequence_res res;
6078};
6079
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006080static void nfs41_sequence_release(void *data)
6081{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006082 struct nfs4_sequence_data *calldata = data;
6083 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006084
Alexandros Batsakis71358402010-02-05 03:45:05 -08006085 if (atomic_read(&clp->cl_count) > 1)
6086 nfs4_schedule_state_renewal(clp);
6087 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006088 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006089}
6090
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006091static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6092{
6093 switch(task->tk_status) {
6094 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006095 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6096 return -EAGAIN;
6097 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006098 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006099 }
6100 return 0;
6101}
6102
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006103static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006104{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006105 struct nfs4_sequence_data *calldata = data;
6106 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006107
Trond Myklebust14516c32010-07-31 14:29:06 -04006108 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6109 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006110
6111 if (task->tk_status < 0) {
6112 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006113 if (atomic_read(&clp->cl_count) == 1)
6114 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006115
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006116 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6117 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006118 return;
6119 }
6120 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006121 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006122out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006123 dprintk("<-- %s\n", __func__);
6124}
6125
6126static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6127{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006128 struct nfs4_sequence_data *calldata = data;
6129 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006130 struct nfs4_sequence_args *args;
6131 struct nfs4_sequence_res *res;
6132
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006133 args = task->tk_msg.rpc_argp;
6134 res = task->tk_msg.rpc_resp;
6135
Trond Myklebust9d12b212012-01-17 22:04:25 -05006136 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006137 return;
6138 rpc_call_start(task);
6139}
6140
6141static const struct rpc_call_ops nfs41_sequence_ops = {
6142 .rpc_call_done = nfs41_sequence_call_done,
6143 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006144 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006145};
6146
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006147static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006148{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006149 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006150 struct rpc_message msg = {
6151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6152 .rpc_cred = cred,
6153 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006154 struct rpc_task_setup task_setup_data = {
6155 .rpc_client = clp->cl_rpcclient,
6156 .rpc_message = &msg,
6157 .callback_ops = &nfs41_sequence_ops,
6158 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6159 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006160
Alexandros Batsakis71358402010-02-05 03:45:05 -08006161 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006162 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006163 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006164 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006165 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006166 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006167 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006168 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006169 msg.rpc_argp = &calldata->args;
6170 msg.rpc_resp = &calldata->res;
6171 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006172 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006173
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006174 return rpc_run_task(&task_setup_data);
6175}
6176
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006177static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006178{
6179 struct rpc_task *task;
6180 int ret = 0;
6181
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006182 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6183 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006184 task = _nfs41_proc_sequence(clp, cred);
6185 if (IS_ERR(task))
6186 ret = PTR_ERR(task);
6187 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006188 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006189 dprintk("<-- %s status=%d\n", __func__, ret);
6190 return ret;
6191}
6192
6193static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6194{
6195 struct rpc_task *task;
6196 int ret;
6197
6198 task = _nfs41_proc_sequence(clp, cred);
6199 if (IS_ERR(task)) {
6200 ret = PTR_ERR(task);
6201 goto out;
6202 }
6203 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006204 if (!ret) {
6205 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6206
6207 if (task->tk_status == 0)
6208 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006209 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006210 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006211 rpc_put_task(task);
6212out:
6213 dprintk("<-- %s status=%d\n", __func__, ret);
6214 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006215}
6216
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006217struct nfs4_reclaim_complete_data {
6218 struct nfs_client *clp;
6219 struct nfs41_reclaim_complete_args arg;
6220 struct nfs41_reclaim_complete_res res;
6221};
6222
6223static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6224{
6225 struct nfs4_reclaim_complete_data *calldata = data;
6226
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006227 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04006228 if (nfs41_setup_sequence(calldata->clp->cl_session,
6229 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006230 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006231 return;
6232
6233 rpc_call_start(task);
6234}
6235
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006236static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6237{
6238 switch(task->tk_status) {
6239 case 0:
6240 case -NFS4ERR_COMPLETE_ALREADY:
6241 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6242 break;
6243 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006244 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006245 /* fall through */
6246 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006247 return -EAGAIN;
6248 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006249 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006250 }
6251 return 0;
6252}
6253
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006254static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6255{
6256 struct nfs4_reclaim_complete_data *calldata = data;
6257 struct nfs_client *clp = calldata->clp;
6258 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6259
6260 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006261 if (!nfs41_sequence_done(task, res))
6262 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006263
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006264 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6265 rpc_restart_call_prepare(task);
6266 return;
6267 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006268 dprintk("<-- %s\n", __func__);
6269}
6270
6271static void nfs4_free_reclaim_complete_data(void *data)
6272{
6273 struct nfs4_reclaim_complete_data *calldata = data;
6274
6275 kfree(calldata);
6276}
6277
6278static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6279 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6280 .rpc_call_done = nfs4_reclaim_complete_done,
6281 .rpc_release = nfs4_free_reclaim_complete_data,
6282};
6283
6284/*
6285 * Issue a global reclaim complete.
6286 */
6287static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6288{
6289 struct nfs4_reclaim_complete_data *calldata;
6290 struct rpc_task *task;
6291 struct rpc_message msg = {
6292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6293 };
6294 struct rpc_task_setup task_setup_data = {
6295 .rpc_client = clp->cl_rpcclient,
6296 .rpc_message = &msg,
6297 .callback_ops = &nfs4_reclaim_complete_call_ops,
6298 .flags = RPC_TASK_ASYNC,
6299 };
6300 int status = -ENOMEM;
6301
6302 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006303 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006304 if (calldata == NULL)
6305 goto out;
6306 calldata->clp = clp;
6307 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006308
Trond Myklebust9d12b212012-01-17 22:04:25 -05006309 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006310 msg.rpc_argp = &calldata->arg;
6311 msg.rpc_resp = &calldata->res;
6312 task_setup_data.callback_data = calldata;
6313 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006314 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006315 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006316 goto out;
6317 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006318 status = nfs4_wait_for_completion_rpc_task(task);
6319 if (status == 0)
6320 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006321 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006322 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006323out:
6324 dprintk("<-- %s status=%d\n", __func__, status);
6325 return status;
6326}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006327
6328static void
6329nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6330{
6331 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006332 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006333
6334 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006335 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6336 * right now covering the LAYOUTGET we are about to send.
6337 * However, that is not so catastrophic, and there seems
6338 * to be no way to prevent it completely.
6339 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006340 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006341 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006342 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006343 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6344 NFS_I(lgp->args.inode)->layout,
6345 lgp->args.ctx->state)) {
6346 rpc_exit(task, NFS4_OK);
6347 return;
6348 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006349 rpc_call_start(task);
6350}
6351
6352static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6353{
6354 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006355 struct inode *inode = lgp->args.inode;
6356 struct nfs_server *server = NFS_SERVER(inode);
6357 struct pnfs_layout_hdr *lo;
6358 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006359
6360 dprintk("--> %s\n", __func__);
6361
6362 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006363 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006364
6365 switch (task->tk_status) {
6366 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006367 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006368 case -NFS4ERR_LAYOUTTRYLATER:
6369 case -NFS4ERR_RECALLCONFLICT:
6370 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006371 break;
6372 case -NFS4ERR_EXPIRED:
6373 case -NFS4ERR_BAD_STATEID:
6374 spin_lock(&inode->i_lock);
6375 lo = NFS_I(inode)->layout;
6376 if (!lo || list_empty(&lo->plh_segs)) {
6377 spin_unlock(&inode->i_lock);
6378 /* If the open stateid was bad, then recover it. */
6379 state = lgp->args.ctx->state;
6380 } else {
6381 LIST_HEAD(head);
6382
6383 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6384 spin_unlock(&inode->i_lock);
6385 /* Mark the bad layout state as invalid, then
6386 * retry using the open stateid. */
6387 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006388 }
6389 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006390 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6391 rpc_restart_call_prepare(task);
6392out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006393 dprintk("<-- %s\n", __func__);
6394}
6395
Idan Kedar85541162012-08-02 11:47:10 +03006396static size_t max_response_pages(struct nfs_server *server)
6397{
6398 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6399 return nfs_page_array_len(0, max_resp_sz);
6400}
6401
6402static void nfs4_free_pages(struct page **pages, size_t size)
6403{
6404 int i;
6405
6406 if (!pages)
6407 return;
6408
6409 for (i = 0; i < size; i++) {
6410 if (!pages[i])
6411 break;
6412 __free_page(pages[i]);
6413 }
6414 kfree(pages);
6415}
6416
6417static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6418{
6419 struct page **pages;
6420 int i;
6421
6422 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6423 if (!pages) {
6424 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6425 return NULL;
6426 }
6427
6428 for (i = 0; i < size; i++) {
6429 pages[i] = alloc_page(gfp_flags);
6430 if (!pages[i]) {
6431 dprintk("%s: failed to allocate page\n", __func__);
6432 nfs4_free_pages(pages, size);
6433 return NULL;
6434 }
6435 }
6436
6437 return pages;
6438}
6439
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006440static void nfs4_layoutget_release(void *calldata)
6441{
6442 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006443 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6444 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006445
6446 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006447 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006448 put_nfs_open_context(lgp->args.ctx);
6449 kfree(calldata);
6450 dprintk("<-- %s\n", __func__);
6451}
6452
6453static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6454 .rpc_call_prepare = nfs4_layoutget_prepare,
6455 .rpc_call_done = nfs4_layoutget_done,
6456 .rpc_release = nfs4_layoutget_release,
6457};
6458
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006459struct pnfs_layout_segment *
6460nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006461{
6462 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006463 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006464 struct rpc_task *task;
6465 struct rpc_message msg = {
6466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6467 .rpc_argp = &lgp->args,
6468 .rpc_resp = &lgp->res,
6469 };
6470 struct rpc_task_setup task_setup_data = {
6471 .rpc_client = server->client,
6472 .rpc_message = &msg,
6473 .callback_ops = &nfs4_layoutget_call_ops,
6474 .callback_data = lgp,
6475 .flags = RPC_TASK_ASYNC,
6476 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006477 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006478 int status = 0;
6479
6480 dprintk("--> %s\n", __func__);
6481
Idan Kedar85541162012-08-02 11:47:10 +03006482 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6483 if (!lgp->args.layout.pages) {
6484 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006485 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006486 }
6487 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6488
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006489 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006490 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006491 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006492 task = rpc_run_task(&task_setup_data);
6493 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006494 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006495 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006496 if (status == 0)
6497 status = task->tk_status;
6498 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006499 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006500 rpc_put_task(task);
6501 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006502 if (status)
6503 return ERR_PTR(status);
6504 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006505}
6506
Benny Halevycbe82602011-05-22 19:52:37 +03006507static void
6508nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6509{
6510 struct nfs4_layoutreturn *lrp = calldata;
6511
6512 dprintk("--> %s\n", __func__);
6513 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006514 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006515 return;
6516 rpc_call_start(task);
6517}
6518
6519static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6520{
6521 struct nfs4_layoutreturn *lrp = calldata;
6522 struct nfs_server *server;
6523
6524 dprintk("--> %s\n", __func__);
6525
6526 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6527 return;
6528
6529 server = NFS_SERVER(lrp->args.inode);
6530 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006531 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006532 return;
6533 }
Benny Halevycbe82602011-05-22 19:52:37 +03006534 dprintk("<-- %s\n", __func__);
6535}
6536
6537static void nfs4_layoutreturn_release(void *calldata)
6538{
6539 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006540 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006541
6542 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006543 spin_lock(&lo->plh_inode->i_lock);
6544 if (lrp->res.lrs_present)
6545 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6546 lo->plh_block_lgets--;
6547 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006548 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006549 kfree(calldata);
6550 dprintk("<-- %s\n", __func__);
6551}
6552
6553static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6554 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6555 .rpc_call_done = nfs4_layoutreturn_done,
6556 .rpc_release = nfs4_layoutreturn_release,
6557};
6558
6559int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6560{
6561 struct rpc_task *task;
6562 struct rpc_message msg = {
6563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6564 .rpc_argp = &lrp->args,
6565 .rpc_resp = &lrp->res,
6566 };
6567 struct rpc_task_setup task_setup_data = {
6568 .rpc_client = lrp->clp->cl_rpcclient,
6569 .rpc_message = &msg,
6570 .callback_ops = &nfs4_layoutreturn_call_ops,
6571 .callback_data = lrp,
6572 };
6573 int status;
6574
6575 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006576 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006577 task = rpc_run_task(&task_setup_data);
6578 if (IS_ERR(task))
6579 return PTR_ERR(task);
6580 status = task->tk_status;
6581 dprintk("<-- %s status=%d\n", __func__, status);
6582 rpc_put_task(task);
6583 return status;
6584}
6585
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006586/*
6587 * Retrieve the list of Data Server devices from the MDS.
6588 */
6589static int _nfs4_getdevicelist(struct nfs_server *server,
6590 const struct nfs_fh *fh,
6591 struct pnfs_devicelist *devlist)
6592{
6593 struct nfs4_getdevicelist_args args = {
6594 .fh = fh,
6595 .layoutclass = server->pnfs_curr_ld->id,
6596 };
6597 struct nfs4_getdevicelist_res res = {
6598 .devlist = devlist,
6599 };
6600 struct rpc_message msg = {
6601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6602 .rpc_argp = &args,
6603 .rpc_resp = &res,
6604 };
6605 int status;
6606
6607 dprintk("--> %s\n", __func__);
6608 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6609 &res.seq_res, 0);
6610 dprintk("<-- %s status=%d\n", __func__, status);
6611 return status;
6612}
6613
6614int nfs4_proc_getdevicelist(struct nfs_server *server,
6615 const struct nfs_fh *fh,
6616 struct pnfs_devicelist *devlist)
6617{
6618 struct nfs4_exception exception = { };
6619 int err;
6620
6621 do {
6622 err = nfs4_handle_exception(server,
6623 _nfs4_getdevicelist(server, fh, devlist),
6624 &exception);
6625 } while (exception.retry);
6626
6627 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6628 err, devlist->num_devs);
6629
6630 return err;
6631}
6632EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6633
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006634static int
6635_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6636{
6637 struct nfs4_getdeviceinfo_args args = {
6638 .pdev = pdev,
6639 };
6640 struct nfs4_getdeviceinfo_res res = {
6641 .pdev = pdev,
6642 };
6643 struct rpc_message msg = {
6644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6645 .rpc_argp = &args,
6646 .rpc_resp = &res,
6647 };
6648 int status;
6649
6650 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006651 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006652 dprintk("<-- %s status=%d\n", __func__, status);
6653
6654 return status;
6655}
6656
6657int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6658{
6659 struct nfs4_exception exception = { };
6660 int err;
6661
6662 do {
6663 err = nfs4_handle_exception(server,
6664 _nfs4_proc_getdeviceinfo(server, pdev),
6665 &exception);
6666 } while (exception.retry);
6667 return err;
6668}
6669EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6670
Andy Adamson863a3c62011-03-23 13:27:54 +00006671static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6672{
6673 struct nfs4_layoutcommit_data *data = calldata;
6674 struct nfs_server *server = NFS_SERVER(data->args.inode);
6675
6676 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006677 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006678 return;
6679 rpc_call_start(task);
6680}
6681
6682static void
6683nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6684{
6685 struct nfs4_layoutcommit_data *data = calldata;
6686 struct nfs_server *server = NFS_SERVER(data->args.inode);
6687
6688 if (!nfs4_sequence_done(task, &data->res.seq_res))
6689 return;
6690
6691 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006692 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6693 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6694 case -NFS4ERR_BADLAYOUT: /* no layout */
6695 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006696 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006697 break;
6698 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006699 nfs_post_op_update_inode_force_wcc(data->args.inode,
6700 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006701 break;
6702 default:
6703 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6704 rpc_restart_call_prepare(task);
6705 return;
6706 }
6707 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006708}
6709
6710static void nfs4_layoutcommit_release(void *calldata)
6711{
6712 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006713 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006714 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006715
Andy Adamsondb29c082011-07-30 20:52:38 -04006716 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006717 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006718 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6719 list_del_init(&lseg->pls_lc_list);
6720 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6721 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006722 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006723 }
Peng Tao92407e72011-10-23 20:21:17 -07006724
6725 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6726 smp_mb__after_clear_bit();
6727 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6728
Andy Adamson863a3c62011-03-23 13:27:54 +00006729 put_rpccred(data->cred);
6730 kfree(data);
6731}
6732
6733static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6734 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6735 .rpc_call_done = nfs4_layoutcommit_done,
6736 .rpc_release = nfs4_layoutcommit_release,
6737};
6738
6739int
Andy Adamsonef311532011-03-12 02:58:10 -05006740nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006741{
6742 struct rpc_message msg = {
6743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6744 .rpc_argp = &data->args,
6745 .rpc_resp = &data->res,
6746 .rpc_cred = data->cred,
6747 };
6748 struct rpc_task_setup task_setup_data = {
6749 .task = &data->task,
6750 .rpc_client = NFS_CLIENT(data->args.inode),
6751 .rpc_message = &msg,
6752 .callback_ops = &nfs4_layoutcommit_ops,
6753 .callback_data = data,
6754 .flags = RPC_TASK_ASYNC,
6755 };
6756 struct rpc_task *task;
6757 int status = 0;
6758
6759 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6760 "lbw: %llu inode %lu\n",
6761 data->task.tk_pid, sync,
6762 data->args.lastbytewritten,
6763 data->args.inode->i_ino);
6764
Trond Myklebust9d12b212012-01-17 22:04:25 -05006765 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006766 task = rpc_run_task(&task_setup_data);
6767 if (IS_ERR(task))
6768 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006769 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006770 goto out;
6771 status = nfs4_wait_for_completion_rpc_task(task);
6772 if (status != 0)
6773 goto out;
6774 status = task->tk_status;
6775out:
6776 dprintk("%s: status %d\n", __func__, status);
6777 rpc_put_task(task);
6778 return status;
6779}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006780
6781static int
6782_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6783 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6784{
6785 struct nfs41_secinfo_no_name_args args = {
6786 .style = SECINFO_STYLE_CURRENT_FH,
6787 };
6788 struct nfs4_secinfo_res res = {
6789 .flavors = flavors,
6790 };
6791 struct rpc_message msg = {
6792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6793 .rpc_argp = &args,
6794 .rpc_resp = &res,
6795 };
6796 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6797}
6798
6799static int
6800nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6801 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6802{
6803 struct nfs4_exception exception = { };
6804 int err;
6805 do {
6806 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6807 switch (err) {
6808 case 0:
6809 case -NFS4ERR_WRONGSEC:
6810 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006811 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006812 default:
6813 err = nfs4_handle_exception(server, err, &exception);
6814 }
6815 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006816out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006817 return err;
6818}
6819
6820static int
6821nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6822 struct nfs_fsinfo *info)
6823{
6824 int err;
6825 struct page *page;
6826 rpc_authflavor_t flavor;
6827 struct nfs4_secinfo_flavors *flavors;
6828
6829 page = alloc_page(GFP_KERNEL);
6830 if (!page) {
6831 err = -ENOMEM;
6832 goto out;
6833 }
6834
6835 flavors = page_address(page);
6836 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6837
6838 /*
6839 * Fall back on "guess and check" method if
6840 * the server doesn't support SECINFO_NO_NAME
6841 */
6842 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6843 err = nfs4_find_root_sec(server, fhandle, info);
6844 goto out_freepage;
6845 }
6846 if (err)
6847 goto out_freepage;
6848
6849 flavor = nfs_find_best_sec(flavors);
6850 if (err == 0)
6851 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6852
6853out_freepage:
6854 put_page(page);
6855 if (err == -EACCES)
6856 return -EPERM;
6857out:
6858 return err;
6859}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006860
6861static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006862{
6863 int status;
6864 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006865 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006866 };
6867 struct nfs41_test_stateid_res res;
6868 struct rpc_message msg = {
6869 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6870 .rpc_argp = &args,
6871 .rpc_resp = &res,
6872 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006873
Chuck Lever38527b12012-07-11 16:30:23 -04006874 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006875 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006876 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006877 if (status != NFS_OK) {
6878 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006879 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006880 }
6881 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006882 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006883}
6884
Chuck Lever38527b12012-07-11 16:30:23 -04006885/**
6886 * nfs41_test_stateid - perform a TEST_STATEID operation
6887 *
6888 * @server: server / transport on which to perform the operation
6889 * @stateid: state ID to test
6890 *
6891 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6892 * Otherwise a negative NFS4ERR value is returned if the operation
6893 * failed or the state ID is not currently valid.
6894 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006895static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006896{
6897 struct nfs4_exception exception = { };
6898 int err;
6899 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006900 err = _nfs41_test_stateid(server, stateid);
6901 if (err != -NFS4ERR_DELAY)
6902 break;
6903 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006904 } while (exception.retry);
6905 return err;
6906}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006907
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006908static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006909{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006910 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006911 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006912 };
6913 struct nfs41_free_stateid_res res;
6914 struct rpc_message msg = {
6915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6916 .rpc_argp = &args,
6917 .rpc_resp = &res,
6918 };
Chuck Lever38527b12012-07-11 16:30:23 -04006919 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006920
Chuck Lever38527b12012-07-11 16:30:23 -04006921 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006922 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006923 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006924 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006925 dprintk("NFS reply free_stateid: %d\n", status);
6926 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006927}
6928
Chuck Lever38527b12012-07-11 16:30:23 -04006929/**
6930 * nfs41_free_stateid - perform a FREE_STATEID operation
6931 *
6932 * @server: server / transport on which to perform the operation
6933 * @stateid: state ID to release
6934 *
6935 * Returns NFS_OK if the server freed "stateid". Otherwise a
6936 * negative NFS4ERR value is returned.
6937 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006938static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006939{
6940 struct nfs4_exception exception = { };
6941 int err;
6942 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006943 err = _nfs4_free_stateid(server, stateid);
6944 if (err != -NFS4ERR_DELAY)
6945 break;
6946 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006947 } while (exception.retry);
6948 return err;
6949}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006950
6951static bool nfs41_match_stateid(const nfs4_stateid *s1,
6952 const nfs4_stateid *s2)
6953{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006954 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006955 return false;
6956
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006957 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006958 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006959 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006960 return true;
6961
6962 return false;
6963}
6964
Andy Adamson557134a2009-04-01 09:21:53 -04006965#endif /* CONFIG_NFS_V4_1 */
6966
Trond Myklebust36281ca2012-03-04 18:13:56 -05006967static bool nfs4_match_stateid(const nfs4_stateid *s1,
6968 const nfs4_stateid *s2)
6969{
Trond Myklebustf597c532012-03-04 18:13:56 -05006970 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006971}
6972
6973
Trond Myklebust17280172012-03-11 13:11:00 -04006974static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006975 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006976 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 .recover_open = nfs4_open_reclaim,
6978 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006979 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006980 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006981 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982};
6983
Andy Adamson591d71c2009-04-01 09:22:47 -04006984#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006985static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006986 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6987 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6988 .recover_open = nfs4_open_reclaim,
6989 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006990 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006991 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006992 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006993 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006994};
6995#endif /* CONFIG_NFS_V4_1 */
6996
Trond Myklebust17280172012-03-11 13:11:00 -04006997static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006998 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006999 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 .recover_open = nfs4_open_expired,
7001 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007002 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007003 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004};
7005
Andy Adamson591d71c2009-04-01 09:22:47 -04007006#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007007static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007008 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7009 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007010 .recover_open = nfs41_open_expired,
7011 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007012 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007013 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007014};
7015#endif /* CONFIG_NFS_V4_1 */
7016
Trond Myklebust17280172012-03-11 13:11:00 -04007017static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007018 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007019 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007020 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007021};
7022
7023#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007024static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007025 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007026 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007027 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007028};
7029#endif
7030
Trond Myklebust97dc1352010-06-16 09:52:26 -04007031static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7032 .minor_version = 0,
7033 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007034 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007035 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007036 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7037 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7038 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007039};
7040
7041#if defined(CONFIG_NFS_V4_1)
7042static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7043 .minor_version = 1,
7044 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007045 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007046 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007047 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7048 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7049 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007050};
7051#endif
7052
Trond Myklebust97dc1352010-06-16 09:52:26 -04007053const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7054 [0] = &nfs_v4_0_minor_ops,
7055#if defined(CONFIG_NFS_V4_1)
7056 [1] = &nfs_v4_1_minor_ops,
7057#endif
7058};
7059
Bryan Schumaker73a79702012-07-16 16:39:12 -04007060const struct inode_operations nfs4_dir_inode_operations = {
7061 .create = nfs_create,
7062 .lookup = nfs_lookup,
7063 .atomic_open = nfs_atomic_open,
7064 .link = nfs_link,
7065 .unlink = nfs_unlink,
7066 .symlink = nfs_symlink,
7067 .mkdir = nfs_mkdir,
7068 .rmdir = nfs_rmdir,
7069 .mknod = nfs_mknod,
7070 .rename = nfs_rename,
7071 .permission = nfs_permission,
7072 .getattr = nfs_getattr,
7073 .setattr = nfs_setattr,
7074 .getxattr = generic_getxattr,
7075 .setxattr = generic_setxattr,
7076 .listxattr = generic_listxattr,
7077 .removexattr = generic_removexattr,
7078};
7079
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007080static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007081 .permission = nfs_permission,
7082 .getattr = nfs_getattr,
7083 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007084 .getxattr = generic_getxattr,
7085 .setxattr = generic_setxattr,
7086 .listxattr = generic_listxattr,
7087 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007088};
7089
David Howells509de812006-08-22 20:06:11 -04007090const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091 .version = 4, /* protocol version */
7092 .dentry_ops = &nfs4_dentry_operations,
7093 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007094 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007095 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007097 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007098 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 .getattr = nfs4_proc_getattr,
7100 .setattr = nfs4_proc_setattr,
7101 .lookup = nfs4_proc_lookup,
7102 .access = nfs4_proc_access,
7103 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 .create = nfs4_proc_create,
7105 .remove = nfs4_proc_remove,
7106 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007107 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 .unlink_done = nfs4_proc_unlink_done,
7109 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007110 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007111 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007112 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 .link = nfs4_proc_link,
7114 .symlink = nfs4_proc_symlink,
7115 .mkdir = nfs4_proc_mkdir,
7116 .rmdir = nfs4_proc_remove,
7117 .readdir = nfs4_proc_readdir,
7118 .mknod = nfs4_proc_mknod,
7119 .statfs = nfs4_proc_statfs,
7120 .fsinfo = nfs4_proc_fsinfo,
7121 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007122 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 .decode_dirent = nfs4_decode_dirent,
7124 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007125 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007126 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007127 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007129 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007130 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007131 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007133 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007134 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007136 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007137 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007138 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007139 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007140 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007141 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007142 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04007143 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007144 .create_server = nfs4_create_server,
7145 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146};
7147
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007148static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7149 .prefix = XATTR_NAME_NFSV4_ACL,
7150 .list = nfs4_xattr_list_nfs4_acl,
7151 .get = nfs4_xattr_get_nfs4_acl,
7152 .set = nfs4_xattr_set_nfs4_acl,
7153};
7154
7155const struct xattr_handler *nfs4_xattr_handlers[] = {
7156 &nfs4_xattr_nfs4_acl_handler,
7157 NULL
7158};
7159
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160/*
7161 * Local variables:
7162 * c-basic-offset: 8
7163 * End:
7164 */