blob: d5fbf1f49d5f0ed6534fa2792aefbc1e5a4f3e4b [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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-06-16 09:52:26 -04001572 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001573 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001574 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001575 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001576 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001577 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001578unlock_no_action:
1579 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001580out_no_action:
1581 task->tk_action = NULL;
1582
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001583}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001585static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1586{
1587 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1588 nfs4_open_prepare(task, calldata);
1589}
1590
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001591static void nfs4_open_done(struct rpc_task *task, void *calldata)
1592{
1593 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001595 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001596
Trond Myklebust14516c32010-07-31 14:29:06 -04001597 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1598 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001599
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001600 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001601 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1602 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001603 case S_IFREG:
1604 break;
1605 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001606 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001607 break;
1608 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001609 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001610 break;
1611 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001613 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001614 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001615 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001616 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1617 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001618 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001619 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001620}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001621
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622static void nfs4_open_release(void *calldata)
1623{
1624 struct nfs4_opendata *data = calldata;
1625 struct nfs4_state *state = NULL;
1626
1627 /* If this request hasn't been cancelled, do nothing */
1628 if (data->cancelled == 0)
1629 goto out_free;
1630 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001631 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001632 goto out_free;
1633 /* In case we need an open_confirm, no cleanup! */
1634 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1635 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001636 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001637 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001638 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001640 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641}
1642
1643static const struct rpc_call_ops nfs4_open_ops = {
1644 .rpc_call_prepare = nfs4_open_prepare,
1645 .rpc_call_done = nfs4_open_done,
1646 .rpc_release = nfs4_open_release,
1647};
1648
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001649static const struct rpc_call_ops nfs4_recover_open_ops = {
1650 .rpc_call_prepare = nfs4_recover_open_prepare,
1651 .rpc_call_done = nfs4_open_done,
1652 .rpc_release = nfs4_open_release,
1653};
1654
1655static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001656{
1657 struct inode *dir = data->dir->d_inode;
1658 struct nfs_server *server = NFS_SERVER(dir);
1659 struct nfs_openargs *o_arg = &data->o_arg;
1660 struct nfs_openres *o_res = &data->o_res;
1661 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001662 struct rpc_message msg = {
1663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1664 .rpc_argp = o_arg,
1665 .rpc_resp = o_res,
1666 .rpc_cred = data->owner->so_cred,
1667 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001668 struct rpc_task_setup task_setup_data = {
1669 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001670 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001671 .callback_ops = &nfs4_open_ops,
1672 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001673 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001674 .flags = RPC_TASK_ASYNC,
1675 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 int status;
1677
Trond Myklebust9d12b212012-01-17 22:04:25 -05001678 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001679 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001680 data->rpc_done = 0;
1681 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001682 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001683 if (isrecover)
1684 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001685 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001686 if (IS_ERR(task))
1687 return PTR_ERR(task);
1688 status = nfs4_wait_for_completion_rpc_task(task);
1689 if (status != 0) {
1690 data->cancelled = 1;
1691 smp_wmb();
1692 } else
1693 status = data->rpc_status;
1694 rpc_put_task(task);
1695
1696 return status;
1697}
1698
1699static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1700{
1701 struct inode *dir = data->dir->d_inode;
1702 struct nfs_openres *o_res = &data->o_res;
1703 int status;
1704
1705 status = nfs4_run_open_task(data, 1);
1706 if (status != 0 || !data->rpc_done)
1707 return status;
1708
Trond Myklebust6926afd2012-01-07 13:22:46 -05001709 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1710
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001711 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1712 status = _nfs4_proc_open_confirm(data);
1713 if (status != 0)
1714 return status;
1715 }
1716
1717 return status;
1718}
1719
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001720static int nfs4_opendata_access(struct rpc_cred *cred,
1721 struct nfs4_opendata *opendata,
1722 struct nfs4_state *state, fmode_t fmode)
1723{
1724 struct nfs_access_entry cache;
1725 u32 mask;
1726
1727 /* access call failed or for some reason the server doesn't
1728 * support any access modes -- defer access call until later */
1729 if (opendata->o_res.access_supported == 0)
1730 return 0;
1731
1732 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001733 /* don't check MAY_WRITE - a newly created file may not have
1734 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001735 if (fmode & FMODE_READ)
1736 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001737 if (fmode & FMODE_EXEC)
1738 mask |= MAY_EXEC;
1739
1740 cache.cred = cred;
1741 cache.jiffies = jiffies;
1742 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1743 nfs_access_add_cache(state->inode, &cache);
1744
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001745 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001746 return 0;
1747
1748 /* even though OPEN succeeded, access is denied. Close the file */
1749 nfs4_close_state(state, fmode);
1750 return -NFS4ERR_ACCESS;
1751}
1752
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001753/*
1754 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1755 */
1756static int _nfs4_proc_open(struct nfs4_opendata *data)
1757{
1758 struct inode *dir = data->dir->d_inode;
1759 struct nfs_server *server = NFS_SERVER(dir);
1760 struct nfs_openargs *o_arg = &data->o_arg;
1761 struct nfs_openres *o_res = &data->o_res;
1762 int status;
1763
1764 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001765 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001766 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001767 if (status != 0) {
1768 if (status == -NFS4ERR_BADNAME &&
1769 !(o_arg->open_flags & O_CREAT))
1770 return -ENOENT;
1771 return status;
1772 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773
Trond Myklebust6926afd2012-01-07 13:22:46 -05001774 nfs_fattr_map_and_free_names(server, &data->f_attr);
1775
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001776 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001777 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001778 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1779 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001781 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001786 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Andy Adamsond83217c2011-03-01 01:34:17 +00001790static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001791{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001792 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001793 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001794
Trond Myklebusta78cb572009-08-09 15:06:19 -04001795 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001796 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001797 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001798 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001799 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1800 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001801 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001802 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001803 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001804 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001805 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001806}
1807
Andy Adamsond83217c2011-03-01 01:34:17 +00001808static int nfs4_recover_expired_lease(struct nfs_server *server)
1809{
1810 return nfs4_client_recover_expired_lease(server->nfs_client);
1811}
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813/*
1814 * OPEN_EXPIRED:
1815 * reclaim state on the server after a network partition.
1816 * Assumes caller holds the appropriate lock
1817 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001818static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001820 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001821 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001823 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1824 if (IS_ERR(opendata))
1825 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001826 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001827 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001828 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001829 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001830 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001833static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001834{
Trond Myklebust539cd032007-06-05 11:46:42 -04001835 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001836 struct nfs4_exception exception = { };
1837 int err;
1838
1839 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001840 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001841 switch (err) {
1842 default:
1843 goto out;
1844 case -NFS4ERR_GRACE:
1845 case -NFS4ERR_DELAY:
1846 nfs4_handle_exception(server, err, &exception);
1847 err = 0;
1848 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001849 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001850out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001851 return err;
1852}
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1855{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001857 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 ctx = nfs4_state_find_open_context(state);
1860 if (IS_ERR(ctx))
1861 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001862 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001863 put_nfs_open_context(ctx);
1864 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001867#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001868static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001869{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001870 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001871 nfs4_stateid *stateid = &state->stateid;
1872 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001873
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001874 /* If a state reset has been done, test_stateid is unneeded */
1875 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1876 return;
1877
1878 status = nfs41_test_stateid(server, stateid);
1879 if (status != NFS_OK) {
1880 /* Free the stateid unless the server explicitly
1881 * informs us the stateid is unrecognized. */
1882 if (status != -NFS4ERR_BAD_STATEID)
1883 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001884 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001885
Bryan Schumaker69388672012-09-26 15:25:52 -04001886 write_seqlock(&state->seqlock);
1887 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1888 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001889 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1890 }
1891}
1892
1893/**
1894 * nfs41_check_open_stateid - possibly free an open stateid
1895 *
1896 * @state: NFSv4 state for an inode
1897 *
1898 * Returns NFS_OK if recovery for this stateid is now finished.
1899 * Otherwise a negative NFS4ERR value is returned.
1900 */
1901static int nfs41_check_open_stateid(struct nfs4_state *state)
1902{
1903 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001904 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001905 int status;
1906
1907 /* If a state reset has been done, test_stateid is unneeded */
1908 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1909 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1910 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1911 return -NFS4ERR_BAD_STATEID;
1912
1913 status = nfs41_test_stateid(server, stateid);
1914 if (status != NFS_OK) {
1915 /* Free the stateid unless the server explicitly
1916 * informs us the stateid is unrecognized. */
1917 if (status != -NFS4ERR_BAD_STATEID)
1918 nfs41_free_stateid(server, stateid);
1919
1920 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1921 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1922 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001923 }
1924 return status;
1925}
1926
1927static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1928{
Chuck Levereb64cf92012-07-11 16:30:05 -04001929 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001930
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001931 nfs41_clear_delegation_stateid(state);
1932 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001933 if (status != NFS_OK)
1934 status = nfs4_open_expired(sp, state);
1935 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001936}
1937#endif
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001940 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1941 * fields corresponding to attributes that were used to store the verifier.
1942 * Make sure we clobber those fields in the later setattr call
1943 */
1944static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1945{
1946 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1947 !(sattr->ia_valid & ATTR_ATIME_SET))
1948 sattr->ia_valid |= ATTR_ATIME;
1949
1950 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1951 !(sattr->ia_valid & ATTR_MTIME_SET))
1952 sattr->ia_valid |= ATTR_MTIME;
1953}
1954
1955/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001956 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 */
Andy Adamson82be4172012-05-23 05:02:35 -04001958static int _nfs4_do_open(struct inode *dir,
1959 struct dentry *dentry,
1960 fmode_t fmode,
1961 int flags,
1962 struct iattr *sattr,
1963 struct rpc_cred *cred,
1964 struct nfs4_state **res,
1965 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 struct nfs4_state_owner *sp;
1968 struct nfs4_state *state = NULL;
1969 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001970 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001971 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001975 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1976 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1978 goto out_err;
1979 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001980 status = nfs4_recover_expired_lease(server);
1981 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001982 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001983 if (dentry->d_inode != NULL)
1984 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001985 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001986 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001987 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001988 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Andy Adamson82be4172012-05-23 05:02:35 -04001990 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1991 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1992 if (!opendata->f_attr.mdsthreshold)
1993 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001994 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001995 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001996 if (dentry->d_inode != NULL)
1997 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001998
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001999 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002001 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002003 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002004 status = PTR_ERR(state);
2005 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002006 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002007 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002008 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002009
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002010 status = nfs4_opendata_access(cred, opendata, state, fmode);
2011 if (status != 0)
2012 goto err_opendata_put;
2013
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002014 if (opendata->o_arg.open_flags & O_EXCL) {
2015 nfs4_exclusive_attrset(opendata, sattr);
2016
2017 nfs_fattr_init(opendata->o_res.f_attr);
2018 status = nfs4_do_setattr(state->inode, cred,
2019 opendata->o_res.f_attr, sattr,
2020 state);
2021 if (status == 0)
2022 nfs_setattr_update_inode(state->inode, sattr);
2023 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2024 }
Andy Adamson82be4172012-05-23 05:02:35 -04002025
2026 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2027 *ctx_th = opendata->f_attr.mdsthreshold;
2028 else
2029 kfree(opendata->f_attr.mdsthreshold);
2030 opendata->f_attr.mdsthreshold = NULL;
2031
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002032 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 *res = state;
2035 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002036err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002037 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002038 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002039err_put_state_owner:
2040 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 *res = NULL;
2043 return status;
2044}
2045
2046
Andy Adamson82be4172012-05-23 05:02:35 -04002047static struct nfs4_state *nfs4_do_open(struct inode *dir,
2048 struct dentry *dentry,
2049 fmode_t fmode,
2050 int flags,
2051 struct iattr *sattr,
2052 struct rpc_cred *cred,
2053 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 struct nfs4_exception exception = { };
2056 struct nfs4_state *res;
2057 int status;
2058
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002059 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002061 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2062 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (status == 0)
2064 break;
2065 /* NOTE: BAD_SEQID means the server and client disagree about the
2066 * book-keeping w.r.t. state-changing operations
2067 * (OPEN/CLOSE/LOCK/LOCKU...)
2068 * It is actually a sign of a bug on the client or on the server.
2069 *
2070 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002071 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 * have unhashed the old state_owner for us, and that we can
2073 * therefore safely retry using a new one. We should still warn
2074 * the user though...
2075 */
2076 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002077 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002078 " returned a bad sequence-id error!\n",
2079 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 exception.retry = 1;
2081 continue;
2082 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002083 /*
2084 * BAD_STATEID on OPEN means that the server cancelled our
2085 * state before it received the OPEN_CONFIRM.
2086 * Recover by retrying the request as per the discussion
2087 * on Page 181 of RFC3530.
2088 */
2089 if (status == -NFS4ERR_BAD_STATEID) {
2090 exception.retry = 1;
2091 continue;
2092 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002093 if (status == -EAGAIN) {
2094 /* We must have found a delegation */
2095 exception.retry = 1;
2096 continue;
2097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2099 status, &exception));
2100 } while (exception.retry);
2101 return res;
2102}
2103
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002104static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2105 struct nfs_fattr *fattr, struct iattr *sattr,
2106 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002108 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002110 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 .iap = sattr,
2112 .server = server,
2113 .bitmask = server->attr_bitmask,
2114 };
2115 struct nfs_setattrres res = {
2116 .fattr = fattr,
2117 .server = server,
2118 };
2119 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2121 .rpc_argp = &arg,
2122 .rpc_resp = &res,
2123 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002125 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002126 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Trond Myklebust0e574af2005-10-27 22:12:38 -04002128 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Trond Myklebust4fc87962012-03-08 17:42:01 -05002130 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002131 struct nfs_lockowner lockowner = {
2132 .l_owner = current->files,
2133 .l_pid = current->tgid,
2134 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002135 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002136 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002137 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2138 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002139 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002140 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002141 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Bryan Schumaker7c513052011-03-24 17:12:24 +00002143 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002144 if (status == 0 && state != NULL)
2145 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002146 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002149static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2150 struct nfs_fattr *fattr, struct iattr *sattr,
2151 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002153 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002154 struct nfs4_exception exception = {
2155 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002156 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002157 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 int err;
2159 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002160 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2161 switch (err) {
2162 case -NFS4ERR_OPENMODE:
2163 if (state && !(state->state & FMODE_WRITE)) {
2164 err = -EBADF;
2165 if (sattr->ia_valid & ATTR_OPEN)
2166 err = -EACCES;
2167 goto out;
2168 }
2169 }
2170 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002172out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return err;
2174}
2175
2176struct nfs4_closedata {
2177 struct inode *inode;
2178 struct nfs4_state *state;
2179 struct nfs_closeargs arg;
2180 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002181 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002182 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002183 bool roc;
2184 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185};
2186
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002187static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002188{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002189 struct nfs4_closedata *calldata = data;
2190 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002191 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002192
Fred Isamanf7e89172011-01-06 11:36:32 +00002193 if (calldata->roc)
2194 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002195 nfs4_put_open_state(calldata->state);
2196 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002197 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002198 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002199 kfree(calldata);
2200}
2201
Trond Myklebust88069f72009-12-08 08:33:16 -05002202static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2203 fmode_t fmode)
2204{
2205 spin_lock(&state->owner->so_lock);
2206 if (!(fmode & FMODE_READ))
2207 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2208 if (!(fmode & FMODE_WRITE))
2209 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2210 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2211 spin_unlock(&state->owner->so_lock);
2212}
2213
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002214static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002216 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct nfs_server *server = NFS_SERVER(calldata->inode);
2219
Chuck Levera3ca5652012-03-01 17:00:40 -05002220 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002221 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2222 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /* hmm. we are done with the inode, and in the process of freeing
2224 * the state_owner. we keep this around to process errors
2225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 switch (task->tk_status) {
2227 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002228 if (calldata->roc)
2229 pnfs_roc_set_barrier(state->inode,
2230 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002231 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002232 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002233 nfs4_close_clear_stateid_flags(state,
2234 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 break;
2236 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002237 case -NFS4ERR_OLD_STATEID:
2238 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002240 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002243 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2244 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002246 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002247 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002248 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002251static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002253 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002254 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002255 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002256 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002257
Chuck Levera3ca5652012-03-01 17:00:40 -05002258 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002259 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002260 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002261
Trond Myklebust88069f72009-12-08 08:33:16 -05002262 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2263 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002264 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002265 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002266 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002267 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002268 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2269 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2270 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002271 }
2272 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002273 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2274 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2275 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002276 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002277 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002278 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002279
2280 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002281 /* Note: exit _without_ calling nfs4_close_done */
2282 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002283 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002284 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002285
Fred Isamanf7e89172011-01-06 11:36:32 +00002286 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002287 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002288 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002289 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002290 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002291 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002292
Trond Myklebust516a6af2005-10-27 22:12:41 -04002293 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002294 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002295 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002296 &calldata->arg.seq_args,
2297 &calldata->res.seq_res,
2298 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002299 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002300 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002301out:
2302 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002305static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002306 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002307 .rpc_call_done = nfs4_close_done,
2308 .rpc_release = nfs4_free_closedata,
2309};
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311/*
2312 * It is possible for data to be read/written from a mem-mapped file
2313 * after the sys_close call (which hits the vfs layer as a flush).
2314 * This means that we can't safely call nfsv4 close on a file until
2315 * the inode is cleared. This in turn means that we are not good
2316 * NFSv4 citizens - we do not indicate to the server to update the file's
2317 * share state even when we are done with one of the three share
2318 * stateid's in the inode.
2319 *
2320 * NOTE: Caller must be holding the sp->so_owner semaphore!
2321 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002322int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002324 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002326 struct nfs4_state_owner *sp = state->owner;
2327 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002328 struct rpc_message msg = {
2329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2330 .rpc_cred = state->owner->so_cred,
2331 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002332 struct rpc_task_setup task_setup_data = {
2333 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002334 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002335 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002336 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002337 .flags = RPC_TASK_ASYNC,
2338 };
Trond Myklebust95121352005-10-18 14:20:12 -07002339 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002341 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002343 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002344 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002345 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002347 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002348 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002350 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002351 if (calldata->arg.seqid == NULL)
2352 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002353 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002354 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002355 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002356 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002357 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002358 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002359 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002360
Trond Myklebust1174dd12010-12-21 10:52:24 -05002361 msg.rpc_argp = &calldata->arg;
2362 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002363 task_setup_data.callback_data = calldata;
2364 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002365 if (IS_ERR(task))
2366 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002367 status = 0;
2368 if (wait)
2369 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002370 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002371 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002372out_free_calldata:
2373 kfree(calldata);
2374out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002375 nfs4_put_open_state(state);
2376 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002377 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
Trond Myklebust2b484292010-09-17 10:56:51 -04002380static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002381nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 struct nfs4_state *state;
2384
Trond Myklebust565277f2007-10-15 18:17:53 -04002385 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002386 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2387 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002388 if (IS_ERR(state))
2389 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002390 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002391 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
Trond Myklebust1185a552009-12-03 15:54:02 -05002394static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002395{
2396 if (ctx->state == NULL)
2397 return;
2398 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002399 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002400 else
Al Viro643168c2011-06-22 18:20:23 -04002401 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2405{
Benny Halevy43652ad2009-04-01 09:21:54 -04002406 struct nfs4_server_caps_arg args = {
2407 .fhandle = fhandle,
2408 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 struct nfs4_server_caps_res res = {};
2410 struct rpc_message msg = {
2411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002412 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 .rpc_resp = &res,
2414 };
2415 int status;
2416
Bryan Schumaker7c513052011-03-24 17:12:24 +00002417 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 if (status == 0) {
2419 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002420 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2421 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2422 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2423 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2424 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2426 server->caps |= NFS_CAP_ACLS;
2427 if (res.has_links != 0)
2428 server->caps |= NFS_CAP_HARDLINKS;
2429 if (res.has_symlinks != 0)
2430 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002431 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2432 server->caps |= NFS_CAP_FILEID;
2433 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2434 server->caps |= NFS_CAP_MODE;
2435 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2436 server->caps |= NFS_CAP_NLINK;
2437 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2438 server->caps |= NFS_CAP_OWNER;
2439 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2440 server->caps |= NFS_CAP_OWNER_GROUP;
2441 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2442 server->caps |= NFS_CAP_ATIME;
2443 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2444 server->caps |= NFS_CAP_CTIME;
2445 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2446 server->caps |= NFS_CAP_MTIME;
2447
Trond Myklebusta65318b2009-03-11 14:10:28 -04002448 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2449 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2450 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002452 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return status;
2456}
2457
Trond Myklebust55a97592006-06-09 09:34:19 -04002458int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
2460 struct nfs4_exception exception = { };
2461 int err;
2462 do {
2463 err = nfs4_handle_exception(server,
2464 _nfs4_server_capabilities(server, fhandle),
2465 &exception);
2466 } while (exception.retry);
2467 return err;
2468}
2469
2470static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2471 struct nfs_fsinfo *info)
2472{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 struct nfs4_lookup_root_arg args = {
2474 .bitmask = nfs4_fattr_bitmap,
2475 };
2476 struct nfs4_lookup_res res = {
2477 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002478 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 .fh = fhandle,
2480 };
2481 struct rpc_message msg = {
2482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2483 .rpc_argp = &args,
2484 .rpc_resp = &res,
2485 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002486
Trond Myklebust0e574af2005-10-27 22:12:38 -04002487 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002488 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
2491static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2492 struct nfs_fsinfo *info)
2493{
2494 struct nfs4_exception exception = { };
2495 int err;
2496 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002497 err = _nfs4_lookup_root(server, fhandle, info);
2498 switch (err) {
2499 case 0:
2500 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002501 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002502 default:
2503 err = nfs4_handle_exception(server, err, &exception);
2504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002506out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return err;
2508}
2509
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002510static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2511 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2512{
2513 struct rpc_auth *auth;
2514 int ret;
2515
2516 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002517 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002518 ret = -EIO;
2519 goto out;
2520 }
2521 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002522out:
2523 return ret;
2524}
2525
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002526static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002527 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002529 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002530 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002532 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2533 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002534
2535 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002536 /* AUTH_UNIX is the default flavor if none was specified,
2537 * thus has already been tried. */
2538 if (flav_array[i] == RPC_AUTH_UNIX)
2539 continue;
2540
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002541 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002542 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002543 continue;
2544 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002545 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002546 /*
2547 * -EACCESS could mean that the user doesn't have correct permissions
2548 * to access the mount. It could also mean that we tried to mount
2549 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2550 * existing mount programs don't handle -EACCES very well so it should
2551 * be mapped to -EPERM instead.
2552 */
2553 if (status == -EACCES)
2554 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002555 return status;
2556}
2557
2558/*
2559 * get the file handle for the "/" directory on the server
2560 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002561int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2562 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002563{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002564 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002565 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002566 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2567 /*
2568 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2569 * by nfs4_map_errors() as this function exits.
2570 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002571 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 if (status == 0)
2573 status = nfs4_server_capabilities(server, fhandle);
2574 if (status == 0)
2575 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002576 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
Bryan Schumakerbae36242012-05-10 15:07:31 -04002579static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2580 struct nfs_fsinfo *info)
2581{
2582 int error;
2583 struct nfs_fattr *fattr = info->fattr;
2584
2585 error = nfs4_server_capabilities(server, mntfh);
2586 if (error < 0) {
2587 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2588 return error;
2589 }
2590
2591 error = nfs4_proc_getattr(server, mntfh, fattr);
2592 if (error < 0) {
2593 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2594 return error;
2595 }
2596
2597 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2598 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2599 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2600
2601 return error;
2602}
2603
Manoj Naik6b97fd32006-06-09 09:34:29 -04002604/*
2605 * Get locations and (maybe) other attributes of a referral.
2606 * Note that we'll actually follow the referral later when
2607 * we detect fsid mismatch in inode revalidation
2608 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002609static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2610 const struct qstr *name, struct nfs_fattr *fattr,
2611 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002612{
2613 int status = -ENOMEM;
2614 struct page *page = NULL;
2615 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002616
2617 page = alloc_page(GFP_KERNEL);
2618 if (page == NULL)
2619 goto out;
2620 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2621 if (locations == NULL)
2622 goto out;
2623
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002624 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002625 if (status != 0)
2626 goto out;
2627 /* Make sure server returned a different fsid for the referral */
2628 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002629 dprintk("%s: server did not return a different fsid for"
2630 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002631 status = -EIO;
2632 goto out;
2633 }
Andy Adamson533eb462011-06-13 18:25:56 -04002634 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2635 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002636
Andy Adamson533eb462011-06-13 18:25:56 -04002637 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002638 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002639 memset(fhandle, 0, sizeof(struct nfs_fh));
2640out:
2641 if (page)
2642 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002643 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002644 return status;
2645}
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2648{
2649 struct nfs4_getattr_arg args = {
2650 .fh = fhandle,
2651 .bitmask = server->attr_bitmask,
2652 };
2653 struct nfs4_getattr_res res = {
2654 .fattr = fattr,
2655 .server = server,
2656 };
2657 struct rpc_message msg = {
2658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2659 .rpc_argp = &args,
2660 .rpc_resp = &res,
2661 };
2662
Trond Myklebust0e574af2005-10-27 22:12:38 -04002663 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002664 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
2667static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2668{
2669 struct nfs4_exception exception = { };
2670 int err;
2671 do {
2672 err = nfs4_handle_exception(server,
2673 _nfs4_proc_getattr(server, fhandle, fattr),
2674 &exception);
2675 } while (exception.retry);
2676 return err;
2677}
2678
2679/*
2680 * The file is not closed if it is opened due to the a request to change
2681 * the size of the file. The open call will not be needed once the
2682 * VFS layer lookup-intents are implemented.
2683 *
2684 * Close is called when the inode is destroyed.
2685 * If we haven't opened the file for O_WRONLY, we
2686 * need to in the size_change case to obtain a stateid.
2687 *
2688 * Got race?
2689 * Because OPEN is always done by name in nfsv4, it is
2690 * possible that we opened a different file by the same
2691 * name. We can recognize this race condition, but we
2692 * can't do anything about it besides returning an error.
2693 *
2694 * This will be fixed with VFS changes (lookup-intent).
2695 */
2696static int
2697nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2698 struct iattr *sattr)
2699{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002700 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002701 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002702 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 int status;
2704
Benny Halevy8a1636c2010-07-14 15:43:57 -04002705 if (pnfs_ld_layoutret_on_setattr(inode))
2706 pnfs_return_layout(inode);
2707
Trond Myklebust0e574af2005-10-27 22:12:38 -04002708 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Andy Adamson26699402012-05-30 16:12:24 -04002710 /* Deal with open(O_TRUNC) */
2711 if (sattr->ia_valid & ATTR_OPEN)
2712 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2713
2714 /* Optimization: if the end result is no change, don't RPC */
2715 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2716 return 0;
2717
Trond Myklebustd5308382005-11-04 15:33:38 -05002718 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002719 if (sattr->ia_valid & ATTR_FILE) {
2720 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002721
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002722 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002723 if (ctx) {
2724 cred = ctx->cred;
2725 state = ctx->state;
2726 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002727 }
2728
2729 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002730 if (status == 0)
2731 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return status;
2733}
2734
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002735static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2736 const struct qstr *name, struct nfs_fh *fhandle,
2737 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002738{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002739 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002740 int status;
2741 struct nfs4_lookup_arg args = {
2742 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002743 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002744 .name = name,
2745 };
2746 struct nfs4_lookup_res res = {
2747 .server = server,
2748 .fattr = fattr,
2749 .fh = fhandle,
2750 };
2751 struct rpc_message msg = {
2752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2753 .rpc_argp = &args,
2754 .rpc_resp = &res,
2755 };
2756
2757 nfs_fattr_init(fattr);
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002760 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 dprintk("NFS reply lookup: %d\n", status);
2762 return status;
2763}
2764
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002765static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002766{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002767 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002768 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002769 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2770 fattr->nlink = 2;
2771}
2772
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002773static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2774 struct qstr *name, struct nfs_fh *fhandle,
2775 struct nfs_fattr *fattr)
2776{
2777 struct nfs4_exception exception = { };
2778 struct rpc_clnt *client = *clnt;
2779 int err;
2780 do {
2781 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2782 switch (err) {
2783 case -NFS4ERR_BADNAME:
2784 err = -ENOENT;
2785 goto out;
2786 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002787 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002788 goto out;
2789 case -NFS4ERR_WRONGSEC:
2790 err = -EPERM;
2791 if (client != *clnt)
2792 goto out;
2793
2794 client = nfs4_create_sec_client(client, dir, name);
2795 if (IS_ERR(client))
2796 return PTR_ERR(client);
2797
2798 exception.retry = 1;
2799 break;
2800 default:
2801 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2802 }
2803 } while (exception.retry);
2804
2805out:
2806 if (err == 0)
2807 *clnt = client;
2808 else if (client != *clnt)
2809 rpc_shutdown_client(client);
2810
2811 return err;
2812}
2813
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002814static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002815 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002817 int status;
2818 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002819
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002820 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2821 if (client != NFS_CLIENT(dir)) {
2822 rpc_shutdown_client(client);
2823 nfs_fixup_secinfo_attributes(fattr);
2824 }
2825 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002828struct rpc_clnt *
2829nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2830 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2831{
2832 int status;
2833 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2834
2835 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2836 if (status < 0) {
2837 rpc_shutdown_client(client);
2838 return ERR_PTR(status);
2839 }
2840 return client;
2841}
2842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2844{
Trond Myklebust76b32992007-08-10 17:45:11 -04002845 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 struct nfs4_accessargs args = {
2847 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002848 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002850 struct nfs4_accessres res = {
2851 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002852 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 struct rpc_message msg = {
2854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2855 .rpc_argp = &args,
2856 .rpc_resp = &res,
2857 .rpc_cred = entry->cred,
2858 };
2859 int mode = entry->mask;
2860 int status;
2861
2862 /*
2863 * Determine which access bits we want to ask for...
2864 */
2865 if (mode & MAY_READ)
2866 args.access |= NFS4_ACCESS_READ;
2867 if (S_ISDIR(inode->i_mode)) {
2868 if (mode & MAY_WRITE)
2869 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2870 if (mode & MAY_EXEC)
2871 args.access |= NFS4_ACCESS_LOOKUP;
2872 } else {
2873 if (mode & MAY_WRITE)
2874 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2875 if (mode & MAY_EXEC)
2876 args.access |= NFS4_ACCESS_EXECUTE;
2877 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002878
2879 res.fattr = nfs_alloc_fattr();
2880 if (res.fattr == NULL)
2881 return -ENOMEM;
2882
Bryan Schumaker7c513052011-03-24 17:12:24 +00002883 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002885 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002886 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002888 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return status;
2890}
2891
2892static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2893{
2894 struct nfs4_exception exception = { };
2895 int err;
2896 do {
2897 err = nfs4_handle_exception(NFS_SERVER(inode),
2898 _nfs4_proc_access(inode, entry),
2899 &exception);
2900 } while (exception.retry);
2901 return err;
2902}
2903
2904/*
2905 * TODO: For the time being, we don't try to get any attributes
2906 * along with any of the zero-copy operations READ, READDIR,
2907 * READLINK, WRITE.
2908 *
2909 * In the case of the first three, we want to put the GETATTR
2910 * after the read-type operation -- this is because it is hard
2911 * to predict the length of a GETATTR response in v4, and thus
2912 * align the READ data correctly. This means that the GETATTR
2913 * may end up partially falling into the page cache, and we should
2914 * shift it into the 'tail' of the xdr_buf before processing.
2915 * To do this efficiently, we need to know the total length
2916 * of data received, which doesn't seem to be available outside
2917 * of the RPC layer.
2918 *
2919 * In the case of WRITE, we also want to put the GETATTR after
2920 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002921 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 *
2923 * Both of these changes to the XDR layer would in fact be quite
2924 * minor, but I decided to leave them for a subsequent patch.
2925 */
2926static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2927 unsigned int pgbase, unsigned int pglen)
2928{
2929 struct nfs4_readlink args = {
2930 .fh = NFS_FH(inode),
2931 .pgbase = pgbase,
2932 .pglen = pglen,
2933 .pages = &page,
2934 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002935 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 struct rpc_message msg = {
2937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2938 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002939 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 };
2941
Bryan Schumaker7c513052011-03-24 17:12:24 +00002942 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 -07002943}
2944
2945static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2946 unsigned int pgbase, unsigned int pglen)
2947{
2948 struct nfs4_exception exception = { };
2949 int err;
2950 do {
2951 err = nfs4_handle_exception(NFS_SERVER(inode),
2952 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2953 &exception);
2954 } while (exception.retry);
2955 return err;
2956}
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002959 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961static int
2962nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002963 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002965 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 int status = 0;
2968
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002969 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2970 if (IS_ERR(ctx))
2971 return PTR_ERR(ctx);
2972
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002973 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002974 state = nfs4_do_open(dir, dentry, ctx->mode,
2975 flags, sattr, ctx->cred,
2976 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002977 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (IS_ERR(state)) {
2979 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002982 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002983 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002984 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002986 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return status;
2988}
2989
2990static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2991{
Trond Myklebust16e42952005-10-27 22:12:44 -04002992 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002993 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002995 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002997 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002998 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002999 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3002 .rpc_argp = &args,
3003 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003005 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003006
Bryan Schumaker7c513052011-03-24 17:12:24 +00003007 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003008 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003009 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return status;
3011}
3012
3013static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3014{
3015 struct nfs4_exception exception = { };
3016 int err;
3017 do {
3018 err = nfs4_handle_exception(NFS_SERVER(dir),
3019 _nfs4_proc_remove(dir, name),
3020 &exception);
3021 } while (exception.retry);
3022 return err;
3023}
3024
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003025static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003027 struct nfs_server *server = NFS_SERVER(dir);
3028 struct nfs_removeargs *args = msg->rpc_argp;
3029 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003031 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003033 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034}
3035
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003036static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3037{
3038 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3039 &data->args.seq_args,
3040 &data->res.seq_res,
3041 task))
3042 return;
3043 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003046static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003048 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3049
Trond Myklebust14516c32010-07-31 14:29:06 -04003050 if (!nfs4_sequence_done(task, &res->seq_res))
3051 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003052 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003053 return 0;
3054 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003055 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056}
3057
Jeff Laytond3d41522010-09-17 17:31:57 -04003058static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3059{
3060 struct nfs_server *server = NFS_SERVER(dir);
3061 struct nfs_renameargs *arg = msg->rpc_argp;
3062 struct nfs_renameres *res = msg->rpc_resp;
3063
3064 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003065 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003066 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003067}
3068
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003069static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3070{
3071 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3072 &data->args.seq_args,
3073 &data->res.seq_res,
3074 task))
3075 return;
3076 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003077}
3078
3079static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3080 struct inode *new_dir)
3081{
3082 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3083
3084 if (!nfs4_sequence_done(task, &res->seq_res))
3085 return 0;
3086 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3087 return 0;
3088
3089 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003090 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003091 return 1;
3092}
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3095 struct inode *new_dir, struct qstr *new_name)
3096{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003097 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003098 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 .old_dir = NFS_FH(old_dir),
3100 .new_dir = NFS_FH(new_dir),
3101 .old_name = old_name,
3102 .new_name = new_name,
3103 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003104 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003105 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003106 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 struct rpc_message msg = {
3108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3109 .rpc_argp = &arg,
3110 .rpc_resp = &res,
3111 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003112 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Bryan Schumaker7c513052011-03-24 17:12:24 +00003114 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 if (!status) {
3116 update_changeattr(old_dir, &res.old_cinfo);
3117 update_changeattr(new_dir, &res.new_cinfo);
3118 }
3119 return status;
3120}
3121
3122static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3123 struct inode *new_dir, struct qstr *new_name)
3124{
3125 struct nfs4_exception exception = { };
3126 int err;
3127 do {
3128 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3129 _nfs4_proc_rename(old_dir, old_name,
3130 new_dir, new_name),
3131 &exception);
3132 } while (exception.retry);
3133 return err;
3134}
3135
3136static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3137{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003138 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 struct nfs4_link_arg arg = {
3140 .fh = NFS_FH(inode),
3141 .dir_fh = NFS_FH(dir),
3142 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003143 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003145 struct nfs4_link_res res = {
3146 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003147 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 struct rpc_message msg = {
3149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3150 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003151 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003153 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Trond Myklebust136f2622010-04-16 16:22:49 -04003155 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003156 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003157 goto out;
3158
Bryan Schumaker7c513052011-03-24 17:12:24 +00003159 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003160 if (!status) {
3161 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003162 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003163 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003164out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003165 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return status;
3167}
3168
3169static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3170{
3171 struct nfs4_exception exception = { };
3172 int err;
3173 do {
3174 err = nfs4_handle_exception(NFS_SERVER(inode),
3175 _nfs4_proc_link(inode, dir, name),
3176 &exception);
3177 } while (exception.retry);
3178 return err;
3179}
3180
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003181struct nfs4_createdata {
3182 struct rpc_message msg;
3183 struct nfs4_create_arg arg;
3184 struct nfs4_create_res res;
3185 struct nfs_fh fh;
3186 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003187};
3188
3189static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3190 struct qstr *name, struct iattr *sattr, u32 ftype)
3191{
3192 struct nfs4_createdata *data;
3193
3194 data = kzalloc(sizeof(*data), GFP_KERNEL);
3195 if (data != NULL) {
3196 struct nfs_server *server = NFS_SERVER(dir);
3197
3198 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3199 data->msg.rpc_argp = &data->arg;
3200 data->msg.rpc_resp = &data->res;
3201 data->arg.dir_fh = NFS_FH(dir);
3202 data->arg.server = server;
3203 data->arg.name = name;
3204 data->arg.attrs = sattr;
3205 data->arg.ftype = ftype;
3206 data->arg.bitmask = server->attr_bitmask;
3207 data->res.server = server;
3208 data->res.fh = &data->fh;
3209 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003210 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003211 }
3212 return data;
3213}
3214
3215static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3216{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003217 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003218 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003219 if (status == 0) {
3220 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003221 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3222 }
3223 return status;
3224}
3225
3226static void nfs4_free_createdata(struct nfs4_createdata *data)
3227{
3228 kfree(data);
3229}
3230
Chuck Lever4f390c12006-08-22 20:06:22 -04003231static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003232 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003234 struct nfs4_createdata *data;
3235 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Chuck Lever94a6d752006-08-22 20:06:23 -04003237 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003238 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003239
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003240 status = -ENOMEM;
3241 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3242 if (data == NULL)
3243 goto out;
3244
3245 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3246 data->arg.u.symlink.pages = &page;
3247 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003249 status = nfs4_do_create(dir, dentry, data);
3250
3251 nfs4_free_createdata(data);
3252out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 return status;
3254}
3255
Chuck Lever4f390c12006-08-22 20:06:22 -04003256static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003257 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
3259 struct nfs4_exception exception = { };
3260 int err;
3261 do {
3262 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003263 _nfs4_proc_symlink(dir, dentry, page,
3264 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 &exception);
3266 } while (exception.retry);
3267 return err;
3268}
3269
3270static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3271 struct iattr *sattr)
3272{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003273 struct nfs4_createdata *data;
3274 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003276 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3277 if (data == NULL)
3278 goto out;
3279
3280 status = nfs4_do_create(dir, dentry, data);
3281
3282 nfs4_free_createdata(data);
3283out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 return status;
3285}
3286
3287static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3288 struct iattr *sattr)
3289{
3290 struct nfs4_exception exception = { };
3291 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003292
3293 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 do {
3295 err = nfs4_handle_exception(NFS_SERVER(dir),
3296 _nfs4_proc_mkdir(dir, dentry, sattr),
3297 &exception);
3298 } while (exception.retry);
3299 return err;
3300}
3301
3302static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003303 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
3305 struct inode *dir = dentry->d_inode;
3306 struct nfs4_readdir_arg args = {
3307 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003308 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 .pgbase = 0,
3310 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003311 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003312 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 };
3314 struct nfs4_readdir_res res;
3315 struct rpc_message msg = {
3316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3317 .rpc_argp = &args,
3318 .rpc_resp = &res,
3319 .rpc_cred = cred,
3320 };
3321 int status;
3322
Harvey Harrison3110ff82008-05-02 13:42:44 -07003323 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003324 dentry->d_parent->d_name.name,
3325 dentry->d_name.name,
3326 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003327 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003329 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 -05003330 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003331 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003332 status += args.pgbase;
3333 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003334
3335 nfs_invalidate_atime(dir);
3336
Harvey Harrison3110ff82008-05-02 13:42:44 -07003337 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 return status;
3339}
3340
3341static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003342 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
3344 struct nfs4_exception exception = { };
3345 int err;
3346 do {
3347 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3348 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003349 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 &exception);
3351 } while (exception.retry);
3352 return err;
3353}
3354
3355static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3356 struct iattr *sattr, dev_t rdev)
3357{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003358 struct nfs4_createdata *data;
3359 int mode = sattr->ia_mode;
3360 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
3362 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3363 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003364
3365 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3366 if (data == NULL)
3367 goto out;
3368
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003370 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003372 data->arg.ftype = NF4BLK;
3373 data->arg.u.device.specdata1 = MAJOR(rdev);
3374 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
3376 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003377 data->arg.ftype = NF4CHR;
3378 data->arg.u.device.specdata1 = MAJOR(rdev);
3379 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003382 status = nfs4_do_create(dir, dentry, data);
3383
3384 nfs4_free_createdata(data);
3385out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 return status;
3387}
3388
3389static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3390 struct iattr *sattr, dev_t rdev)
3391{
3392 struct nfs4_exception exception = { };
3393 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003394
3395 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 do {
3397 err = nfs4_handle_exception(NFS_SERVER(dir),
3398 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3399 &exception);
3400 } while (exception.retry);
3401 return err;
3402}
3403
3404static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3405 struct nfs_fsstat *fsstat)
3406{
3407 struct nfs4_statfs_arg args = {
3408 .fh = fhandle,
3409 .bitmask = server->attr_bitmask,
3410 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003411 struct nfs4_statfs_res res = {
3412 .fsstat = fsstat,
3413 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 struct rpc_message msg = {
3415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3416 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003417 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 };
3419
Trond Myklebust0e574af2005-10-27 22:12:38 -04003420 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003421 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
3423
3424static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3425{
3426 struct nfs4_exception exception = { };
3427 int err;
3428 do {
3429 err = nfs4_handle_exception(server,
3430 _nfs4_proc_statfs(server, fhandle, fsstat),
3431 &exception);
3432 } while (exception.retry);
3433 return err;
3434}
3435
3436static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3437 struct nfs_fsinfo *fsinfo)
3438{
3439 struct nfs4_fsinfo_arg args = {
3440 .fh = fhandle,
3441 .bitmask = server->attr_bitmask,
3442 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003443 struct nfs4_fsinfo_res res = {
3444 .fsinfo = fsinfo,
3445 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 struct rpc_message msg = {
3447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3448 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003449 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 };
3451
Bryan Schumaker7c513052011-03-24 17:12:24 +00003452 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453}
3454
3455static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3456{
3457 struct nfs4_exception exception = { };
3458 int err;
3459
3460 do {
3461 err = nfs4_handle_exception(server,
3462 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3463 &exception);
3464 } while (exception.retry);
3465 return err;
3466}
3467
3468static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3469{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003470 int error;
3471
Trond Myklebust0e574af2005-10-27 22:12:38 -04003472 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003473 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003474 if (error == 0) {
3475 /* block layout checks this! */
3476 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003477 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003478 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003479
3480 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481}
3482
3483static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3484 struct nfs_pathconf *pathconf)
3485{
3486 struct nfs4_pathconf_arg args = {
3487 .fh = fhandle,
3488 .bitmask = server->attr_bitmask,
3489 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003490 struct nfs4_pathconf_res res = {
3491 .pathconf = pathconf,
3492 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 struct rpc_message msg = {
3494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3495 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003496 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 };
3498
3499 /* None of the pathconf attributes are mandatory to implement */
3500 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3501 memset(pathconf, 0, sizeof(*pathconf));
3502 return 0;
3503 }
3504
Trond Myklebust0e574af2005-10-27 22:12:38 -04003505 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003506 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
3508
3509static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3510 struct nfs_pathconf *pathconf)
3511{
3512 struct nfs4_exception exception = { };
3513 int err;
3514
3515 do {
3516 err = nfs4_handle_exception(server,
3517 _nfs4_proc_pathconf(server, fhandle, pathconf),
3518 &exception);
3519 } while (exception.retry);
3520 return err;
3521}
3522
Benny Halevyd20581a2011-05-22 19:52:03 +03003523void __nfs4_read_done_cb(struct nfs_read_data *data)
3524{
Fred Isamancd841602012-04-20 14:47:44 -04003525 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003526}
3527
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003528static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Fred Isamancd841602012-04-20 14:47:44 -04003530 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003532 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003533 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003534 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003536
Benny Halevyd20581a2011-05-22 19:52:03 +03003537 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003539 renew_lease(server, data->timestamp);
3540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541}
3542
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003543static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3544{
3545
3546 dprintk("--> %s\n", __func__);
3547
3548 if (!nfs4_sequence_done(task, &data->res.seq_res))
3549 return -EAGAIN;
3550
Benny Halevyd20581a2011-05-22 19:52:03 +03003551 return data->read_done_cb ? data->read_done_cb(task, data) :
3552 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003553}
3554
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003555static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003558 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003559 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003560 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003563static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3564{
Fred Isamancd841602012-04-20 14:47:44 -04003565 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003566 &data->args.seq_args,
3567 &data->res.seq_res,
3568 task))
3569 return;
3570 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
Fred Isamanb029bc92011-03-03 15:13:42 +00003573static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
Fred Isamancd841602012-04-20 14:47:44 -04003575 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003577 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003578 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003579 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003581 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003583 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003584 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
Fred Isamanb029bc92011-03-03 15:13:42 +00003588static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3589{
3590 if (!nfs4_sequence_done(task, &data->res.seq_res))
3591 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003592 return data->write_done_cb ? data->write_done_cb(task, data) :
3593 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003594}
3595
Trond Myklebust5a37f852012-04-28 14:55:16 -04003596static
3597bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003598{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003599 const struct nfs_pgio_header *hdr = data->header;
3600
3601 /* Don't request attributes for pNFS or O_DIRECT writes */
3602 if (data->ds_clp != NULL || hdr->dreq != NULL)
3603 return false;
3604 /* Otherwise, request attributes if and only if we don't hold
3605 * a delegation
3606 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003607 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003608}
Fred Isamana69aef12011-03-03 15:13:47 +00003609
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003610static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611{
Fred Isamancd841602012-04-20 14:47:44 -04003612 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003613
Trond Myklebust5a37f852012-04-28 14:55:16 -04003614 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003615 data->args.bitmask = NULL;
3616 data->res.fattr = NULL;
3617 } else
3618 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003619
Fred Isamanb029bc92011-03-03 15:13:42 +00003620 if (!data->write_done_cb)
3621 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003622 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 data->timestamp = jiffies;
3624
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003625 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003626 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627}
3628
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003629static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3630{
Fred Isamancd841602012-04-20 14:47:44 -04003631 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003632 &data->args.seq_args,
3633 &data->res.seq_res,
3634 task))
3635 return;
3636 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637}
3638
Fred Isaman0b7c0152012-04-20 14:47:39 -04003639static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3640{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3642 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003643 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 return;
3646 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003647}
3648
Fred Isaman0b7c0152012-04-20 14:47:39 -04003649static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
3651 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003652
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003653 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003654 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003655 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658}
3659
Fred Isaman0b7c0152012-04-20 14:47:39 -04003660static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003661{
3662 if (!nfs4_sequence_done(task, &data->res.seq_res))
3663 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003664 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003665}
3666
Fred Isaman0b7c0152012-04-20 14:47:39 -04003667static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003669 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003670
Fred Isaman0b7c0152012-04-20 14:47:39 -04003671 if (data->commit_done_cb == NULL)
3672 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003673 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003674 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003675 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676}
3677
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003678struct nfs4_renewdata {
3679 struct nfs_client *client;
3680 unsigned long timestamp;
3681};
3682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683/*
3684 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3685 * standalone procedure for queueing an asynchronous RENEW.
3686 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003687static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003688{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003689 struct nfs4_renewdata *data = calldata;
3690 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003691
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003692 if (atomic_read(&clp->cl_count) > 1)
3693 nfs4_schedule_state_renewal(clp);
3694 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003695 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003696}
3697
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003698static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003700 struct nfs4_renewdata *data = calldata;
3701 struct nfs_client *clp = data->client;
3702 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
3704 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003705 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003706 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3707 return;
3708 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003709 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003710 return;
3711 }
3712 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003714 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715}
3716
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003717static const struct rpc_call_ops nfs4_renew_ops = {
3718 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003719 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003720};
3721
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003722static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723{
3724 struct rpc_message msg = {
3725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3726 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003727 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003729 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003731 if (renew_flags == 0)
3732 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003733 if (!atomic_inc_not_zero(&clp->cl_count))
3734 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003735 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003736 if (data == NULL)
3737 return -ENOMEM;
3738 data->client = clp;
3739 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003741 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003744static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
3746 struct rpc_message msg = {
3747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3748 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003749 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 };
3751 unsigned long now = jiffies;
3752 int status;
3753
3754 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3755 if (status < 0)
3756 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003757 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 return 0;
3759}
3760
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003761static inline int nfs4_server_supports_acls(struct nfs_server *server)
3762{
3763 return (server->caps & NFS_CAP_ACLS)
3764 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3765 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3766}
3767
Trond Myklebust21f498c2012-08-24 10:59:25 -04003768/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3769 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003770 * the stack.
3771 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003772#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003773
Neil Hormane9e3d722011-03-04 19:26:03 -05003774static int buf_to_pages_noslab(const void *buf, size_t buflen,
3775 struct page **pages, unsigned int *pgbase)
3776{
3777 struct page *newpage, **spages;
3778 int rc = 0;
3779 size_t len;
3780 spages = pages;
3781
3782 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003783 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003784 newpage = alloc_page(GFP_KERNEL);
3785
3786 if (newpage == NULL)
3787 goto unwind;
3788 memcpy(page_address(newpage), buf, len);
3789 buf += len;
3790 buflen -= len;
3791 *pages++ = newpage;
3792 rc++;
3793 } while (buflen != 0);
3794
3795 return rc;
3796
3797unwind:
3798 for(; rc > 0; rc--)
3799 __free_page(spages[rc-1]);
3800 return -ENOMEM;
3801}
3802
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003803struct nfs4_cached_acl {
3804 int cached;
3805 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003806 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003807};
3808
3809static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003810{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003811 struct nfs_inode *nfsi = NFS_I(inode);
3812
3813 spin_lock(&inode->i_lock);
3814 kfree(nfsi->nfs4_acl);
3815 nfsi->nfs4_acl = acl;
3816 spin_unlock(&inode->i_lock);
3817}
3818
3819static void nfs4_zap_acl_attr(struct inode *inode)
3820{
3821 nfs4_set_cached_acl(inode, NULL);
3822}
3823
3824static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3825{
3826 struct nfs_inode *nfsi = NFS_I(inode);
3827 struct nfs4_cached_acl *acl;
3828 int ret = -ENOENT;
3829
3830 spin_lock(&inode->i_lock);
3831 acl = nfsi->nfs4_acl;
3832 if (acl == NULL)
3833 goto out;
3834 if (buf == NULL) /* user is just asking for length */
3835 goto out_len;
3836 if (acl->cached == 0)
3837 goto out;
3838 ret = -ERANGE; /* see getxattr(2) man page */
3839 if (acl->len > buflen)
3840 goto out;
3841 memcpy(buf, acl->data, acl->len);
3842out_len:
3843 ret = acl->len;
3844out:
3845 spin_unlock(&inode->i_lock);
3846 return ret;
3847}
3848
Sachin Prabhu5794d212012-04-17 14:36:40 +01003849static 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 +00003850{
3851 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003852 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003853
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003854 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003855 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003856 if (acl == NULL)
3857 goto out;
3858 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003859 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003860 } else {
3861 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3862 if (acl == NULL)
3863 goto out;
3864 acl->cached = 0;
3865 }
3866 acl->len = acl_len;
3867out:
3868 nfs4_set_cached_acl(inode, acl);
3869}
3870
Andy Adamsonbf118a32011-12-07 11:55:27 -05003871/*
3872 * The getxattr API returns the required buffer length when called with a
3873 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3874 * the required buf. On a NULL buf, we send a page of data to the server
3875 * guessing that the ACL request can be serviced by a page. If so, we cache
3876 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3877 * the cache. If not so, we throw away the page, and cache the required
3878 * length. The next getxattr call will then produce another round trip to
3879 * the server, this time with the input buf of the required size.
3880 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003881static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003882{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003883 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003884 struct nfs_getaclargs args = {
3885 .fh = NFS_FH(inode),
3886 .acl_pages = pages,
3887 .acl_len = buflen,
3888 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003889 struct nfs_getaclres res = {
3890 .acl_len = buflen,
3891 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3894 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003895 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003896 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003897 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3898 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003899
Andy Adamsonbf118a32011-12-07 11:55:27 -05003900 /* As long as we're doing a round trip to the server anyway,
3901 * let's be prepared for a page of acl data. */
3902 if (npages == 0)
3903 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003904 if (npages > ARRAY_SIZE(pages))
3905 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003906
Andy Adamsonbf118a32011-12-07 11:55:27 -05003907 for (i = 0; i < npages; i++) {
3908 pages[i] = alloc_page(GFP_KERNEL);
3909 if (!pages[i])
3910 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003911 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003912
3913 /* for decoding across pages */
3914 res.acl_scratch = alloc_page(GFP_KERNEL);
3915 if (!res.acl_scratch)
3916 goto out_free;
3917
Andy Adamsonbf118a32011-12-07 11:55:27 -05003918 args.acl_len = npages * PAGE_SIZE;
3919 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003920
Peng Taode040be2012-01-10 22:42:47 +08003921 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003922 __func__, buf, buflen, npages, args.acl_len);
3923 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3924 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003925 if (ret)
3926 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003927
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003928 /* Handle the case where the passed-in buffer is too short */
3929 if (res.acl_flags & NFS4_ACL_TRUNC) {
3930 /* Did the user only issue a request for the acl length? */
3931 if (buf == NULL)
3932 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003933 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003934 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003935 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003936 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3937 if (buf)
3938 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3939out_ok:
3940 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003941out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003942 for (i = 0; i < npages; i++)
3943 if (pages[i])
3944 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003945 if (res.acl_scratch)
3946 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003947 return ret;
3948}
3949
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003950static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3951{
3952 struct nfs4_exception exception = { };
3953 ssize_t ret;
3954 do {
3955 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3956 if (ret >= 0)
3957 break;
3958 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3959 } while (exception.retry);
3960 return ret;
3961}
3962
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003963static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3964{
3965 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003966 int ret;
3967
3968 if (!nfs4_server_supports_acls(server))
3969 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003970 ret = nfs_revalidate_inode(server, inode);
3971 if (ret < 0)
3972 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003973 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3974 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003975 ret = nfs4_read_cached_acl(inode, buf, buflen);
3976 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003977 /* -ENOENT is returned if there is no ACL or if there is an ACL
3978 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003979 return ret;
3980 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003981}
3982
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003983static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003984{
3985 struct nfs_server *server = NFS_SERVER(inode);
3986 struct page *pages[NFS4ACL_MAXPAGES];
3987 struct nfs_setaclargs arg = {
3988 .fh = NFS_FH(inode),
3989 .acl_pages = pages,
3990 .acl_len = buflen,
3991 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003992 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003993 struct rpc_message msg = {
3994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3995 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003996 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003997 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003998 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003999 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004000
4001 if (!nfs4_server_supports_acls(server))
4002 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004003 if (npages > ARRAY_SIZE(pages))
4004 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004005 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4006 if (i < 0)
4007 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004008 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004009 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004010
4011 /*
4012 * Free each page after tx, so the only ref left is
4013 * held by the network stack
4014 */
4015 for (; i > 0; i--)
4016 put_page(pages[i-1]);
4017
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004018 /*
4019 * Acl update can result in inode attribute update.
4020 * so mark the attribute cache invalid.
4021 */
4022 spin_lock(&inode->i_lock);
4023 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4024 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004025 nfs_access_zap_cache(inode);
4026 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004027 return ret;
4028}
4029
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004030static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4031{
4032 struct nfs4_exception exception = { };
4033 int err;
4034 do {
4035 err = nfs4_handle_exception(NFS_SERVER(inode),
4036 __nfs4_proc_set_acl(inode, buf, buflen),
4037 &exception);
4038 } while (exception.retry);
4039 return err;
4040}
4041
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004043nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004045 struct nfs_client *clp = server->nfs_client;
4046
4047 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 return 0;
4049 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004050 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004051 case -NFS4ERR_ADMIN_REVOKED:
4052 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004053 if (state == NULL)
4054 break;
4055 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004056 case -NFS4ERR_OPENMODE:
4057 if (state == NULL)
4058 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004059 nfs4_schedule_stateid_recovery(server, state);
4060 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004061 case -NFS4ERR_EXPIRED:
4062 if (state != NULL)
4063 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004065 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004066 nfs4_schedule_lease_recovery(clp);
4067 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004068#if defined(CONFIG_NFS_V4_1)
4069 case -NFS4ERR_BADSESSION:
4070 case -NFS4ERR_BADSLOT:
4071 case -NFS4ERR_BAD_HIGH_SLOT:
4072 case -NFS4ERR_DEADSESSION:
4073 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4074 case -NFS4ERR_SEQ_FALSE_RETRY:
4075 case -NFS4ERR_SEQ_MISORDERED:
4076 dprintk("%s ERROR %d, Reset session\n", __func__,
4077 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004078 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004079 task->tk_status = 0;
4080 return -EAGAIN;
4081#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004083 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004084 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004085 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4087 task->tk_status = 0;
4088 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004089 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 case -NFS4ERR_OLD_STATEID:
4091 task->tk_status = 0;
4092 return -EAGAIN;
4093 }
4094 task->tk_status = nfs4_map_errors(task->tk_status);
4095 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004096wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004097 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004098 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4099 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4100 task->tk_status = 0;
4101 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102}
4103
Chuck Leverf0920752012-05-21 22:45:41 -04004104static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4105 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004106{
4107 __be32 verf[2];
4108
Chuck Lever2c820d92012-05-21 22:45:33 -04004109 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4110 /* An impossible timestamp guarantees this value
4111 * will never match a generated boot time. */
4112 verf[0] = 0;
4113 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4114 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004115 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4116 verf[0] = (__be32)nn->boot_time.tv_sec;
4117 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004118 }
Chuck Levercd937102012-03-02 17:14:31 -05004119 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4120}
4121
Chuck Levere984a552012-09-14 17:24:21 -04004122static unsigned int
4123nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4124 char *buf, size_t len)
4125{
4126 unsigned int result;
4127
4128 rcu_read_lock();
4129 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4130 clp->cl_ipaddr,
4131 rpc_peeraddr2str(clp->cl_rpcclient,
4132 RPC_DISPLAY_ADDR),
4133 rpc_peeraddr2str(clp->cl_rpcclient,
4134 RPC_DISPLAY_PROTO));
4135 rcu_read_unlock();
4136 return result;
4137}
4138
4139static unsigned int
4140nfs4_init_uniform_client_string(const struct nfs_client *clp,
4141 char *buf, size_t len)
4142{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004143 char *nodename = clp->cl_rpcclient->cl_nodename;
4144
4145 if (nfs4_client_id_uniquifier[0] != '\0')
4146 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004147 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4148 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004149 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004150}
4151
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004152/**
4153 * nfs4_proc_setclientid - Negotiate client ID
4154 * @clp: state data structure
4155 * @program: RPC program for NFSv4 callback service
4156 * @port: IP port number for NFS4 callback service
4157 * @cred: RPC credential to use for this call
4158 * @res: where to place the result
4159 *
4160 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4161 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004162int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4163 unsigned short port, struct rpc_cred *cred,
4164 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
4166 nfs4_verifier sc_verifier;
4167 struct nfs4_setclientid setclientid = {
4168 .sc_verifier = &sc_verifier,
4169 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004170 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 };
4172 struct rpc_message msg = {
4173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4174 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004175 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004176 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004178 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
Chuck Leverde734832012-07-11 16:30:50 -04004180 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004181 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004182 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4183 setclientid.sc_name_len =
4184 nfs4_init_uniform_client_string(clp,
4185 setclientid.sc_name,
4186 sizeof(setclientid.sc_name));
4187 else
4188 setclientid.sc_name_len =
4189 nfs4_init_nonuniform_client_string(clp,
4190 setclientid.sc_name,
4191 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004192 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004193 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004194 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004195 sizeof(setclientid.sc_netid),
4196 rpc_peeraddr2str(clp->cl_rpcclient,
4197 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004198 rcu_read_unlock();
4199 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004200 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 clp->cl_ipaddr, port >> 8, port & 255);
4202
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004203 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4204 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4205 setclientid.sc_name_len, setclientid.sc_name);
4206 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4207 dprintk("NFS reply setclientid: %d\n", status);
4208 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209}
4210
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004211/**
4212 * nfs4_proc_setclientid_confirm - Confirm client ID
4213 * @clp: state data structure
4214 * @res: result of a previous SETCLIENTID
4215 * @cred: RPC credential to use for this call
4216 *
4217 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4218 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004219int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004220 struct nfs4_setclientid_res *arg,
4221 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222{
4223 struct nfs_fsinfo fsinfo;
4224 struct rpc_message msg = {
4225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004226 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004228 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 };
4230 unsigned long now;
4231 int status;
4232
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004233 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4234 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4235 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004237 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 if (status == 0) {
4239 spin_lock(&clp->cl_lock);
4240 clp->cl_lease_time = fsinfo.lease_time * HZ;
4241 clp->cl_last_renewal = now;
4242 spin_unlock(&clp->cl_lock);
4243 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004244 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 return status;
4246}
4247
Trond Myklebustfe650402006-01-03 09:55:18 +01004248struct nfs4_delegreturndata {
4249 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004250 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004251 struct nfs_fh fh;
4252 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004253 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004254 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004255 int rpc_status;
4256};
4257
Trond Myklebustfe650402006-01-03 09:55:18 +01004258static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4259{
4260 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004261
Trond Myklebust14516c32010-07-31 14:29:06 -04004262 if (!nfs4_sequence_done(task, &data->res.seq_res))
4263 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004264
Ricardo Labiaga79708862009-12-07 09:23:21 -05004265 switch (task->tk_status) {
4266 case -NFS4ERR_STALE_STATEID:
4267 case -NFS4ERR_EXPIRED:
4268 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004269 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004270 break;
4271 default:
4272 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4273 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004274 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004275 return;
4276 }
4277 }
4278 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004279}
4280
4281static void nfs4_delegreturn_release(void *calldata)
4282{
Trond Myklebustfe650402006-01-03 09:55:18 +01004283 kfree(calldata);
4284}
4285
Andy Adamson938e1012009-04-01 09:22:28 -04004286#if defined(CONFIG_NFS_V4_1)
4287static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4288{
4289 struct nfs4_delegreturndata *d_data;
4290
4291 d_data = (struct nfs4_delegreturndata *)data;
4292
Trond Myklebust035168a2010-06-16 09:52:26 -04004293 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004294 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004295 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004296 return;
4297 rpc_call_start(task);
4298}
4299#endif /* CONFIG_NFS_V4_1 */
4300
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004301static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004302#if defined(CONFIG_NFS_V4_1)
4303 .rpc_call_prepare = nfs4_delegreturn_prepare,
4304#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004305 .rpc_call_done = nfs4_delegreturn_done,
4306 .rpc_release = nfs4_delegreturn_release,
4307};
4308
Trond Myklebuste6f81072008-01-24 18:14:34 -05004309static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004310{
4311 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004312 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004313 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004314 struct rpc_message msg = {
4315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4316 .rpc_cred = cred,
4317 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004318 struct rpc_task_setup task_setup_data = {
4319 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004320 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004321 .callback_ops = &nfs4_delegreturn_ops,
4322 .flags = RPC_TASK_ASYNC,
4323 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004324 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004325
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004326 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004327 if (data == NULL)
4328 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004329 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004330 data->args.fhandle = &data->fh;
4331 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004332 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004333 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004334 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004335 data->res.fattr = &data->fattr;
4336 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004337 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004338 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004339 data->rpc_status = 0;
4340
Trond Myklebustc970aa82007-07-14 15:39:59 -04004341 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004342 msg.rpc_argp = &data->args;
4343 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004344 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004345 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004346 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004347 if (!issync)
4348 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004349 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004350 if (status != 0)
4351 goto out;
4352 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004353 if (status == 0)
4354 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4355 else
4356 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004357out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004358 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004359 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
Trond Myklebuste6f81072008-01-24 18:14:34 -05004362int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363{
4364 struct nfs_server *server = NFS_SERVER(inode);
4365 struct nfs4_exception exception = { };
4366 int err;
4367 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004368 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 switch (err) {
4370 case -NFS4ERR_STALE_STATEID:
4371 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 case 0:
4373 return 0;
4374 }
4375 err = nfs4_handle_exception(server, err, &exception);
4376 } while (exception.retry);
4377 return err;
4378}
4379
4380#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4381#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4382
4383/*
4384 * sleep, with exponential backoff, and retry the LOCK operation.
4385 */
4386static unsigned long
4387nfs4_set_lock_task_retry(unsigned long timeout)
4388{
Jeff Laytond3103102011-12-01 22:44:39 +01004389 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 timeout <<= 1;
4391 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4392 return NFS4_LOCK_MAXTIMEOUT;
4393 return timeout;
4394}
4395
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4397{
4398 struct inode *inode = state->inode;
4399 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004400 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004401 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004403 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004405 struct nfs_lockt_res res = {
4406 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 };
4408 struct rpc_message msg = {
4409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4410 .rpc_argp = &arg,
4411 .rpc_resp = &res,
4412 .rpc_cred = state->owner->so_cred,
4413 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 struct nfs4_lock_state *lsp;
4415 int status;
4416
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004417 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004418 status = nfs4_set_lock_state(state, request);
4419 if (status != 0)
4420 goto out;
4421 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004422 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004423 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004424 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004425 switch (status) {
4426 case 0:
4427 request->fl_type = F_UNLCK;
4428 break;
4429 case -NFS4ERR_DENIED:
4430 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004432 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004433out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 return status;
4435}
4436
4437static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4438{
4439 struct nfs4_exception exception = { };
4440 int err;
4441
4442 do {
4443 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4444 _nfs4_proc_getlk(state, cmd, request),
4445 &exception);
4446 } while (exception.retry);
4447 return err;
4448}
4449
4450static int do_vfs_lock(struct file *file, struct file_lock *fl)
4451{
4452 int res = 0;
4453 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4454 case FL_POSIX:
4455 res = posix_lock_file_wait(file, fl);
4456 break;
4457 case FL_FLOCK:
4458 res = flock_lock_file_wait(file, fl);
4459 break;
4460 default:
4461 BUG();
4462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 return res;
4464}
4465
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004466struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004467 struct nfs_locku_args arg;
4468 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004469 struct nfs4_lock_state *lsp;
4470 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004471 struct file_lock fl;
4472 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004473 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004474};
4475
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004476static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4477 struct nfs_open_context *ctx,
4478 struct nfs4_lock_state *lsp,
4479 struct nfs_seqid *seqid)
4480{
4481 struct nfs4_unlockdata *p;
4482 struct inode *inode = lsp->ls_state->inode;
4483
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004484 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004485 if (p == NULL)
4486 return NULL;
4487 p->arg.fh = NFS_FH(inode);
4488 p->arg.fl = &p->fl;
4489 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004490 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004491 p->arg.stateid = &lsp->ls_stateid;
4492 p->lsp = lsp;
4493 atomic_inc(&lsp->ls_count);
4494 /* Ensure we don't close file until we're done freeing locks! */
4495 p->ctx = get_nfs_open_context(ctx);
4496 memcpy(&p->fl, fl, sizeof(p->fl));
4497 p->server = NFS_SERVER(inode);
4498 return p;
4499}
4500
Trond Myklebust06f814a2006-01-03 09:55:07 +01004501static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004502{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004503 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004504 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004505 nfs4_put_lock_state(calldata->lsp);
4506 put_nfs_open_context(calldata->ctx);
4507 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004508}
4509
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004510static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004511{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004512 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004513
Trond Myklebust14516c32010-07-31 14:29:06 -04004514 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4515 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004516 switch (task->tk_status) {
4517 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004518 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4519 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004520 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004521 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004522 case -NFS4ERR_BAD_STATEID:
4523 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004524 case -NFS4ERR_STALE_STATEID:
4525 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004526 break;
4527 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004528 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004529 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004530 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004531}
4532
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004533static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004534{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004535 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004537 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004538 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004539 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004540 /* Note: exit _without_ running nfs4_locku_done */
4541 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004542 return;
4543 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004544 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004545 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004546 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004547 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004548 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004549 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550}
4551
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004552static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004553 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004554 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004555 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004556};
4557
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004558static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4559 struct nfs_open_context *ctx,
4560 struct nfs4_lock_state *lsp,
4561 struct nfs_seqid *seqid)
4562{
4563 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004564 struct rpc_message msg = {
4565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4566 .rpc_cred = ctx->cred,
4567 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004568 struct rpc_task_setup task_setup_data = {
4569 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004570 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004571 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004572 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004573 .flags = RPC_TASK_ASYNC,
4574 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004575
Frank Filz137d6ac2007-07-09 15:32:29 -07004576 /* Ensure this is an unlock - when canceling a lock, the
4577 * canceled lock is passed in, and it won't be an unlock.
4578 */
4579 fl->fl_type = F_UNLCK;
4580
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004581 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4582 if (data == NULL) {
4583 nfs_free_seqid(seqid);
4584 return ERR_PTR(-ENOMEM);
4585 }
4586
Trond Myklebust9d12b212012-01-17 22:04:25 -05004587 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004588 msg.rpc_argp = &data->arg;
4589 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004590 task_setup_data.callback_data = data;
4591 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004592}
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4595{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004596 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004597 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004598 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004599 struct rpc_task *task;
4600 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004601 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
Trond Myklebust9b073572006-06-29 16:38:34 -04004603 status = nfs4_set_lock_state(state, request);
4604 /* Unlock _before_ we do the RPC call */
4605 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004606 down_read(&nfsi->rwsem);
4607 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4608 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004609 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004610 }
4611 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004612 if (status != 0)
4613 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004614 /* Is this a delegated lock? */
4615 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004616 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004617 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004618 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004619 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004620 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004621 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004622 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004623 status = PTR_ERR(task);
4624 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004625 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004626 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004627 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004628out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004629 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004630 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631}
4632
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004633struct nfs4_lockdata {
4634 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004635 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004636 struct nfs4_lock_state *lsp;
4637 struct nfs_open_context *ctx;
4638 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004639 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004640 int rpc_status;
4641 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004642 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004643};
4644
4645static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004646 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4647 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004648{
4649 struct nfs4_lockdata *p;
4650 struct inode *inode = lsp->ls_state->inode;
4651 struct nfs_server *server = NFS_SERVER(inode);
4652
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004653 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004654 if (p == NULL)
4655 return NULL;
4656
4657 p->arg.fh = NFS_FH(inode);
4658 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004659 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004660 if (p->arg.open_seqid == NULL)
4661 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004662 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004663 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004664 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004665 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004666 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004667 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004668 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004669 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004670 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004671 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004672 atomic_inc(&lsp->ls_count);
4673 p->ctx = get_nfs_open_context(ctx);
4674 memcpy(&p->fl, fl, sizeof(p->fl));
4675 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004676out_free_seqid:
4677 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004678out_free:
4679 kfree(p);
4680 return NULL;
4681}
4682
4683static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4684{
4685 struct nfs4_lockdata *data = calldata;
4686 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Harvey Harrison3110ff82008-05-02 13:42:44 -07004688 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004689 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4690 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004691 /* Do we need to do an open_to_lock_owner? */
4692 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004693 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4694 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004695 data->arg.open_stateid = &state->stateid;
4696 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004697 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004698 } else
4699 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004700 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004701 if (nfs4_setup_sequence(data->server,
4702 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004703 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004704 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004705 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004706 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004707}
4708
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004709static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4710{
4711 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4712 nfs4_lock_prepare(task, calldata);
4713}
4714
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004715static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4716{
4717 struct nfs4_lockdata *data = calldata;
4718
Harvey Harrison3110ff82008-05-02 13:42:44 -07004719 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004720
Trond Myklebust14516c32010-07-31 14:29:06 -04004721 if (!nfs4_sequence_done(task, &data->res.seq_res))
4722 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004723
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004724 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004725 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004726 if (data->rpc_status == 0)
4727 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4728 else
4729 goto out;
4730 }
4731 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004732 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004733 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004734 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004735 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004736out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004737 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004738}
4739
4740static void nfs4_lock_release(void *calldata)
4741{
4742 struct nfs4_lockdata *data = calldata;
4743
Harvey Harrison3110ff82008-05-02 13:42:44 -07004744 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004745 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004746 if (data->cancelled != 0) {
4747 struct rpc_task *task;
4748 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4749 data->arg.lock_seqid);
4750 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004751 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004752 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004753 } else
4754 nfs_free_seqid(data->arg.lock_seqid);
4755 nfs4_put_lock_state(data->lsp);
4756 put_nfs_open_context(data->ctx);
4757 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004758 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004759}
4760
4761static const struct rpc_call_ops nfs4_lock_ops = {
4762 .rpc_call_prepare = nfs4_lock_prepare,
4763 .rpc_call_done = nfs4_lock_done,
4764 .rpc_release = nfs4_lock_release,
4765};
4766
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004767static const struct rpc_call_ops nfs4_recover_lock_ops = {
4768 .rpc_call_prepare = nfs4_recover_lock_prepare,
4769 .rpc_call_done = nfs4_lock_done,
4770 .rpc_release = nfs4_lock_release,
4771};
4772
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004773static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4774{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004775 switch (error) {
4776 case -NFS4ERR_ADMIN_REVOKED:
4777 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004778 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004779 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004780 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004781 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004782 break;
4783 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004784 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004785 case -NFS4ERR_EXPIRED:
4786 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004787 };
4788}
4789
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004790static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004791{
4792 struct nfs4_lockdata *data;
4793 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004794 struct rpc_message msg = {
4795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4796 .rpc_cred = state->owner->so_cred,
4797 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004798 struct rpc_task_setup task_setup_data = {
4799 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004800 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004801 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004802 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004803 .flags = RPC_TASK_ASYNC,
4804 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004805 int ret;
4806
Harvey Harrison3110ff82008-05-02 13:42:44 -07004807 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004808 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004809 fl->fl_u.nfs4_fl.owner,
4810 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004811 if (data == NULL)
4812 return -ENOMEM;
4813 if (IS_SETLKW(cmd))
4814 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004815 if (recovery_type > NFS_LOCK_NEW) {
4816 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004817 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004818 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4819 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004820 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004821 msg.rpc_argp = &data->arg;
4822 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004823 task_setup_data.callback_data = data;
4824 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004825 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004826 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004827 ret = nfs4_wait_for_completion_rpc_task(task);
4828 if (ret == 0) {
4829 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004830 if (ret)
4831 nfs4_handle_setlk_error(data->server, data->lsp,
4832 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004833 } else
4834 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004835 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004836 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004837 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838}
4839
4840static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4841{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004842 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004843 struct nfs4_exception exception = {
4844 .inode = state->inode,
4845 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004846 int err;
4847
4848 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004849 /* Cache the lock if possible... */
4850 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4851 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004852 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004853 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004854 break;
4855 nfs4_handle_exception(server, err, &exception);
4856 } while (exception.retry);
4857 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858}
4859
4860static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4861{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004862 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004863 struct nfs4_exception exception = {
4864 .inode = state->inode,
4865 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004866 int err;
4867
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004868 err = nfs4_set_lock_state(state, request);
4869 if (err != 0)
4870 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004871 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004872 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4873 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004874 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004875 switch (err) {
4876 default:
4877 goto out;
4878 case -NFS4ERR_GRACE:
4879 case -NFS4ERR_DELAY:
4880 nfs4_handle_exception(server, err, &exception);
4881 err = 0;
4882 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004883 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004884out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004885 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886}
4887
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004888#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004889/**
4890 * nfs41_check_expired_locks - possibly free a lock stateid
4891 *
4892 * @state: NFSv4 state for an inode
4893 *
4894 * Returns NFS_OK if recovery for this stateid is now finished.
4895 * Otherwise a negative NFS4ERR value is returned.
4896 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004897static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004898{
Chuck Levereb64cf92012-07-11 16:30:05 -04004899 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004900 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004901 struct nfs_server *server = NFS_SERVER(state->inode);
4902
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004903 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004904 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004905 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4906 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004907 /* Free the stateid unless the server
4908 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004909 if (status != -NFS4ERR_BAD_STATEID)
4910 nfs41_free_stateid(server,
4911 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004912 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004913 ret = status;
4914 }
4915 }
4916 };
4917
4918 return ret;
4919}
4920
4921static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4922{
4923 int status = NFS_OK;
4924
4925 if (test_bit(LK_STATE_IN_USE, &state->flags))
4926 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004927 if (status != NFS_OK)
4928 status = nfs4_lock_expired(state, request);
4929 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004930}
4931#endif
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4934{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004935 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004936 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004937 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004939 if ((fl_flags & FL_POSIX) &&
4940 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4941 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004942 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004943 status = nfs4_set_lock_state(state, request);
4944 if (status != 0)
4945 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004946 request->fl_flags |= FL_ACCESS;
4947 status = do_vfs_lock(request->fl_file, request);
4948 if (status < 0)
4949 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004950 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004951 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004952 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004953 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004954 request->fl_flags = fl_flags & ~FL_SLEEP;
4955 status = do_vfs_lock(request->fl_file, request);
4956 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004957 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004958 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004959 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004960 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004961 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004962 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004963 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004964 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4965 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004966out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004967 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004968out:
4969 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 return status;
4971}
4972
4973static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4974{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004975 struct nfs4_exception exception = {
4976 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004977 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004978 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 int err;
4980
4981 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004982 err = _nfs4_proc_setlk(state, cmd, request);
4983 if (err == -NFS4ERR_DENIED)
4984 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004986 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 } while (exception.retry);
4988 return err;
4989}
4990
4991static int
4992nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4993{
4994 struct nfs_open_context *ctx;
4995 struct nfs4_state *state;
4996 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4997 int status;
4998
4999 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005000 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 state = ctx->state;
5002
5003 if (request->fl_start < 0 || request->fl_end < 0)
5004 return -EINVAL;
5005
Trond Myklebustd9531262009-07-21 19:22:38 -04005006 if (IS_GETLK(cmd)) {
5007 if (state != NULL)
5008 return nfs4_proc_getlk(state, F_GETLK, request);
5009 return 0;
5010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011
5012 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5013 return -EINVAL;
5014
Trond Myklebustd9531262009-07-21 19:22:38 -04005015 if (request->fl_type == F_UNLCK) {
5016 if (state != NULL)
5017 return nfs4_proc_unlck(state, cmd, request);
5018 return 0;
5019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020
Trond Myklebustd9531262009-07-21 19:22:38 -04005021 if (state == NULL)
5022 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005023 /*
5024 * Don't rely on the VFS having checked the file open mode,
5025 * since it won't do this for flock() locks.
5026 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005027 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005028 case F_RDLCK:
5029 if (!(filp->f_mode & FMODE_READ))
5030 return -EBADF;
5031 break;
5032 case F_WRLCK:
5033 if (!(filp->f_mode & FMODE_WRITE))
5034 return -EBADF;
5035 }
5036
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 do {
5038 status = nfs4_proc_setlk(state, cmd, request);
5039 if ((status != -EAGAIN) || IS_SETLK(cmd))
5040 break;
5041 timeout = nfs4_set_lock_task_retry(timeout);
5042 status = -ERESTARTSYS;
5043 if (signalled())
5044 break;
5045 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 return status;
5047}
5048
Trond Myklebust888e6942005-11-04 15:38:11 -05005049int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5050{
5051 struct nfs_server *server = NFS_SERVER(state->inode);
5052 struct nfs4_exception exception = { };
5053 int err;
5054
5055 err = nfs4_set_lock_state(state, fl);
5056 if (err != 0)
5057 goto out;
5058 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005059 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005060 switch (err) {
5061 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005062 printk(KERN_ERR "NFS: %s: unhandled error "
5063 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005064 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005065 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005066 goto out;
5067 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005068 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005069 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005070 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005071 nfs4_schedule_lease_recovery(server->nfs_client);
5072 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005073 case -NFS4ERR_BADSESSION:
5074 case -NFS4ERR_BADSLOT:
5075 case -NFS4ERR_BAD_HIGH_SLOT:
5076 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5077 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005078 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005079 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005080 case -ERESTARTSYS:
5081 /*
5082 * The show must go on: exit, but mark the
5083 * stateid as needing recovery.
5084 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005085 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005086 case -NFS4ERR_ADMIN_REVOKED:
5087 case -NFS4ERR_BAD_STATEID:
5088 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005089 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005090 err = 0;
5091 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005092 case -EKEYEXPIRED:
5093 /*
5094 * User RPCSEC_GSS context has expired.
5095 * We cannot recover this stateid now, so
5096 * skip it and allow recovery thread to
5097 * proceed.
5098 */
5099 err = 0;
5100 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005101 case -ENOMEM:
5102 case -NFS4ERR_DENIED:
5103 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5104 err = 0;
5105 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005106 case -NFS4ERR_DELAY:
5107 break;
5108 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005109 err = nfs4_handle_exception(server, err, &exception);
5110 } while (exception.retry);
5111out:
5112 return err;
5113}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005114
Trond Myklebustcf470c32012-03-07 13:49:12 -05005115struct nfs_release_lockowner_data {
5116 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005117 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005118 struct nfs_release_lockowner_args args;
5119};
5120
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005121static void nfs4_release_lockowner_release(void *calldata)
5122{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005123 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005124 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005125 kfree(calldata);
5126}
5127
Trond Myklebust17280172012-03-11 13:11:00 -04005128static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005129 .rpc_release = nfs4_release_lockowner_release,
5130};
5131
Trond Myklebustcf470c32012-03-07 13:49:12 -05005132int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005133{
5134 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005135 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005136 struct rpc_message msg = {
5137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5138 };
5139
5140 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005141 return -EINVAL;
5142 data = kmalloc(sizeof(*data), GFP_NOFS);
5143 if (!data)
5144 return -ENOMEM;
5145 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005146 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005147 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5148 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5149 data->args.lock_owner.s_dev = server->s_dev;
5150 msg.rpc_argp = &data->args;
5151 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5152 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005153}
5154
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005155#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5156
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005157static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5158 const void *buf, size_t buflen,
5159 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005160{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005161 if (strcmp(key, "") != 0)
5162 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005163
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005164 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005165}
5166
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005167static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5168 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005169{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005170 if (strcmp(key, "") != 0)
5171 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005172
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005173 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005174}
5175
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005176static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5177 size_t list_len, const char *name,
5178 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005179{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005180 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005181
J. Bruce Fields096455a2006-03-20 23:23:42 -05005182 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5183 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005184
5185 if (list && len <= list_len)
5186 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005187 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005188}
5189
Andy Adamson533eb462011-06-13 18:25:56 -04005190/*
5191 * nfs_fhget will use either the mounted_on_fileid or the fileid
5192 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005193static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5194{
Andy Adamson533eb462011-06-13 18:25:56 -04005195 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5196 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5197 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005198 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005199 return;
5200
5201 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005202 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005203 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5204 fattr->nlink = 2;
5205}
5206
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005207static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5208 const struct qstr *name,
5209 struct nfs4_fs_locations *fs_locations,
5210 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005211{
5212 struct nfs_server *server = NFS_SERVER(dir);
5213 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005214 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005215 };
5216 struct nfs4_fs_locations_arg args = {
5217 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005218 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005219 .page = page,
5220 .bitmask = bitmask,
5221 };
Benny Halevy22958462009-04-01 09:22:02 -04005222 struct nfs4_fs_locations_res res = {
5223 .fs_locations = fs_locations,
5224 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005225 struct rpc_message msg = {
5226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5227 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005228 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005229 };
5230 int status;
5231
Harvey Harrison3110ff82008-05-02 13:42:44 -07005232 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005233
5234 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5235 * is not supported */
5236 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5237 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5238 else
5239 bitmask[0] |= FATTR4_WORD0_FILEID;
5240
Trond Myklebustc228fd32007-01-13 02:28:11 -05005241 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005242 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005243 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005244 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005245 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005246 return status;
5247}
5248
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005249int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5250 const struct qstr *name,
5251 struct nfs4_fs_locations *fs_locations,
5252 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005253{
5254 struct nfs4_exception exception = { };
5255 int err;
5256 do {
5257 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005258 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005259 &exception);
5260 } while (exception.retry);
5261 return err;
5262}
5263
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005264static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5265{
5266 int status;
5267 struct nfs4_secinfo_arg args = {
5268 .dir_fh = NFS_FH(dir),
5269 .name = name,
5270 };
5271 struct nfs4_secinfo_res res = {
5272 .flavors = flavors,
5273 };
5274 struct rpc_message msg = {
5275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5276 .rpc_argp = &args,
5277 .rpc_resp = &res,
5278 };
5279
5280 dprintk("NFS call secinfo %s\n", name->name);
5281 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5282 dprintk("NFS reply secinfo: %d\n", status);
5283 return status;
5284}
5285
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005286int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5287 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005288{
5289 struct nfs4_exception exception = { };
5290 int err;
5291 do {
5292 err = nfs4_handle_exception(NFS_SERVER(dir),
5293 _nfs4_proc_secinfo(dir, name, flavors),
5294 &exception);
5295 } while (exception.retry);
5296 return err;
5297}
5298
Andy Adamson557134a2009-04-01 09:21:53 -04005299#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005300/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005301 * Check the exchange flags returned by the server for invalid flags, having
5302 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5303 * DS flags set.
5304 */
5305static int nfs4_check_cl_exchange_flags(u32 flags)
5306{
5307 if (flags & ~EXCHGID4_FLAG_MASK_R)
5308 goto out_inval;
5309 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5310 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5311 goto out_inval;
5312 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5313 goto out_inval;
5314 return NFS_OK;
5315out_inval:
5316 return -NFS4ERR_INVAL;
5317}
5318
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005319static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005320nfs41_same_server_scope(struct nfs41_server_scope *a,
5321 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005322{
5323 if (a->server_scope_sz == b->server_scope_sz &&
5324 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5325 return true;
5326
5327 return false;
5328}
5329
Andy Adamson357f54d2010-12-14 10:11:57 -05005330/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005331 * nfs4_proc_bind_conn_to_session()
5332 *
5333 * The 4.1 client currently uses the same TCP connection for the
5334 * fore and backchannel.
5335 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005336int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005337{
5338 int status;
5339 struct nfs41_bind_conn_to_session_res res;
5340 struct rpc_message msg = {
5341 .rpc_proc =
5342 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5343 .rpc_argp = clp,
5344 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005345 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005346 };
5347
5348 dprintk("--> %s\n", __func__);
5349 BUG_ON(clp == NULL);
5350
5351 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5352 if (unlikely(res.session == NULL)) {
5353 status = -ENOMEM;
5354 goto out;
5355 }
5356
5357 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5358 if (status == 0) {
5359 if (memcmp(res.session->sess_id.data,
5360 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5361 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5362 status = -EIO;
5363 goto out_session;
5364 }
5365 if (res.dir != NFS4_CDFS4_BOTH) {
5366 dprintk("NFS: %s: Unexpected direction from server\n",
5367 __func__);
5368 status = -EIO;
5369 goto out_session;
5370 }
5371 if (res.use_conn_in_rdma_mode) {
5372 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5373 __func__);
5374 status = -EIO;
5375 goto out_session;
5376 }
5377 }
5378out_session:
5379 kfree(res.session);
5380out:
5381 dprintk("<-- %s status= %d\n", __func__, status);
5382 return status;
5383}
5384
5385/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005386 * nfs4_proc_exchange_id()
5387 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005388 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5389 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005390 * Since the clientid has expired, all compounds using sessions
5391 * associated with the stale clientid will be returning
5392 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5393 * be in some phase of session reset.
5394 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005395int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005396{
5397 nfs4_verifier verifier;
5398 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005399 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005400 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005401 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005402 };
5403 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005404 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005405 };
5406 int status;
5407 struct rpc_message msg = {
5408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5409 .rpc_argp = &args,
5410 .rpc_resp = &res,
5411 .rpc_cred = cred,
5412 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005413
Chuck Leverf0920752012-05-21 22:45:41 -04005414 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005415 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5416 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005417 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5418 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5419 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005420
Chuck Leveracdeb692012-05-21 22:46:16 -04005421 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005422 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005423 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005424 status = -ENOMEM;
5425 goto out;
5426 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005427
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005428 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005429 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005430 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005431 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005432 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005433 }
5434
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005435 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005436 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005437 status = -ENOMEM;
5438 goto out_server_scope;
5439 }
5440
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005441 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005442 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005443 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005444
Chuck Lever177313f2012-05-21 22:44:58 -04005445 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005446 clp->cl_clientid = res.clientid;
5447 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5448 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5449 clp->cl_seqid = res.seqid;
5450
Chuck Leveracdeb692012-05-21 22:46:16 -04005451 kfree(clp->cl_serverowner);
5452 clp->cl_serverowner = res.server_owner;
5453 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005454
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005455 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005456 kfree(clp->cl_implid);
5457 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005458
Chuck Lever177313f2012-05-21 22:44:58 -04005459 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005460 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005461 res.server_scope)) {
5462 dprintk("%s: server_scope mismatch detected\n",
5463 __func__);
5464 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005465 kfree(clp->cl_serverscope);
5466 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005467 }
5468
Chuck Lever177313f2012-05-21 22:44:58 -04005469 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005470 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005471 goto out;
5472 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005473 } else
5474 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005475
Chuck Leveracdeb692012-05-21 22:46:16 -04005476out_server_owner:
5477 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005478out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005479 kfree(res.server_scope);
5480out:
Chuck Lever177313f2012-05-21 22:44:58 -04005481 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005482 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005483 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005484 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005485 clp->cl_implid->date.seconds,
5486 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005487 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005488 return status;
5489}
5490
Trond Myklebust66245532012-05-25 17:18:09 -04005491static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5492 struct rpc_cred *cred)
5493{
5494 struct rpc_message msg = {
5495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5496 .rpc_argp = clp,
5497 .rpc_cred = cred,
5498 };
5499 int status;
5500
5501 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5502 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005503 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005504 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5505 return status;
5506}
5507
5508static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5509 struct rpc_cred *cred)
5510{
5511 unsigned int loop;
5512 int ret;
5513
5514 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5515 ret = _nfs4_proc_destroy_clientid(clp, cred);
5516 switch (ret) {
5517 case -NFS4ERR_DELAY:
5518 case -NFS4ERR_CLIENTID_BUSY:
5519 ssleep(1);
5520 break;
5521 default:
5522 return ret;
5523 }
5524 }
5525 return 0;
5526}
5527
5528int nfs4_destroy_clientid(struct nfs_client *clp)
5529{
5530 struct rpc_cred *cred;
5531 int ret = 0;
5532
5533 if (clp->cl_mvops->minor_version < 1)
5534 goto out;
5535 if (clp->cl_exchange_flags == 0)
5536 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005537 if (clp->cl_preserve_clid)
5538 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005539 cred = nfs4_get_exchange_id_cred(clp);
5540 ret = nfs4_proc_destroy_clientid(clp, cred);
5541 if (cred)
5542 put_rpccred(cred);
5543 switch (ret) {
5544 case 0:
5545 case -NFS4ERR_STALE_CLIENTID:
5546 clp->cl_exchange_flags = 0;
5547 }
5548out:
5549 return ret;
5550}
5551
Andy Adamson2050f0c2009-04-01 09:22:30 -04005552struct nfs4_get_lease_time_data {
5553 struct nfs4_get_lease_time_args *args;
5554 struct nfs4_get_lease_time_res *res;
5555 struct nfs_client *clp;
5556};
5557
5558static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5559 void *calldata)
5560{
5561 int ret;
5562 struct nfs4_get_lease_time_data *data =
5563 (struct nfs4_get_lease_time_data *)calldata;
5564
5565 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005566 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005567 /* just setup sequence, do not trigger session recovery
5568 since we're invoked within one */
5569 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005570 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005571 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005572
5573 BUG_ON(ret == -EAGAIN);
5574 rpc_call_start(task);
5575 dprintk("<-- %s\n", __func__);
5576}
5577
5578/*
5579 * Called from nfs4_state_manager thread for session setup, so don't recover
5580 * from sequence operation or clientid errors.
5581 */
5582static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5583{
5584 struct nfs4_get_lease_time_data *data =
5585 (struct nfs4_get_lease_time_data *)calldata;
5586
5587 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005588 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5589 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005590 switch (task->tk_status) {
5591 case -NFS4ERR_DELAY:
5592 case -NFS4ERR_GRACE:
5593 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5594 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5595 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005596 /* fall through */
5597 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005598 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005599 return;
5600 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005601 dprintk("<-- %s\n", __func__);
5602}
5603
Trond Myklebust17280172012-03-11 13:11:00 -04005604static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005605 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5606 .rpc_call_done = nfs4_get_lease_time_done,
5607};
5608
5609int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5610{
5611 struct rpc_task *task;
5612 struct nfs4_get_lease_time_args args;
5613 struct nfs4_get_lease_time_res res = {
5614 .lr_fsinfo = fsinfo,
5615 };
5616 struct nfs4_get_lease_time_data data = {
5617 .args = &args,
5618 .res = &res,
5619 .clp = clp,
5620 };
5621 struct rpc_message msg = {
5622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5623 .rpc_argp = &args,
5624 .rpc_resp = &res,
5625 };
5626 struct rpc_task_setup task_setup = {
5627 .rpc_client = clp->cl_rpcclient,
5628 .rpc_message = &msg,
5629 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005630 .callback_data = &data,
5631 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005632 };
5633 int status;
5634
Trond Myklebust9d12b212012-01-17 22:04:25 -05005635 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005636 dprintk("--> %s\n", __func__);
5637 task = rpc_run_task(&task_setup);
5638
5639 if (IS_ERR(task))
5640 status = PTR_ERR(task);
5641 else {
5642 status = task->tk_status;
5643 rpc_put_task(task);
5644 }
5645 dprintk("<-- %s return %d\n", __func__, status);
5646
5647 return status;
5648}
5649
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005650static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5651{
5652 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5653}
5654
5655static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5656 struct nfs4_slot *new,
5657 u32 max_slots,
5658 u32 ivalue)
5659{
5660 struct nfs4_slot *old = NULL;
5661 u32 i;
5662
5663 spin_lock(&tbl->slot_tbl_lock);
5664 if (new) {
5665 old = tbl->slots;
5666 tbl->slots = new;
5667 tbl->max_slots = max_slots;
5668 }
5669 tbl->highest_used_slotid = -1; /* no slot is currently used */
5670 for (i = 0; i < tbl->max_slots; i++)
5671 tbl->slots[i].seq_nr = ivalue;
5672 spin_unlock(&tbl->slot_tbl_lock);
5673 kfree(old);
5674}
5675
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005676/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005677 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005678 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005679static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5680 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005681{
Andy Adamson104aeba2010-01-14 17:45:10 -05005682 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005683 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005684
Andy Adamson104aeba2010-01-14 17:45:10 -05005685 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5686 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005687
Andy Adamson104aeba2010-01-14 17:45:10 -05005688 /* Does the newly negotiated max_reqs match the existing slot table? */
5689 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005690 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005691 if (!new)
5692 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005693 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005694 ret = 0;
5695
5696 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005697 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5698 tbl, tbl->slots, tbl->max_slots);
5699out:
5700 dprintk("<-- %s: return %d\n", __func__, ret);
5701 return ret;
5702}
5703
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005704/* Destroy the slot table */
5705static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5706{
5707 if (session->fc_slot_table.slots != NULL) {
5708 kfree(session->fc_slot_table.slots);
5709 session->fc_slot_table.slots = NULL;
5710 }
5711 if (session->bc_slot_table.slots != NULL) {
5712 kfree(session->bc_slot_table.slots);
5713 session->bc_slot_table.slots = NULL;
5714 }
5715 return;
5716}
5717
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005718/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005719 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005720 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005721static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005722{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005723 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005724 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005725
Andy Adamsonaacd5532011-11-09 13:58:21 -05005726 dprintk("--> %s\n", __func__);
5727 /* Fore channel */
5728 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005729 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5730 if (status) /* -ENOMEM */
5731 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005732 /* Back channel */
5733 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005734 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5735 if (status && tbl->slots == NULL)
5736 /* Fore and back channel share a connection so get
5737 * both slot tables or neither */
5738 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005739 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005740}
5741
5742struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5743{
5744 struct nfs4_session *session;
5745 struct nfs4_slot_table *tbl;
5746
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005747 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005748 if (!session)
5749 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005750
Andy Adamson557134a2009-04-01 09:21:53 -04005751 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005752 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005753 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005754 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005755 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005756
5757 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005758 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005759 spin_lock_init(&tbl->slot_tbl_lock);
5760 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005761 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005762
Trond Myklebust1055d762010-06-16 09:52:27 -04005763 session->session_state = 1<<NFS4_SESSION_INITING;
5764
Andy Adamson557134a2009-04-01 09:21:53 -04005765 session->clp = clp;
5766 return session;
5767}
5768
5769void nfs4_destroy_session(struct nfs4_session *session)
5770{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005771 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005772 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005773
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005774 cred = nfs4_get_exchange_id_cred(session->clp);
5775 nfs4_proc_destroy_session(session, cred);
5776 if (cred)
5777 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005778
5779 rcu_read_lock();
5780 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5781 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005782 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005783 __func__, xprt);
5784 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005785 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005786 kfree(session);
5787}
5788
Andy Adamsonfc931582009-04-01 09:22:31 -04005789/*
5790 * Initialize the values to be used by the client in CREATE_SESSION
5791 * If nfs4_init_session set the fore channel request and response sizes,
5792 * use them.
5793 *
5794 * Set the back channel max_resp_sz_cached to zero to force the client to
5795 * always set csa_cachethis to FALSE because the current implementation
5796 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5797 */
5798static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5799{
5800 struct nfs4_session *session = args->client->cl_session;
5801 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5802 mxresp_sz = session->fc_attrs.max_resp_sz;
5803
5804 if (mxrqst_sz == 0)
5805 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5806 if (mxresp_sz == 0)
5807 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5808 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005809 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5810 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005811 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005812 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005813
5814 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005815 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005816 __func__,
5817 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005818 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005819
5820 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005821 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5822 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5823 args->bc_attrs.max_resp_sz_cached = 0;
5824 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5825 args->bc_attrs.max_reqs = 1;
5826
5827 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5828 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5829 __func__,
5830 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5831 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5832 args->bc_attrs.max_reqs);
5833}
5834
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005835static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005836{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005837 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5838 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5839
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005840 if (rcvd->max_resp_sz > sent->max_resp_sz)
5841 return -EINVAL;
5842 /*
5843 * Our requested max_ops is the minimum we need; we're not
5844 * prepared to break up compounds into smaller pieces than that.
5845 * So, no point even trying to continue if the server won't
5846 * cooperate:
5847 */
5848 if (rcvd->max_ops < sent->max_ops)
5849 return -EINVAL;
5850 if (rcvd->max_reqs == 0)
5851 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005852 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5853 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005854 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005855}
5856
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005857static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5858{
5859 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5860 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005861
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005862 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5863 return -EINVAL;
5864 if (rcvd->max_resp_sz < sent->max_resp_sz)
5865 return -EINVAL;
5866 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5867 return -EINVAL;
5868 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005869 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005870 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005871 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005872 return -EINVAL;
5873 return 0;
5874}
Andy Adamson8d353012009-04-01 09:22:32 -04005875
Andy Adamson8d353012009-04-01 09:22:32 -04005876static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5877 struct nfs4_session *session)
5878{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005879 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005880
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005881 ret = nfs4_verify_fore_channel_attrs(args, session);
5882 if (ret)
5883 return ret;
5884 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005885}
5886
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005887static int _nfs4_proc_create_session(struct nfs_client *clp,
5888 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005889{
5890 struct nfs4_session *session = clp->cl_session;
5891 struct nfs41_create_session_args args = {
5892 .client = clp,
5893 .cb_program = NFS4_CALLBACK,
5894 };
5895 struct nfs41_create_session_res res = {
5896 .client = clp,
5897 };
5898 struct rpc_message msg = {
5899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5900 .rpc_argp = &args,
5901 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005902 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005903 };
5904 int status;
5905
5906 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005907 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005908
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005909 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005910
Andy Adamson8d353012009-04-01 09:22:32 -04005911 if (!status)
5912 /* Verify the session's negotiated channel_attrs values */
5913 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005914 if (!status) {
5915 /* Increment the clientid slot sequence id */
5916 clp->cl_seqid++;
5917 }
5918
5919 return status;
5920}
5921
5922/*
5923 * Issues a CREATE_SESSION operation to the server.
5924 * It is the responsibility of the caller to verify the session is
5925 * expired before calling this routine.
5926 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005927int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005928{
5929 int status;
5930 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005931 struct nfs4_session *session = clp->cl_session;
5932
5933 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5934
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005935 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005936 if (status)
5937 goto out;
5938
Andy Adamsonaacd5532011-11-09 13:58:21 -05005939 /* Init or reset the session slot tables */
5940 status = nfs4_setup_session_slot_tables(session);
5941 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005942 if (status)
5943 goto out;
5944
5945 ptr = (unsigned *)&session->sess_id.data[0];
5946 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5947 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005948out:
5949 dprintk("<-- %s\n", __func__);
5950 return status;
5951}
5952
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005953/*
5954 * Issue the over-the-wire RPC DESTROY_SESSION.
5955 * The caller must serialize access to this routine.
5956 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005957int nfs4_proc_destroy_session(struct nfs4_session *session,
5958 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005959{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005960 struct rpc_message msg = {
5961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5962 .rpc_argp = session,
5963 .rpc_cred = cred,
5964 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005965 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005966
5967 dprintk("--> nfs4_proc_destroy_session\n");
5968
5969 /* session is still being setup */
5970 if (session->clp->cl_cons_state != NFS_CS_READY)
5971 return status;
5972
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005973 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005974
5975 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005976 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005977 "Session has been destroyed regardless...\n", status);
5978
5979 dprintk("<-- nfs4_proc_destroy_session\n");
5980 return status;
5981}
5982
Trond Myklebust7b38c362012-05-23 13:23:31 -04005983/*
5984 * With sessions, the client is not marked ready until after a
5985 * successful EXCHANGE_ID and CREATE_SESSION.
5986 *
5987 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5988 * other versions of NFS can be tried.
5989 */
5990static int nfs41_check_session_ready(struct nfs_client *clp)
5991{
5992 int ret;
5993
5994 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5995 ret = nfs4_client_recover_expired_lease(clp);
5996 if (ret)
5997 return ret;
5998 }
5999 if (clp->cl_cons_state < NFS_CS_READY)
6000 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006001 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006002 return 0;
6003}
6004
Trond Myklebustfccba802009-07-21 16:48:07 -04006005int nfs4_init_session(struct nfs_server *server)
6006{
6007 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006008 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05006009 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04006010
6011 if (!nfs4_has_session(clp))
6012 return 0;
6013
Trond Myklebust1055d762010-06-16 09:52:27 -04006014 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006015 spin_lock(&clp->cl_lock);
6016 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04006017
Trond Myklebust7b38c362012-05-23 13:23:31 -04006018 rsize = server->rsize;
6019 if (rsize == 0)
6020 rsize = NFS_MAX_FILE_IO_SIZE;
6021 wsize = server->wsize;
6022 if (wsize == 0)
6023 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05006024
Trond Myklebust7b38c362012-05-23 13:23:31 -04006025 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
6026 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
6027 }
6028 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006029
Trond Myklebust7b38c362012-05-23 13:23:31 -04006030 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006031}
6032
Trond Myklebust7b38c362012-05-23 13:23:31 -04006033int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006034{
6035 struct nfs4_session *session = clp->cl_session;
6036 int ret;
6037
Trond Myklebust7b38c362012-05-23 13:23:31 -04006038 spin_lock(&clp->cl_lock);
6039 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6040 /*
6041 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6042 * DS lease to be equal to the MDS lease.
6043 */
6044 clp->cl_lease_time = lease_time;
6045 clp->cl_last_renewal = jiffies;
6046 }
6047 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006048
Trond Myklebust7b38c362012-05-23 13:23:31 -04006049 ret = nfs41_check_session_ready(clp);
6050 if (ret)
6051 return ret;
6052 /* Test for the DS role */
6053 if (!is_ds_client(clp))
6054 return -ENODEV;
6055 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006056}
6057EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6058
6059
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006060/*
6061 * Renew the cl_session lease.
6062 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006063struct nfs4_sequence_data {
6064 struct nfs_client *clp;
6065 struct nfs4_sequence_args args;
6066 struct nfs4_sequence_res res;
6067};
6068
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006069static void nfs41_sequence_release(void *data)
6070{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006071 struct nfs4_sequence_data *calldata = data;
6072 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006073
Alexandros Batsakis71358402010-02-05 03:45:05 -08006074 if (atomic_read(&clp->cl_count) > 1)
6075 nfs4_schedule_state_renewal(clp);
6076 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006077 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006078}
6079
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006080static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6081{
6082 switch(task->tk_status) {
6083 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006084 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6085 return -EAGAIN;
6086 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006087 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006088 }
6089 return 0;
6090}
6091
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006092static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006093{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006094 struct nfs4_sequence_data *calldata = data;
6095 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006096
Trond Myklebust14516c32010-07-31 14:29:06 -04006097 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6098 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006099
6100 if (task->tk_status < 0) {
6101 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006102 if (atomic_read(&clp->cl_count) == 1)
6103 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006104
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006105 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6106 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006107 return;
6108 }
6109 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006110 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006111out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006112 dprintk("<-- %s\n", __func__);
6113}
6114
6115static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6116{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006117 struct nfs4_sequence_data *calldata = data;
6118 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006119 struct nfs4_sequence_args *args;
6120 struct nfs4_sequence_res *res;
6121
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006122 args = task->tk_msg.rpc_argp;
6123 res = task->tk_msg.rpc_resp;
6124
Trond Myklebust9d12b212012-01-17 22:04:25 -05006125 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006126 return;
6127 rpc_call_start(task);
6128}
6129
6130static const struct rpc_call_ops nfs41_sequence_ops = {
6131 .rpc_call_done = nfs41_sequence_call_done,
6132 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006133 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006134};
6135
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006136static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006137{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006138 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006139 struct rpc_message msg = {
6140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6141 .rpc_cred = cred,
6142 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006143 struct rpc_task_setup task_setup_data = {
6144 .rpc_client = clp->cl_rpcclient,
6145 .rpc_message = &msg,
6146 .callback_ops = &nfs41_sequence_ops,
6147 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6148 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006149
Alexandros Batsakis71358402010-02-05 03:45:05 -08006150 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006151 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006152 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006153 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006154 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006155 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006156 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006157 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006158 msg.rpc_argp = &calldata->args;
6159 msg.rpc_resp = &calldata->res;
6160 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006161 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006162
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006163 return rpc_run_task(&task_setup_data);
6164}
6165
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006166static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006167{
6168 struct rpc_task *task;
6169 int ret = 0;
6170
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006171 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6172 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006173 task = _nfs41_proc_sequence(clp, cred);
6174 if (IS_ERR(task))
6175 ret = PTR_ERR(task);
6176 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006177 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006178 dprintk("<-- %s status=%d\n", __func__, ret);
6179 return ret;
6180}
6181
6182static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6183{
6184 struct rpc_task *task;
6185 int ret;
6186
6187 task = _nfs41_proc_sequence(clp, cred);
6188 if (IS_ERR(task)) {
6189 ret = PTR_ERR(task);
6190 goto out;
6191 }
6192 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006193 if (!ret) {
6194 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6195
6196 if (task->tk_status == 0)
6197 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006198 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006199 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006200 rpc_put_task(task);
6201out:
6202 dprintk("<-- %s status=%d\n", __func__, ret);
6203 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006204}
6205
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006206struct nfs4_reclaim_complete_data {
6207 struct nfs_client *clp;
6208 struct nfs41_reclaim_complete_args arg;
6209 struct nfs41_reclaim_complete_res res;
6210};
6211
6212static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6213{
6214 struct nfs4_reclaim_complete_data *calldata = data;
6215
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006216 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006217 if (nfs41_setup_sequence(calldata->clp->cl_session,
6218 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006219 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006220 return;
6221
6222 rpc_call_start(task);
6223}
6224
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006225static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6226{
6227 switch(task->tk_status) {
6228 case 0:
6229 case -NFS4ERR_COMPLETE_ALREADY:
6230 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6231 break;
6232 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006233 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006234 /* fall through */
6235 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006236 return -EAGAIN;
6237 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006238 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006239 }
6240 return 0;
6241}
6242
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006243static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6244{
6245 struct nfs4_reclaim_complete_data *calldata = data;
6246 struct nfs_client *clp = calldata->clp;
6247 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6248
6249 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006250 if (!nfs41_sequence_done(task, res))
6251 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006252
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006253 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6254 rpc_restart_call_prepare(task);
6255 return;
6256 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006257 dprintk("<-- %s\n", __func__);
6258}
6259
6260static void nfs4_free_reclaim_complete_data(void *data)
6261{
6262 struct nfs4_reclaim_complete_data *calldata = data;
6263
6264 kfree(calldata);
6265}
6266
6267static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6268 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6269 .rpc_call_done = nfs4_reclaim_complete_done,
6270 .rpc_release = nfs4_free_reclaim_complete_data,
6271};
6272
6273/*
6274 * Issue a global reclaim complete.
6275 */
6276static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6277{
6278 struct nfs4_reclaim_complete_data *calldata;
6279 struct rpc_task *task;
6280 struct rpc_message msg = {
6281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6282 };
6283 struct rpc_task_setup task_setup_data = {
6284 .rpc_client = clp->cl_rpcclient,
6285 .rpc_message = &msg,
6286 .callback_ops = &nfs4_reclaim_complete_call_ops,
6287 .flags = RPC_TASK_ASYNC,
6288 };
6289 int status = -ENOMEM;
6290
6291 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006292 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006293 if (calldata == NULL)
6294 goto out;
6295 calldata->clp = clp;
6296 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006297
Trond Myklebust9d12b212012-01-17 22:04:25 -05006298 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006299 msg.rpc_argp = &calldata->arg;
6300 msg.rpc_resp = &calldata->res;
6301 task_setup_data.callback_data = calldata;
6302 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006303 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006304 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006305 goto out;
6306 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006307 status = nfs4_wait_for_completion_rpc_task(task);
6308 if (status == 0)
6309 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006310 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006311 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006312out:
6313 dprintk("<-- %s status=%d\n", __func__, status);
6314 return status;
6315}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006316
6317static void
6318nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6319{
6320 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006321 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006322
6323 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006324 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6325 * right now covering the LAYOUTGET we are about to send.
6326 * However, that is not so catastrophic, and there seems
6327 * to be no way to prevent it completely.
6328 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006329 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006330 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006331 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006332 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6333 NFS_I(lgp->args.inode)->layout,
6334 lgp->args.ctx->state)) {
6335 rpc_exit(task, NFS4_OK);
6336 return;
6337 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006338 rpc_call_start(task);
6339}
6340
6341static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6342{
6343 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006344 struct inode *inode = lgp->args.inode;
6345 struct nfs_server *server = NFS_SERVER(inode);
6346 struct pnfs_layout_hdr *lo;
6347 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006348
6349 dprintk("--> %s\n", __func__);
6350
6351 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006352 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006353
6354 switch (task->tk_status) {
6355 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006356 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006357 case -NFS4ERR_LAYOUTTRYLATER:
6358 case -NFS4ERR_RECALLCONFLICT:
6359 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006360 break;
6361 case -NFS4ERR_EXPIRED:
6362 case -NFS4ERR_BAD_STATEID:
6363 spin_lock(&inode->i_lock);
6364 lo = NFS_I(inode)->layout;
6365 if (!lo || list_empty(&lo->plh_segs)) {
6366 spin_unlock(&inode->i_lock);
6367 /* If the open stateid was bad, then recover it. */
6368 state = lgp->args.ctx->state;
6369 } else {
6370 LIST_HEAD(head);
6371
6372 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6373 spin_unlock(&inode->i_lock);
6374 /* Mark the bad layout state as invalid, then
6375 * retry using the open stateid. */
6376 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006377 }
6378 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006379 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6380 rpc_restart_call_prepare(task);
6381out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006382 dprintk("<-- %s\n", __func__);
6383}
6384
Idan Kedar85541162012-08-02 11:47:10 +03006385static size_t max_response_pages(struct nfs_server *server)
6386{
6387 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6388 return nfs_page_array_len(0, max_resp_sz);
6389}
6390
6391static void nfs4_free_pages(struct page **pages, size_t size)
6392{
6393 int i;
6394
6395 if (!pages)
6396 return;
6397
6398 for (i = 0; i < size; i++) {
6399 if (!pages[i])
6400 break;
6401 __free_page(pages[i]);
6402 }
6403 kfree(pages);
6404}
6405
6406static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6407{
6408 struct page **pages;
6409 int i;
6410
6411 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6412 if (!pages) {
6413 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6414 return NULL;
6415 }
6416
6417 for (i = 0; i < size; i++) {
6418 pages[i] = alloc_page(gfp_flags);
6419 if (!pages[i]) {
6420 dprintk("%s: failed to allocate page\n", __func__);
6421 nfs4_free_pages(pages, size);
6422 return NULL;
6423 }
6424 }
6425
6426 return pages;
6427}
6428
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006429static void nfs4_layoutget_release(void *calldata)
6430{
6431 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006432 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6433 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006434
6435 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006436 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006437 put_nfs_open_context(lgp->args.ctx);
6438 kfree(calldata);
6439 dprintk("<-- %s\n", __func__);
6440}
6441
6442static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6443 .rpc_call_prepare = nfs4_layoutget_prepare,
6444 .rpc_call_done = nfs4_layoutget_done,
6445 .rpc_release = nfs4_layoutget_release,
6446};
6447
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006448struct pnfs_layout_segment *
6449nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006450{
6451 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006452 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006453 struct rpc_task *task;
6454 struct rpc_message msg = {
6455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6456 .rpc_argp = &lgp->args,
6457 .rpc_resp = &lgp->res,
6458 };
6459 struct rpc_task_setup task_setup_data = {
6460 .rpc_client = server->client,
6461 .rpc_message = &msg,
6462 .callback_ops = &nfs4_layoutget_call_ops,
6463 .callback_data = lgp,
6464 .flags = RPC_TASK_ASYNC,
6465 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006466 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006467 int status = 0;
6468
6469 dprintk("--> %s\n", __func__);
6470
Idan Kedar85541162012-08-02 11:47:10 +03006471 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6472 if (!lgp->args.layout.pages) {
6473 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006474 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006475 }
6476 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6477
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006478 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006479 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006480 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006481 task = rpc_run_task(&task_setup_data);
6482 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006483 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006484 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006485 if (status == 0)
6486 status = task->tk_status;
6487 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006488 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006489 rpc_put_task(task);
6490 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006491 if (status)
6492 return ERR_PTR(status);
6493 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006494}
6495
Benny Halevycbe82602011-05-22 19:52:37 +03006496static void
6497nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6498{
6499 struct nfs4_layoutreturn *lrp = calldata;
6500
6501 dprintk("--> %s\n", __func__);
6502 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006503 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006504 return;
6505 rpc_call_start(task);
6506}
6507
6508static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6509{
6510 struct nfs4_layoutreturn *lrp = calldata;
6511 struct nfs_server *server;
6512
6513 dprintk("--> %s\n", __func__);
6514
6515 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6516 return;
6517
6518 server = NFS_SERVER(lrp->args.inode);
6519 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006520 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006521 return;
6522 }
Benny Halevycbe82602011-05-22 19:52:37 +03006523 dprintk("<-- %s\n", __func__);
6524}
6525
6526static void nfs4_layoutreturn_release(void *calldata)
6527{
6528 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006529 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006530
6531 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006532 spin_lock(&lo->plh_inode->i_lock);
6533 if (lrp->res.lrs_present)
6534 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6535 lo->plh_block_lgets--;
6536 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006537 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006538 kfree(calldata);
6539 dprintk("<-- %s\n", __func__);
6540}
6541
6542static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6543 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6544 .rpc_call_done = nfs4_layoutreturn_done,
6545 .rpc_release = nfs4_layoutreturn_release,
6546};
6547
6548int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6549{
6550 struct rpc_task *task;
6551 struct rpc_message msg = {
6552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6553 .rpc_argp = &lrp->args,
6554 .rpc_resp = &lrp->res,
6555 };
6556 struct rpc_task_setup task_setup_data = {
6557 .rpc_client = lrp->clp->cl_rpcclient,
6558 .rpc_message = &msg,
6559 .callback_ops = &nfs4_layoutreturn_call_ops,
6560 .callback_data = lrp,
6561 };
6562 int status;
6563
6564 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006565 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006566 task = rpc_run_task(&task_setup_data);
6567 if (IS_ERR(task))
6568 return PTR_ERR(task);
6569 status = task->tk_status;
6570 dprintk("<-- %s status=%d\n", __func__, status);
6571 rpc_put_task(task);
6572 return status;
6573}
6574
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006575/*
6576 * Retrieve the list of Data Server devices from the MDS.
6577 */
6578static int _nfs4_getdevicelist(struct nfs_server *server,
6579 const struct nfs_fh *fh,
6580 struct pnfs_devicelist *devlist)
6581{
6582 struct nfs4_getdevicelist_args args = {
6583 .fh = fh,
6584 .layoutclass = server->pnfs_curr_ld->id,
6585 };
6586 struct nfs4_getdevicelist_res res = {
6587 .devlist = devlist,
6588 };
6589 struct rpc_message msg = {
6590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6591 .rpc_argp = &args,
6592 .rpc_resp = &res,
6593 };
6594 int status;
6595
6596 dprintk("--> %s\n", __func__);
6597 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6598 &res.seq_res, 0);
6599 dprintk("<-- %s status=%d\n", __func__, status);
6600 return status;
6601}
6602
6603int nfs4_proc_getdevicelist(struct nfs_server *server,
6604 const struct nfs_fh *fh,
6605 struct pnfs_devicelist *devlist)
6606{
6607 struct nfs4_exception exception = { };
6608 int err;
6609
6610 do {
6611 err = nfs4_handle_exception(server,
6612 _nfs4_getdevicelist(server, fh, devlist),
6613 &exception);
6614 } while (exception.retry);
6615
6616 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6617 err, devlist->num_devs);
6618
6619 return err;
6620}
6621EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6622
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006623static int
6624_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6625{
6626 struct nfs4_getdeviceinfo_args args = {
6627 .pdev = pdev,
6628 };
6629 struct nfs4_getdeviceinfo_res res = {
6630 .pdev = pdev,
6631 };
6632 struct rpc_message msg = {
6633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6634 .rpc_argp = &args,
6635 .rpc_resp = &res,
6636 };
6637 int status;
6638
6639 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006640 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006641 dprintk("<-- %s status=%d\n", __func__, status);
6642
6643 return status;
6644}
6645
6646int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6647{
6648 struct nfs4_exception exception = { };
6649 int err;
6650
6651 do {
6652 err = nfs4_handle_exception(server,
6653 _nfs4_proc_getdeviceinfo(server, pdev),
6654 &exception);
6655 } while (exception.retry);
6656 return err;
6657}
6658EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6659
Andy Adamson863a3c62011-03-23 13:27:54 +00006660static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6661{
6662 struct nfs4_layoutcommit_data *data = calldata;
6663 struct nfs_server *server = NFS_SERVER(data->args.inode);
6664
6665 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006666 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006667 return;
6668 rpc_call_start(task);
6669}
6670
6671static void
6672nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6673{
6674 struct nfs4_layoutcommit_data *data = calldata;
6675 struct nfs_server *server = NFS_SERVER(data->args.inode);
6676
6677 if (!nfs4_sequence_done(task, &data->res.seq_res))
6678 return;
6679
6680 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006681 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6682 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6683 case -NFS4ERR_BADLAYOUT: /* no layout */
6684 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006685 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006686 break;
6687 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006688 nfs_post_op_update_inode_force_wcc(data->args.inode,
6689 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006690 break;
6691 default:
6692 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6693 rpc_restart_call_prepare(task);
6694 return;
6695 }
6696 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006697}
6698
6699static void nfs4_layoutcommit_release(void *calldata)
6700{
6701 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006702 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006703 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006704
Andy Adamsondb29c082011-07-30 20:52:38 -04006705 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006706 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006707 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6708 list_del_init(&lseg->pls_lc_list);
6709 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6710 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006711 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006712 }
Peng Tao92407e72011-10-23 20:21:17 -07006713
6714 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6715 smp_mb__after_clear_bit();
6716 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6717
Andy Adamson863a3c62011-03-23 13:27:54 +00006718 put_rpccred(data->cred);
6719 kfree(data);
6720}
6721
6722static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6723 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6724 .rpc_call_done = nfs4_layoutcommit_done,
6725 .rpc_release = nfs4_layoutcommit_release,
6726};
6727
6728int
Andy Adamsonef311532011-03-12 02:58:10 -05006729nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006730{
6731 struct rpc_message msg = {
6732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6733 .rpc_argp = &data->args,
6734 .rpc_resp = &data->res,
6735 .rpc_cred = data->cred,
6736 };
6737 struct rpc_task_setup task_setup_data = {
6738 .task = &data->task,
6739 .rpc_client = NFS_CLIENT(data->args.inode),
6740 .rpc_message = &msg,
6741 .callback_ops = &nfs4_layoutcommit_ops,
6742 .callback_data = data,
6743 .flags = RPC_TASK_ASYNC,
6744 };
6745 struct rpc_task *task;
6746 int status = 0;
6747
6748 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6749 "lbw: %llu inode %lu\n",
6750 data->task.tk_pid, sync,
6751 data->args.lastbytewritten,
6752 data->args.inode->i_ino);
6753
Trond Myklebust9d12b212012-01-17 22:04:25 -05006754 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006755 task = rpc_run_task(&task_setup_data);
6756 if (IS_ERR(task))
6757 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006758 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006759 goto out;
6760 status = nfs4_wait_for_completion_rpc_task(task);
6761 if (status != 0)
6762 goto out;
6763 status = task->tk_status;
6764out:
6765 dprintk("%s: status %d\n", __func__, status);
6766 rpc_put_task(task);
6767 return status;
6768}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006769
6770static int
6771_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6772 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6773{
6774 struct nfs41_secinfo_no_name_args args = {
6775 .style = SECINFO_STYLE_CURRENT_FH,
6776 };
6777 struct nfs4_secinfo_res res = {
6778 .flavors = flavors,
6779 };
6780 struct rpc_message msg = {
6781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6782 .rpc_argp = &args,
6783 .rpc_resp = &res,
6784 };
6785 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6786}
6787
6788static int
6789nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6790 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6791{
6792 struct nfs4_exception exception = { };
6793 int err;
6794 do {
6795 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6796 switch (err) {
6797 case 0:
6798 case -NFS4ERR_WRONGSEC:
6799 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006800 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006801 default:
6802 err = nfs4_handle_exception(server, err, &exception);
6803 }
6804 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006805out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006806 return err;
6807}
6808
6809static int
6810nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6811 struct nfs_fsinfo *info)
6812{
6813 int err;
6814 struct page *page;
6815 rpc_authflavor_t flavor;
6816 struct nfs4_secinfo_flavors *flavors;
6817
6818 page = alloc_page(GFP_KERNEL);
6819 if (!page) {
6820 err = -ENOMEM;
6821 goto out;
6822 }
6823
6824 flavors = page_address(page);
6825 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6826
6827 /*
6828 * Fall back on "guess and check" method if
6829 * the server doesn't support SECINFO_NO_NAME
6830 */
6831 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6832 err = nfs4_find_root_sec(server, fhandle, info);
6833 goto out_freepage;
6834 }
6835 if (err)
6836 goto out_freepage;
6837
6838 flavor = nfs_find_best_sec(flavors);
6839 if (err == 0)
6840 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6841
6842out_freepage:
6843 put_page(page);
6844 if (err == -EACCES)
6845 return -EPERM;
6846out:
6847 return err;
6848}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006849
6850static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006851{
6852 int status;
6853 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006854 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006855 };
6856 struct nfs41_test_stateid_res res;
6857 struct rpc_message msg = {
6858 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6859 .rpc_argp = &args,
6860 .rpc_resp = &res,
6861 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006862
Chuck Lever38527b12012-07-11 16:30:23 -04006863 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006864 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006865 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006866 if (status != NFS_OK) {
6867 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006868 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006869 }
6870 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006871 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006872}
6873
Chuck Lever38527b12012-07-11 16:30:23 -04006874/**
6875 * nfs41_test_stateid - perform a TEST_STATEID operation
6876 *
6877 * @server: server / transport on which to perform the operation
6878 * @stateid: state ID to test
6879 *
6880 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6881 * Otherwise a negative NFS4ERR value is returned if the operation
6882 * failed or the state ID is not currently valid.
6883 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006884static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006885{
6886 struct nfs4_exception exception = { };
6887 int err;
6888 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006889 err = _nfs41_test_stateid(server, stateid);
6890 if (err != -NFS4ERR_DELAY)
6891 break;
6892 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006893 } while (exception.retry);
6894 return err;
6895}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006896
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006897static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006898{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006899 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006900 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006901 };
6902 struct nfs41_free_stateid_res res;
6903 struct rpc_message msg = {
6904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6905 .rpc_argp = &args,
6906 .rpc_resp = &res,
6907 };
Chuck Lever38527b12012-07-11 16:30:23 -04006908 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006909
Chuck Lever38527b12012-07-11 16:30:23 -04006910 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006911 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006912 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006913 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006914 dprintk("NFS reply free_stateid: %d\n", status);
6915 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006916}
6917
Chuck Lever38527b12012-07-11 16:30:23 -04006918/**
6919 * nfs41_free_stateid - perform a FREE_STATEID operation
6920 *
6921 * @server: server / transport on which to perform the operation
6922 * @stateid: state ID to release
6923 *
6924 * Returns NFS_OK if the server freed "stateid". Otherwise a
6925 * negative NFS4ERR value is returned.
6926 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006927static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006928{
6929 struct nfs4_exception exception = { };
6930 int err;
6931 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006932 err = _nfs4_free_stateid(server, stateid);
6933 if (err != -NFS4ERR_DELAY)
6934 break;
6935 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006936 } while (exception.retry);
6937 return err;
6938}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006939
6940static bool nfs41_match_stateid(const nfs4_stateid *s1,
6941 const nfs4_stateid *s2)
6942{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006943 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006944 return false;
6945
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006946 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006947 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006948 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006949 return true;
6950
6951 return false;
6952}
6953
Andy Adamson557134a2009-04-01 09:21:53 -04006954#endif /* CONFIG_NFS_V4_1 */
6955
Trond Myklebust36281ca2012-03-04 18:13:56 -05006956static bool nfs4_match_stateid(const nfs4_stateid *s1,
6957 const nfs4_stateid *s2)
6958{
Trond Myklebustf597c532012-03-04 18:13:56 -05006959 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006960}
6961
6962
Trond Myklebust17280172012-03-11 13:11:00 -04006963static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006964 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006965 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 .recover_open = nfs4_open_reclaim,
6967 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006968 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006969 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006970 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971};
6972
Andy Adamson591d71c2009-04-01 09:22:47 -04006973#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006974static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006975 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6976 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6977 .recover_open = nfs4_open_reclaim,
6978 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006979 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006980 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006981 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006982 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006983};
6984#endif /* CONFIG_NFS_V4_1 */
6985
Trond Myklebust17280172012-03-11 13:11:00 -04006986static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006987 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006988 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 .recover_open = nfs4_open_expired,
6990 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006991 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006992 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993};
6994
Andy Adamson591d71c2009-04-01 09:22:47 -04006995#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006996static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006997 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6998 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006999 .recover_open = nfs41_open_expired,
7000 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007001 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007002 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007003};
7004#endif /* CONFIG_NFS_V4_1 */
7005
Trond Myklebust17280172012-03-11 13:11:00 -04007006static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007007 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007008 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007009 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007010};
7011
7012#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007013static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007014 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007015 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007016 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007017};
7018#endif
7019
Trond Myklebust97dc1352010-06-16 09:52:26 -04007020static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7021 .minor_version = 0,
7022 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007023 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007024 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007025 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7026 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7027 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007028};
7029
7030#if defined(CONFIG_NFS_V4_1)
7031static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7032 .minor_version = 1,
7033 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007034 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007035 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007036 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7037 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7038 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007039};
7040#endif
7041
Trond Myklebust97dc1352010-06-16 09:52:26 -04007042const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7043 [0] = &nfs_v4_0_minor_ops,
7044#if defined(CONFIG_NFS_V4_1)
7045 [1] = &nfs_v4_1_minor_ops,
7046#endif
7047};
7048
Bryan Schumaker73a79702012-07-16 16:39:12 -04007049const struct inode_operations nfs4_dir_inode_operations = {
7050 .create = nfs_create,
7051 .lookup = nfs_lookup,
7052 .atomic_open = nfs_atomic_open,
7053 .link = nfs_link,
7054 .unlink = nfs_unlink,
7055 .symlink = nfs_symlink,
7056 .mkdir = nfs_mkdir,
7057 .rmdir = nfs_rmdir,
7058 .mknod = nfs_mknod,
7059 .rename = nfs_rename,
7060 .permission = nfs_permission,
7061 .getattr = nfs_getattr,
7062 .setattr = nfs_setattr,
7063 .getxattr = generic_getxattr,
7064 .setxattr = generic_setxattr,
7065 .listxattr = generic_listxattr,
7066 .removexattr = generic_removexattr,
7067};
7068
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007069static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007070 .permission = nfs_permission,
7071 .getattr = nfs_getattr,
7072 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007073 .getxattr = generic_getxattr,
7074 .setxattr = generic_setxattr,
7075 .listxattr = generic_listxattr,
7076 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007077};
7078
David Howells509de812006-08-22 20:06:11 -04007079const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 .version = 4, /* protocol version */
7081 .dentry_ops = &nfs4_dentry_operations,
7082 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007083 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007084 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007086 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007087 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 .getattr = nfs4_proc_getattr,
7089 .setattr = nfs4_proc_setattr,
7090 .lookup = nfs4_proc_lookup,
7091 .access = nfs4_proc_access,
7092 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093 .create = nfs4_proc_create,
7094 .remove = nfs4_proc_remove,
7095 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007096 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 .unlink_done = nfs4_proc_unlink_done,
7098 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007099 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007100 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007101 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 .link = nfs4_proc_link,
7103 .symlink = nfs4_proc_symlink,
7104 .mkdir = nfs4_proc_mkdir,
7105 .rmdir = nfs4_proc_remove,
7106 .readdir = nfs4_proc_readdir,
7107 .mknod = nfs4_proc_mknod,
7108 .statfs = nfs4_proc_statfs,
7109 .fsinfo = nfs4_proc_fsinfo,
7110 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007111 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 .decode_dirent = nfs4_decode_dirent,
7113 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007114 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007115 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007116 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007118 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007119 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007120 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007122 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007123 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007125 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007126 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007127 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007128 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007129 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007130 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007131 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007132 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007133 .create_server = nfs4_create_server,
7134 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135};
7136
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007137static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7138 .prefix = XATTR_NAME_NFSV4_ACL,
7139 .list = nfs4_xattr_list_nfs4_acl,
7140 .get = nfs4_xattr_get_nfs4_acl,
7141 .set = nfs4_xattr_set_nfs4_acl,
7142};
7143
7144const struct xattr_handler *nfs4_xattr_handlers[] = {
7145 &nfs4_xattr_nfs4_acl_handler,
7146 NULL
7147};
7148
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149/*
7150 * Local variables:
7151 * c-basic-offset: 8
7152 * End:
7153 */