blob: d91abaa522e85a3c9c2b0ae24d275d68ba8b5c10 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000210 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
212 return;
213 }
214
215 readdir->cookie = 0;
216 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
217 if (cookie == 2)
218 return;
219
220 /*
221 * NFSv4 servers do not return entries for '.' and '..'
222 * Therefore, we fake these entries here. We let '.'
223 * have cookie 0 and '..' have cookie 1. Note that
224 * when talking to the server, we always send cookie 0
225 * instead of 1 or 2.
226 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800227 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (cookie == 0) {
230 *p++ = xdr_one; /* next */
231 *p++ = xdr_zero; /* cookie, first word */
232 *p++ = xdr_one; /* cookie, second word */
233 *p++ = xdr_one; /* entry len */
234 memcpy(p, ".\0\0\0", 4); /* entry */
235 p++;
236 *p++ = xdr_one; /* bitmap length */
237 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
238 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400239 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
242 *p++ = xdr_one; /* next */
243 *p++ = xdr_zero; /* cookie, first word */
244 *p++ = xdr_two; /* cookie, second word */
245 *p++ = xdr_two; /* entry len */
246 memcpy(p, "..\0\0", 4); /* entry */
247 p++;
248 *p++ = xdr_one; /* bitmap length */
249 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
250 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400251 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 readdir->pgbase = (char *)p - (char *)start;
254 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800255 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Trond Myklebust65de8722008-12-23 15:21:44 -0500258static int nfs4_wait_clnt_recover(struct nfs_client *clp)
259{
260 int res;
261
262 might_sleep();
263
Trond Myklebuste005e802008-12-23 15:21:48 -0500264 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400265 nfs_wait_bit_killable, TASK_KILLABLE);
Chuck Lever46a87b82012-07-11 16:30:41 -0400266 if (res)
267 return res;
268
269 if (clp->cl_cons_state < 0)
270 return clp->cl_cons_state;
271 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500272}
273
274static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
275{
276 int res = 0;
277
278 might_sleep();
279
280 if (*timeout <= 0)
281 *timeout = NFS4_POLL_RETRY_MIN;
282 if (*timeout > NFS4_POLL_RETRY_MAX)
283 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100284 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500285 if (fatal_signal_pending(current))
286 res = -ERESTARTSYS;
287 *timeout <<= 1;
288 return res;
289}
290
291/* This is the error handling routine for processes that are allowed
292 * to sleep.
293 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800294static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500295{
296 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500297 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500298 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500299 int ret = errorcode;
300
301 exception->retry = 0;
302 switch(errorcode) {
303 case 0:
304 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500305 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400306 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400307 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500308 exception->retry = 1;
309 return 0;
310 }
311 if (state == NULL)
312 break;
313 nfs4_schedule_stateid_recovery(server, state);
314 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500315 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500316 case -NFS4ERR_ADMIN_REVOKED:
317 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500318 if (state == NULL)
319 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400320 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500321 nfs4_schedule_stateid_recovery(server, state);
322 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400323 case -NFS4ERR_EXPIRED:
324 if (state != NULL)
325 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500327 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500328 nfs4_schedule_lease_recovery(clp);
329 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500330#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400331 case -NFS4ERR_BADSESSION:
332 case -NFS4ERR_BADSLOT:
333 case -NFS4ERR_BAD_HIGH_SLOT:
334 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
335 case -NFS4ERR_DEADSESSION:
336 case -NFS4ERR_SEQ_FALSE_RETRY:
337 case -NFS4ERR_SEQ_MISORDERED:
338 dprintk("%s ERROR: %d Reset session\n", __func__,
339 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400340 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400341 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500342#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500343 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500344 if (exception->timeout > HZ) {
345 /* We have retried a decent amount, time to
346 * fail
347 */
348 ret = -EBUSY;
349 break;
350 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500351 case -NFS4ERR_GRACE:
352 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500353 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500354 ret = nfs4_delay(server->client, &exception->timeout);
355 if (ret != 0)
356 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400357 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500358 case -NFS4ERR_OLD_STATEID:
359 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800360 break;
361 case -NFS4ERR_BADOWNER:
362 /* The following works around a Linux server bug! */
363 case -NFS4ERR_BADNAME:
364 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
365 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
366 exception->retry = 1;
367 printk(KERN_WARNING "NFS: v4 server %s "
368 "does not accept raw "
369 "uid/gids. "
370 "Reenabling the idmapper.\n",
371 server->nfs_client->cl_hostname);
372 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500373 }
374 /* We failed to handle the error */
375 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500376wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500377 ret = nfs4_wait_clnt_recover(clp);
378 if (ret == 0)
379 exception->retry = 1;
380 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500381}
382
383
Trond Myklebust452e9352010-07-31 14:29:06 -0400384static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_lock(&clp->cl_lock);
387 if (time_before(clp->cl_last_renewal,timestamp))
388 clp->cl_last_renewal = timestamp;
389 spin_unlock(&clp->cl_lock);
390}
391
Trond Myklebust452e9352010-07-31 14:29:06 -0400392static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
393{
394 do_renew_lease(server->nfs_client, timestamp);
395}
396
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400397#if defined(CONFIG_NFS_V4_1)
398
Benny Halevy510b8172009-04-01 09:22:14 -0400399/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400400 * nfs4_free_slot - free a slot and efficiently update slot table.
401 *
402 * freeing a slot is trivially done by clearing its respective bit
403 * in the bitmap.
404 * If the freed slotid equals highest_used_slotid we want to update it
405 * so that the server would be able to size down the slot table if needed,
406 * otherwise we know that the highest_used_slotid is still in use.
407 * When updating highest_used_slotid there may be "holes" in the bitmap
408 * so we need to scan down from highest_used_slotid to 0 looking for the now
409 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500410 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500411 *
412 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400413 */
414static void
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500415nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400416{
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500417 u32 slotid = slot->slot_nr;
418
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) {
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500424 u32 new_max = find_last_bit(tbl->used_slots, slotid);
425 if (new_max < slotid)
426 tbl->highest_used_slotid = new_max;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400427 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{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500472 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400473 struct nfs4_slot_table *tbl;
474
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 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500481 tbl = res->sr_slot->table;
482 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500483
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500484 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500485 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500486 nfs4_check_drain_fc_complete(session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500487 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400488 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400489}
490
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500491/* Update the client's idea of target_highest_slotid */
492static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
493 u32 target_highest_slotid)
494{
495 if (tbl->target_highest_slotid == target_highest_slotid)
496 return;
497 tbl->target_highest_slotid = target_highest_slotid;
498 tbl->generation++;
499}
500
501static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
502 struct nfs4_slot *slot,
503 struct nfs4_sequence_res *res)
504{
505 spin_lock(&tbl->slot_tbl_lock);
506 if (tbl->generation != slot->generation)
507 goto out;
508 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
509out:
510 spin_unlock(&tbl->slot_tbl_lock);
511}
512
Trond Myklebust14516c32010-07-31 14:29:06 -0400513static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400514{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500515 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500516 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400517 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400518 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400519
520 /*
521 * sr_status remains 1 if an RPC level error occurred. The server
522 * may or may not have processed the sequence operation..
523 * Proceed as if the server received and processed the sequence
524 * operation.
525 */
526 if (res->sr_status == 1)
527 res->sr_status = NFS_OK;
528
Bryan Schumaker468f8612011-04-18 15:57:32 -0400529 /* don't increment the sequence number if the task wasn't sent */
530 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400531 goto out;
532
Trond Myklebust933602e2012-11-16 12:12:38 -0500533 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500534 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500535
Andy Adamson691daf32009-12-04 15:55:39 -0500536 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400537 switch (res->sr_status) {
538 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400539 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500540 ++slot->seq_nr;
541 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500542 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400543 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500544 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500545 if (res->sr_status_flags != 0)
546 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500547 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400548 break;
549 case -NFS4ERR_DELAY:
550 /* The server detected a resend of the RPC call and
551 * returned NFS4ERR_DELAY as per Section 2.10.6.2
552 * of RFC5661.
553 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500554 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400555 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500556 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500557 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400558 goto out_retry;
559 default:
560 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500561 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400562 }
563out:
564 /* The session may be reset by one of the error handlers. */
565 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400566 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400567 return 1;
568out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400569 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400570 goto out;
571 rpc_delay(task, NFS4_POLL_RETRY_MAX);
572 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400573}
574
Trond Myklebust14516c32010-07-31 14:29:06 -0400575static int nfs4_sequence_done(struct rpc_task *task,
576 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400577{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500578 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400579 return 1;
580 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400581}
582
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400583/*
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500584 * nfs4_alloc_slot - efficiently look for a free slot
Benny Halevy510b8172009-04-01 09:22:14 -0400585 *
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500586 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
Benny Halevy510b8172009-04-01 09:22:14 -0400587 * If found, we mark the slot as used, update the highest_used_slotid,
588 * and respectively set up the sequence operation args.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589 *
590 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400591 */
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500592static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400593{
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500594 struct nfs4_slot *ret = NULL;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500595 u32 slotid;
Benny Halevy510b8172009-04-01 09:22:14 -0400596
Trond Myklebust45d43c22012-02-06 19:38:51 -0500597 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400598 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
599 tbl->max_slots);
600 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
601 if (slotid >= tbl->max_slots)
602 goto out;
603 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500604 if (slotid > tbl->highest_used_slotid ||
605 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400606 tbl->highest_used_slotid = slotid;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500607 ret = &tbl->slots[slotid];
608 ret->renewal_time = jiffies;
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500609 ret->generation = tbl->generation;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500610
Benny Halevy510b8172009-04-01 09:22:14 -0400611out:
612 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500613 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
614 ret ? ret->slot_nr : -1);
615 return ret;
Benny Halevy510b8172009-04-01 09:22:14 -0400616}
617
Trond Myklebust9d12b212012-01-17 22:04:25 -0500618static void nfs41_init_sequence(struct nfs4_sequence_args *args,
619 struct nfs4_sequence_res *res, int cache_reply)
620{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500621 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500622 args->sa_cache_this = 0;
623 if (cache_reply)
624 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500625 res->sr_slot = NULL;
626}
627
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000628int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400629 struct nfs4_sequence_args *args,
630 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 struct rpc_task *task)
632{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400633 struct nfs4_slot *slot;
634 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400635
636 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400638 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639 return 0;
640
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400641 tbl = &session->fc_slot_table;
642
643 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400644 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800645 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500646 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500647 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400648 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500649 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500650 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400651 }
652
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800653 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
654 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
655 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
656 spin_unlock(&tbl->slot_tbl_lock);
657 dprintk("%s enforce FIFO order\n", __func__);
658 return -EAGAIN;
659 }
660
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500661 slot = nfs4_alloc_slot(tbl);
662 if (slot == NULL) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400663 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
664 spin_unlock(&tbl->slot_tbl_lock);
665 dprintk("<-- %s: no free slots\n", __func__);
666 return -EAGAIN;
667 }
668 spin_unlock(&tbl->slot_tbl_lock);
669
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800670 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500671
672 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400673
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500674 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
675 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400676
Benny Halevydfb4f3092010-09-24 09:17:01 -0400677 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400678 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400679 /*
680 * sr_status is only set in decode_sequence, and so will remain
681 * set to 1 if an rpc level failure occurs.
682 */
683 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 return 0;
685}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000686EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687
Trond Myklebust035168a2010-06-16 09:52:26 -0400688int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400689 struct nfs4_sequence_args *args,
690 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691 struct rpc_task *task)
692{
Trond Myklebust035168a2010-06-16 09:52:26 -0400693 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400694 int ret = 0;
695
Trond Myklebust9d12b212012-01-17 22:04:25 -0500696 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400697 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400698
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500699 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400700 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500701 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400702
Trond Myklebust9d12b212012-01-17 22:04:25 -0500703 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400704out:
705 dprintk("<-- %s status=%d\n", __func__, ret);
706 return ret;
707}
708
709struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400710 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400711 struct nfs4_sequence_args *seq_args;
712 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713};
714
715static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
716{
717 struct nfs41_call_sync_data *data = calldata;
718
Trond Myklebust035168a2010-06-16 09:52:26 -0400719 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
720
721 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500722 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400723 return;
724 rpc_call_start(task);
725}
726
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800727static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
728{
729 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
730 nfs41_call_sync_prepare(task, calldata);
731}
732
Andy Adamson69ab40c2009-04-01 09:22:19 -0400733static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
734{
735 struct nfs41_call_sync_data *data = calldata;
736
Trond Myklebust14516c32010-07-31 14:29:06 -0400737 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400738}
739
Trond Myklebust17280172012-03-11 13:11:00 -0400740static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400741 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400742 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400743};
744
Trond Myklebust17280172012-03-11 13:11:00 -0400745static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800746 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
747 .rpc_call_done = nfs41_call_sync_done,
748};
749
Bryan Schumaker7c513052011-03-24 17:12:24 +0000750static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
751 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400752 struct rpc_message *msg,
753 struct nfs4_sequence_args *args,
754 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800755 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400756{
757 int ret;
758 struct rpc_task *task;
759 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400760 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400761 .seq_args = args,
762 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400763 };
764 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000765 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400766 .rpc_message = msg,
767 .callback_ops = &nfs41_call_sync_ops,
768 .callback_data = &data
769 };
770
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800771 if (privileged)
772 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400773 task = rpc_run_task(&task_setup);
774 if (IS_ERR(task))
775 ret = PTR_ERR(task);
776 else {
777 ret = task->tk_status;
778 rpc_put_task(task);
779 }
780 return ret;
781}
782
Bryan Schumaker7c513052011-03-24 17:12:24 +0000783int _nfs4_call_sync_session(struct rpc_clnt *clnt,
784 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400785 struct rpc_message *msg,
786 struct nfs4_sequence_args *args,
787 struct nfs4_sequence_res *res,
788 int cache_reply)
789{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500790 nfs41_init_sequence(args, res, cache_reply);
791 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400792}
793
Trond Myklebustdf896452010-06-16 09:52:26 -0400794#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500795static inline
796void nfs41_init_sequence(struct nfs4_sequence_args *args,
797 struct nfs4_sequence_res *res, int cache_reply)
798{
799}
800
Trond Myklebust14516c32010-07-31 14:29:06 -0400801static int nfs4_sequence_done(struct rpc_task *task,
802 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400803{
Trond Myklebust14516c32010-07-31 14:29:06 -0400804 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400805}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400806#endif /* CONFIG_NFS_V4_1 */
807
Bryan Schumaker7c513052011-03-24 17:12:24 +0000808int _nfs4_call_sync(struct rpc_clnt *clnt,
809 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400810 struct rpc_message *msg,
811 struct nfs4_sequence_args *args,
812 struct nfs4_sequence_res *res,
813 int cache_reply)
814{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500815 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000816 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400817}
818
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000819static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000820int nfs4_call_sync(struct rpc_clnt *clnt,
821 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000822 struct rpc_message *msg,
823 struct nfs4_sequence_args *args,
824 struct nfs4_sequence_res *res,
825 int cache_reply)
826{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000827 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
828 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000829}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400830
Trond Myklebust38478b22006-05-25 01:40:57 -0400831static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Trond Myklebust38478b22006-05-25 01:40:57 -0400833 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Trond Myklebust38478b22006-05-25 01:40:57 -0400835 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400836 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700837 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400838 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700839 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400840 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400844 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 struct nfs_openargs o_arg;
846 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100847 struct nfs_open_confirmargs c_arg;
848 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500849 struct nfs4_string owner_name;
850 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100851 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400853 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400855 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100856 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100857 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400858 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100859 int rpc_status;
860 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100861};
862
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400863
864static void nfs4_init_opendata_res(struct nfs4_opendata *p)
865{
866 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400867 p->o_res.seqid = p->o_arg.seqid;
868 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400869 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400870 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400871 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500872 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400873}
874
Al Viro82a2c1b2011-06-22 18:30:55 -0400875static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400877 const struct iattr *attrs,
878 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879{
Al Viro82a2c1b2011-06-22 18:30:55 -0400880 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 struct inode *dir = parent->d_inode;
882 struct nfs_server *server = NFS_SERVER(dir);
883 struct nfs4_opendata *p;
884
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400885 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886 if (p == NULL)
887 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400888 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100889 if (p->o_arg.seqid == NULL)
890 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400891 nfs_sb_active(dentry->d_sb);
892 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893 p->dir = parent;
894 p->owner = sp;
895 atomic_inc(&sp->so_count);
896 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897 p->o_arg.open_flags = flags;
898 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700899 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
900 * will return permission denied for all bits until close */
901 if (!(flags & O_EXCL)) {
902 /* ask server to check for all possible rights as results
903 * are cached */
904 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
905 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700906 }
David Howells7539bba2006-08-22 20:06:09 -0400907 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400908 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
909 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400910 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100911 p->o_arg.server = server;
912 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400913 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100914 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500915 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500916 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400917
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100918 p->o_arg.u.attrs = &p->attrs;
919 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500920
921 verf[0] = jiffies;
922 verf[1] = current->pid;
923 memcpy(p->o_arg.u.verifier.data, verf,
924 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100925 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100926 p->c_arg.fh = &p->o_res.fh;
927 p->c_arg.stateid = &p->o_res.stateid;
928 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400929 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400930 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100931 return p;
932err_free:
933 kfree(p);
934err:
935 dput(parent);
936 return NULL;
937}
938
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400939static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100940{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400941 struct nfs4_opendata *p = container_of(kref,
942 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400943 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400944
945 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400946 if (p->state != NULL)
947 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400948 nfs4_put_state_owner(p->owner);
949 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400950 dput(p->dentry);
951 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500952 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400953 kfree(p);
954}
955
956static void nfs4_opendata_put(struct nfs4_opendata *p)
957{
958 if (p != NULL)
959 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960}
961
Trond Myklebust06f814a2006-01-03 09:55:07 +0100962static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
963{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100964 int ret;
965
Trond Myklebust06f814a2006-01-03 09:55:07 +0100966 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100967 return ret;
968}
969
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400971{
972 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973
Trond Myklebust536e43d2012-01-17 22:04:26 -0500974 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975 goto out;
976 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400977 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500978 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
979 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400980 break;
981 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500982 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
983 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400984 break;
985 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500986 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
987 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400988 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400990 return ret;
991}
992
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400994{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500995 if (delegation == NULL)
996 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400998 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500999 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001000 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001001 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002 return 1;
1003}
1004
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001006{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001008 case FMODE_WRITE:
1009 state->n_wronly++;
1010 break;
1011 case FMODE_READ:
1012 state->n_rdonly++;
1013 break;
1014 case FMODE_READ|FMODE_WRITE:
1015 state->n_rdwr++;
1016 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001018}
1019
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001021{
1022 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001023 nfs4_stateid_copy(&state->stateid, stateid);
1024 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001026 case FMODE_READ:
1027 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1028 break;
1029 case FMODE_WRITE:
1030 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1031 break;
1032 case FMODE_READ|FMODE_WRITE:
1033 set_bit(NFS_O_RDWR_STATE, &state->flags);
1034 }
1035}
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001038{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001039 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001041 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001042}
1043
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044static 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 -07001045{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001046 /*
1047 * Protect the call to nfs4_state_set_mode_locked and
1048 * serialise the stateid update
1049 */
1050 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001051 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001052 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001053 set_bit(NFS_DELEGATED_STATE, &state->flags);
1054 }
1055 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001057 write_sequnlock(&state->seqlock);
1058 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001060 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063static 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 -05001064{
1065 struct nfs_inode *nfsi = NFS_I(state->inode);
1066 struct nfs_delegation *deleg_cur;
1067 int ret = 0;
1068
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001069 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001070
1071 rcu_read_lock();
1072 deleg_cur = rcu_dereference(nfsi->delegation);
1073 if (deleg_cur == NULL)
1074 goto no_delegation;
1075
1076 spin_lock(&deleg_cur->lock);
1077 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001078 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001079 goto no_delegation_unlock;
1080
1081 if (delegation == NULL)
1082 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001083 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001084 goto no_delegation_unlock;
1085
Trond Myklebustb7391f42008-12-23 15:21:52 -05001086 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001088 ret = 1;
1089no_delegation_unlock:
1090 spin_unlock(&deleg_cur->lock);
1091no_delegation:
1092 rcu_read_unlock();
1093
1094 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001096 ret = 1;
1097 }
1098
1099 return ret;
1100}
1101
1102
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001103static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104{
1105 struct nfs_delegation *delegation;
1106
1107 rcu_read_lock();
1108 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001109 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001110 rcu_read_unlock();
1111 return;
1112 }
1113 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001114 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001115}
1116
Trond Myklebust6ee41262007-07-08 14:11:36 -04001117static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118{
1119 struct nfs4_state *state = opendata->state;
1120 struct nfs_inode *nfsi = NFS_I(state->inode);
1121 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001122 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001123 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001124 nfs4_stateid stateid;
1125 int ret = -EAGAIN;
1126
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001128 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001129 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001130 if (can_open_cached(state, fmode, open_mode)) {
1131 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001132 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001133 goto out_return_state;
1134 }
1135 spin_unlock(&state->owner->so_lock);
1136 }
Trond Myklebust34310432008-12-23 15:21:38 -05001137 rcu_read_lock();
1138 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001139 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001140 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001141 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001142 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001143 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001144 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001145 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001146 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001147 if (ret != 0)
1148 goto out;
1149 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001150
1151 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001152 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001153 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001154 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001155out:
1156 return ERR_PTR(ret);
1157out_return_state:
1158 atomic_inc(&state->count);
1159 return state;
1160}
1161
Andy Adamsone23008e2012-10-02 21:07:32 -04001162static void
1163nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1164{
1165 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1166 struct nfs_delegation *delegation;
1167 int delegation_flags = 0;
1168
1169 rcu_read_lock();
1170 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1171 if (delegation)
1172 delegation_flags = delegation->flags;
1173 rcu_read_unlock();
1174 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1175 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1176 "returning a delegation for "
1177 "OPEN(CLAIM_DELEGATE_CUR)\n",
1178 clp->cl_hostname);
1179 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1180 nfs_inode_set_delegation(state->inode,
1181 data->owner->so_cred,
1182 &data->o_res);
1183 else
1184 nfs_inode_reclaim_delegation(state->inode,
1185 data->owner->so_cred,
1186 &data->o_res);
1187}
1188
1189/*
1190 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1191 * and update the nfs4_state.
1192 */
1193static struct nfs4_state *
1194_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1195{
1196 struct inode *inode = data->state->inode;
1197 struct nfs4_state *state = data->state;
1198 int ret;
1199
1200 if (!data->rpc_done) {
1201 ret = data->rpc_status;
1202 goto err;
1203 }
1204
1205 ret = -ESTALE;
1206 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1207 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1208 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1209 goto err;
1210
1211 ret = -ENOMEM;
1212 state = nfs4_get_open_state(inode, data->owner);
1213 if (state == NULL)
1214 goto err;
1215
1216 ret = nfs_refresh_inode(inode, &data->f_attr);
1217 if (ret)
1218 goto err;
1219
1220 if (data->o_res.delegation_type != 0)
1221 nfs4_opendata_check_deleg(data, state);
1222 update_open_stateid(state, &data->o_res.stateid, NULL,
1223 data->o_arg.fmode);
1224
1225 return state;
1226err:
1227 return ERR_PTR(ret);
1228
1229}
1230
1231static struct nfs4_state *
1232_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001233{
1234 struct inode *inode;
1235 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001236 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001237
Trond Myklebustaac00a82007-07-05 19:02:21 -04001238 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001239 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001240 goto out;
1241 }
1242
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001243 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001244 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001245 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001246 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001247 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001248 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001249 goto err;
1250 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001251 state = nfs4_get_open_state(inode, data->owner);
1252 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001253 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001254 if (data->o_res.delegation_type != 0)
1255 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001256 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001257 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001258 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001259out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001260 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001261err_put_inode:
1262 iput(inode);
1263err:
1264 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001265}
1266
Andy Adamsone23008e2012-10-02 21:07:32 -04001267static struct nfs4_state *
1268nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1269{
1270 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1271 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1272 return _nfs4_opendata_to_nfs4_state(data);
1273}
1274
Trond Myklebust864472e2006-01-03 09:55:15 +01001275static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1276{
1277 struct nfs_inode *nfsi = NFS_I(state->inode);
1278 struct nfs_open_context *ctx;
1279
1280 spin_lock(&state->inode->i_lock);
1281 list_for_each_entry(ctx, &nfsi->open_files, list) {
1282 if (ctx->state != state)
1283 continue;
1284 get_nfs_open_context(ctx);
1285 spin_unlock(&state->inode->i_lock);
1286 return ctx;
1287 }
1288 spin_unlock(&state->inode->i_lock);
1289 return ERR_PTR(-ENOENT);
1290}
1291
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001292static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1293{
1294 struct nfs4_opendata *opendata;
1295
Al Viro3d4ff432011-06-22 18:40:12 -04001296 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001297 if (opendata == NULL)
1298 return ERR_PTR(-ENOMEM);
1299 opendata->state = state;
1300 atomic_inc(&state->count);
1301 return opendata;
1302}
1303
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001304static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001305{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001306 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001307 int ret;
1308
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309 opendata->o_arg.open_flags = 0;
1310 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001311 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1312 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1313 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001314 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001315 if (ret != 0)
1316 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001317 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001318 if (IS_ERR(newstate))
1319 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001320 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001321 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001322 return 0;
1323}
1324
1325static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1326{
Trond Myklebust864472e2006-01-03 09:55:15 +01001327 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001328 int ret;
1329
1330 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001331 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001332 smp_rmb();
1333 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001334 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001335 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001336 if (ret != 0)
1337 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001338 if (newstate != state)
1339 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001340 }
1341 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001342 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001343 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001344 if (ret != 0)
1345 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001346 if (newstate != state)
1347 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001348 }
1349 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001350 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001351 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001352 if (ret != 0)
1353 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001354 if (newstate != state)
1355 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001356 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001357 /*
1358 * We may have performed cached opens for all three recoveries.
1359 * Check if we need to update the current stateid.
1360 */
1361 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001362 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001363 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001364 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001365 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001366 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001367 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001368 return 0;
1369}
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371/*
1372 * OPEN_RECLAIM:
1373 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001375static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001377 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001378 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001379 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int status;
1381
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001382 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1383 if (IS_ERR(opendata))
1384 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001385 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1386 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001387 rcu_read_lock();
1388 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001389 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001390 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001391 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001392 opendata->o_arg.u.delegation_type = delegation_type;
1393 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001394 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return status;
1396}
1397
Trond Myklebust539cd032007-06-05 11:46:42 -04001398static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 struct nfs_server *server = NFS_SERVER(state->inode);
1401 struct nfs4_exception exception = { };
1402 int err;
1403 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001404 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001405 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001406 break;
1407 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 } while (exception.retry);
1409 return err;
1410}
1411
Trond Myklebust864472e2006-01-03 09:55:15 +01001412static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1413{
1414 struct nfs_open_context *ctx;
1415 int ret;
1416
1417 ctx = nfs4_state_find_open_context(state);
1418 if (IS_ERR(ctx))
1419 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001420 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001421 put_nfs_open_context(ctx);
1422 return ret;
1423}
1424
Trond Myklebust13437e12007-07-06 15:10:43 -04001425static 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 -07001426{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001427 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001428 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001430 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1431 if (IS_ERR(opendata))
1432 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001433 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001434 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001435 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001436 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001437 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
Trond Myklebust13437e12007-07-06 15:10:43 -04001440int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001443 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int err;
1445 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001446 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 switch (err) {
1448 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001449 case -ENOENT:
1450 case -ESTALE:
1451 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001452 case -NFS4ERR_BADSESSION:
1453 case -NFS4ERR_BADSLOT:
1454 case -NFS4ERR_BAD_HIGH_SLOT:
1455 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1456 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001457 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 case -NFS4ERR_STALE_CLIENTID:
1460 case -NFS4ERR_STALE_STATEID:
1461 case -NFS4ERR_EXPIRED:
1462 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001463 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001464 goto out;
1465 case -ERESTARTSYS:
1466 /*
1467 * The show must go on: exit, but mark the
1468 * stateid as needing recovery.
1469 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001470 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001471 case -NFS4ERR_ADMIN_REVOKED:
1472 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001473 nfs_inode_find_state_and_recover(state->inode,
1474 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001475 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001476 case -EKEYEXPIRED:
1477 /*
1478 * User RPCSEC_GSS context has expired.
1479 * We cannot recover this stateid now, so
1480 * skip it and allow recovery thread to
1481 * proceed.
1482 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001483 case -ENOMEM:
1484 err = 0;
1485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 err = nfs4_handle_exception(server, err, &exception);
1488 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001489out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return err;
1491}
1492
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001493static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1494{
1495 struct nfs4_opendata *data = calldata;
1496
1497 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001498 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001499 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001500 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001501 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001502 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001503 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001504}
1505
1506static void nfs4_open_confirm_release(void *calldata)
1507{
1508 struct nfs4_opendata *data = calldata;
1509 struct nfs4_state *state = NULL;
1510
1511 /* If this request hasn't been cancelled, do nothing */
1512 if (data->cancelled == 0)
1513 goto out_free;
1514 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001515 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001516 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001517 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001518 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001519 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001520out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001521 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001522}
1523
1524static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001525 .rpc_call_done = nfs4_open_confirm_done,
1526 .rpc_release = nfs4_open_confirm_release,
1527};
1528
1529/*
1530 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1531 */
1532static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1533{
1534 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1535 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001536 struct rpc_message msg = {
1537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1538 .rpc_argp = &data->c_arg,
1539 .rpc_resp = &data->c_res,
1540 .rpc_cred = data->owner->so_cred,
1541 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001542 struct rpc_task_setup task_setup_data = {
1543 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001544 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001545 .callback_ops = &nfs4_open_confirm_ops,
1546 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001547 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001548 .flags = RPC_TASK_ASYNC,
1549 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 int status;
1551
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001552 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001553 data->rpc_done = 0;
1554 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001555 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001556 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001557 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001558 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001559 status = nfs4_wait_for_completion_rpc_task(task);
1560 if (status != 0) {
1561 data->cancelled = 1;
1562 smp_wmb();
1563 } else
1564 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001565 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return status;
1567}
1568
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001569static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001571 struct nfs4_opendata *data = calldata;
1572 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001573
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001574 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1575 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001576 /*
1577 * Check if we still need to send an OPEN call, or if we can use
1578 * a delegation instead.
1579 */
1580 if (data->state != NULL) {
1581 struct nfs_delegation *delegation;
1582
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001583 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001584 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001585 rcu_read_lock();
1586 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001587 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1588 can_open_delegated(delegation, data->o_arg.fmode))
1589 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001590 rcu_read_unlock();
1591 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001592 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001593 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001594 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001595 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001596 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001597 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1598 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001599 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001600 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001601 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001602 &data->o_res.seq_res,
1603 task) != 0)
1604 nfs_release_seqid(data->o_arg.seqid);
1605 else
1606 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001607 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001608unlock_no_action:
1609 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001610out_no_action:
1611 task->tk_action = NULL;
1612
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001613}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001615static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1616{
1617 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1618 nfs4_open_prepare(task, calldata);
1619}
1620
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001621static void nfs4_open_done(struct rpc_task *task, void *calldata)
1622{
1623 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001625 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001626
Trond Myklebust14516c32010-07-31 14:29:06 -04001627 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1628 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001629
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001630 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001631 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1632 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001633 case S_IFREG:
1634 break;
1635 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001636 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001637 break;
1638 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001640 break;
1641 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001642 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001643 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001644 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001645 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001646 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1647 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001648 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001649 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001650}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001651
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001652static void nfs4_open_release(void *calldata)
1653{
1654 struct nfs4_opendata *data = calldata;
1655 struct nfs4_state *state = NULL;
1656
1657 /* If this request hasn't been cancelled, do nothing */
1658 if (data->cancelled == 0)
1659 goto out_free;
1660 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001661 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662 goto out_free;
1663 /* In case we need an open_confirm, no cleanup! */
1664 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1665 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001667 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001668 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001669out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001670 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001671}
1672
1673static const struct rpc_call_ops nfs4_open_ops = {
1674 .rpc_call_prepare = nfs4_open_prepare,
1675 .rpc_call_done = nfs4_open_done,
1676 .rpc_release = nfs4_open_release,
1677};
1678
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001679static const struct rpc_call_ops nfs4_recover_open_ops = {
1680 .rpc_call_prepare = nfs4_recover_open_prepare,
1681 .rpc_call_done = nfs4_open_done,
1682 .rpc_release = nfs4_open_release,
1683};
1684
1685static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001686{
1687 struct inode *dir = data->dir->d_inode;
1688 struct nfs_server *server = NFS_SERVER(dir);
1689 struct nfs_openargs *o_arg = &data->o_arg;
1690 struct nfs_openres *o_res = &data->o_res;
1691 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001692 struct rpc_message msg = {
1693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1694 .rpc_argp = o_arg,
1695 .rpc_resp = o_res,
1696 .rpc_cred = data->owner->so_cred,
1697 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001698 struct rpc_task_setup task_setup_data = {
1699 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001700 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001701 .callback_ops = &nfs4_open_ops,
1702 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001703 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001704 .flags = RPC_TASK_ASYNC,
1705 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001706 int status;
1707
Trond Myklebust9d12b212012-01-17 22:04:25 -05001708 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001709 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001710 data->rpc_done = 0;
1711 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001712 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001713 if (isrecover)
1714 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001715 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001716 if (IS_ERR(task))
1717 return PTR_ERR(task);
1718 status = nfs4_wait_for_completion_rpc_task(task);
1719 if (status != 0) {
1720 data->cancelled = 1;
1721 smp_wmb();
1722 } else
1723 status = data->rpc_status;
1724 rpc_put_task(task);
1725
1726 return status;
1727}
1728
1729static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1730{
1731 struct inode *dir = data->dir->d_inode;
1732 struct nfs_openres *o_res = &data->o_res;
1733 int status;
1734
1735 status = nfs4_run_open_task(data, 1);
1736 if (status != 0 || !data->rpc_done)
1737 return status;
1738
Trond Myklebust6926afd2012-01-07 13:22:46 -05001739 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1740
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001741 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1742 status = _nfs4_proc_open_confirm(data);
1743 if (status != 0)
1744 return status;
1745 }
1746
1747 return status;
1748}
1749
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001750static int nfs4_opendata_access(struct rpc_cred *cred,
1751 struct nfs4_opendata *opendata,
1752 struct nfs4_state *state, fmode_t fmode)
1753{
1754 struct nfs_access_entry cache;
1755 u32 mask;
1756
1757 /* access call failed or for some reason the server doesn't
1758 * support any access modes -- defer access call until later */
1759 if (opendata->o_res.access_supported == 0)
1760 return 0;
1761
1762 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001763 /* don't check MAY_WRITE - a newly created file may not have
1764 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001765 if (fmode & FMODE_READ)
1766 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001767 if (fmode & FMODE_EXEC)
1768 mask |= MAY_EXEC;
1769
1770 cache.cred = cred;
1771 cache.jiffies = jiffies;
1772 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1773 nfs_access_add_cache(state->inode, &cache);
1774
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001775 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001776 return 0;
1777
1778 /* even though OPEN succeeded, access is denied. Close the file */
1779 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001780 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001781}
1782
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001783/*
1784 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1785 */
1786static int _nfs4_proc_open(struct nfs4_opendata *data)
1787{
1788 struct inode *dir = data->dir->d_inode;
1789 struct nfs_server *server = NFS_SERVER(dir);
1790 struct nfs_openargs *o_arg = &data->o_arg;
1791 struct nfs_openres *o_res = &data->o_res;
1792 int status;
1793
1794 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001795 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001796 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001797 if (status != 0) {
1798 if (status == -NFS4ERR_BADNAME &&
1799 !(o_arg->open_flags & O_CREAT))
1800 return -ENOENT;
1801 return status;
1802 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001803
Trond Myklebust6926afd2012-01-07 13:22:46 -05001804 nfs_fattr_map_and_free_names(server, &data->f_attr);
1805
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001806 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001807 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001808 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1809 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001811 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001813 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001816 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Andy Adamsond83217c2011-03-01 01:34:17 +00001820static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001821{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001822 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001823 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001824
Trond Myklebusta78cb572009-08-09 15:06:19 -04001825 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001826 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001827 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001828 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001829 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1830 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001831 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001832 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001833 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001834 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001835 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001836}
1837
Andy Adamsond83217c2011-03-01 01:34:17 +00001838static int nfs4_recover_expired_lease(struct nfs_server *server)
1839{
1840 return nfs4_client_recover_expired_lease(server->nfs_client);
1841}
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843/*
1844 * OPEN_EXPIRED:
1845 * reclaim state on the server after a network partition.
1846 * Assumes caller holds the appropriate lock
1847 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001848static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001850 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001851 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001853 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1854 if (IS_ERR(opendata))
1855 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001856 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001857 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001858 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001859 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001860 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001863static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001864{
Trond Myklebust539cd032007-06-05 11:46:42 -04001865 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001866 struct nfs4_exception exception = { };
1867 int err;
1868
1869 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001870 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001871 switch (err) {
1872 default:
1873 goto out;
1874 case -NFS4ERR_GRACE:
1875 case -NFS4ERR_DELAY:
1876 nfs4_handle_exception(server, err, &exception);
1877 err = 0;
1878 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001879 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001880out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001881 return err;
1882}
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1885{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001887 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Trond Myklebust864472e2006-01-03 09:55:15 +01001889 ctx = nfs4_state_find_open_context(state);
1890 if (IS_ERR(ctx))
1891 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001892 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001893 put_nfs_open_context(ctx);
1894 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001897#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001898static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001899{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001900 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001901 nfs4_stateid *stateid = &state->stateid;
1902 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001903
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001904 /* If a state reset has been done, test_stateid is unneeded */
1905 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1906 return;
1907
1908 status = nfs41_test_stateid(server, stateid);
1909 if (status != NFS_OK) {
1910 /* Free the stateid unless the server explicitly
1911 * informs us the stateid is unrecognized. */
1912 if (status != -NFS4ERR_BAD_STATEID)
1913 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001914 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001915
Bryan Schumaker69388672012-09-26 15:25:52 -04001916 write_seqlock(&state->seqlock);
1917 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1918 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001919 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1920 }
1921}
1922
1923/**
1924 * nfs41_check_open_stateid - possibly free an open stateid
1925 *
1926 * @state: NFSv4 state for an inode
1927 *
1928 * Returns NFS_OK if recovery for this stateid is now finished.
1929 * Otherwise a negative NFS4ERR value is returned.
1930 */
1931static int nfs41_check_open_stateid(struct nfs4_state *state)
1932{
1933 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001934 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001935 int status;
1936
1937 /* If a state reset has been done, test_stateid is unneeded */
1938 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1939 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1940 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1941 return -NFS4ERR_BAD_STATEID;
1942
1943 status = nfs41_test_stateid(server, stateid);
1944 if (status != NFS_OK) {
1945 /* Free the stateid unless the server explicitly
1946 * informs us the stateid is unrecognized. */
1947 if (status != -NFS4ERR_BAD_STATEID)
1948 nfs41_free_stateid(server, stateid);
1949
1950 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1951 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1952 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001953 }
1954 return status;
1955}
1956
1957static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1958{
Chuck Levereb64cf92012-07-11 16:30:05 -04001959 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001960
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001961 nfs41_clear_delegation_stateid(state);
1962 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001963 if (status != NFS_OK)
1964 status = nfs4_open_expired(sp, state);
1965 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001966}
1967#endif
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001970 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1971 * fields corresponding to attributes that were used to store the verifier.
1972 * Make sure we clobber those fields in the later setattr call
1973 */
1974static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1975{
1976 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1977 !(sattr->ia_valid & ATTR_ATIME_SET))
1978 sattr->ia_valid |= ATTR_ATIME;
1979
1980 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1981 !(sattr->ia_valid & ATTR_MTIME_SET))
1982 sattr->ia_valid |= ATTR_MTIME;
1983}
1984
1985/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001986 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 */
Andy Adamson82be4172012-05-23 05:02:35 -04001988static int _nfs4_do_open(struct inode *dir,
1989 struct dentry *dentry,
1990 fmode_t fmode,
1991 int flags,
1992 struct iattr *sattr,
1993 struct rpc_cred *cred,
1994 struct nfs4_state **res,
1995 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
1997 struct nfs4_state_owner *sp;
1998 struct nfs4_state *state = NULL;
1999 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002000 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002001 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002005 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2006 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2008 goto out_err;
2009 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002010 status = nfs4_recover_expired_lease(server);
2011 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002012 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002013 if (dentry->d_inode != NULL)
2014 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002015 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04002016 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002017 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002018 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Andy Adamson82be4172012-05-23 05:02:35 -04002020 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2021 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2022 if (!opendata->f_attr.mdsthreshold)
2023 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002024 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002025 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002026 if (dentry->d_inode != NULL)
2027 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002028
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002029 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002031 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002033 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002034 status = PTR_ERR(state);
2035 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002036 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002037 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002038 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002039
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002040 status = nfs4_opendata_access(cred, opendata, state, fmode);
2041 if (status != 0)
2042 goto err_opendata_put;
2043
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002044 if (opendata->o_arg.open_flags & O_EXCL) {
2045 nfs4_exclusive_attrset(opendata, sattr);
2046
2047 nfs_fattr_init(opendata->o_res.f_attr);
2048 status = nfs4_do_setattr(state->inode, cred,
2049 opendata->o_res.f_attr, sattr,
2050 state);
2051 if (status == 0)
2052 nfs_setattr_update_inode(state->inode, sattr);
2053 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2054 }
Andy Adamson82be4172012-05-23 05:02:35 -04002055
2056 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2057 *ctx_th = opendata->f_attr.mdsthreshold;
2058 else
2059 kfree(opendata->f_attr.mdsthreshold);
2060 opendata->f_attr.mdsthreshold = NULL;
2061
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002062 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 *res = state;
2065 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002066err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002067 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002068 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002069err_put_state_owner:
2070 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 *res = NULL;
2073 return status;
2074}
2075
2076
Andy Adamson82be4172012-05-23 05:02:35 -04002077static struct nfs4_state *nfs4_do_open(struct inode *dir,
2078 struct dentry *dentry,
2079 fmode_t fmode,
2080 int flags,
2081 struct iattr *sattr,
2082 struct rpc_cred *cred,
2083 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 struct nfs4_exception exception = { };
2086 struct nfs4_state *res;
2087 int status;
2088
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002089 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002091 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2092 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (status == 0)
2094 break;
2095 /* NOTE: BAD_SEQID means the server and client disagree about the
2096 * book-keeping w.r.t. state-changing operations
2097 * (OPEN/CLOSE/LOCK/LOCKU...)
2098 * It is actually a sign of a bug on the client or on the server.
2099 *
2100 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002101 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 * have unhashed the old state_owner for us, and that we can
2103 * therefore safely retry using a new one. We should still warn
2104 * the user though...
2105 */
2106 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002107 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002108 " returned a bad sequence-id error!\n",
2109 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 exception.retry = 1;
2111 continue;
2112 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002113 /*
2114 * BAD_STATEID on OPEN means that the server cancelled our
2115 * state before it received the OPEN_CONFIRM.
2116 * Recover by retrying the request as per the discussion
2117 * on Page 181 of RFC3530.
2118 */
2119 if (status == -NFS4ERR_BAD_STATEID) {
2120 exception.retry = 1;
2121 continue;
2122 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002123 if (status == -EAGAIN) {
2124 /* We must have found a delegation */
2125 exception.retry = 1;
2126 continue;
2127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2129 status, &exception));
2130 } while (exception.retry);
2131 return res;
2132}
2133
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002134static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2135 struct nfs_fattr *fattr, struct iattr *sattr,
2136 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002138 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002140 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 .iap = sattr,
2142 .server = server,
2143 .bitmask = server->attr_bitmask,
2144 };
2145 struct nfs_setattrres res = {
2146 .fattr = fattr,
2147 .server = server,
2148 };
2149 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2151 .rpc_argp = &arg,
2152 .rpc_resp = &res,
2153 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002155 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002156 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Trond Myklebust0e574af2005-10-27 22:12:38 -04002158 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Trond Myklebust4fc87962012-03-08 17:42:01 -05002160 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002161 struct nfs_lockowner lockowner = {
2162 .l_owner = current->files,
2163 .l_pid = current->tgid,
2164 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002165 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002166 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002167 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2168 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002169 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002170 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002171 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Bryan Schumaker7c513052011-03-24 17:12:24 +00002173 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002174 if (status == 0 && state != NULL)
2175 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002176 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002179static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2180 struct nfs_fattr *fattr, struct iattr *sattr,
2181 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002183 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002184 struct nfs4_exception exception = {
2185 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002186 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002187 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 int err;
2189 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002190 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2191 switch (err) {
2192 case -NFS4ERR_OPENMODE:
2193 if (state && !(state->state & FMODE_WRITE)) {
2194 err = -EBADF;
2195 if (sattr->ia_valid & ATTR_OPEN)
2196 err = -EACCES;
2197 goto out;
2198 }
2199 }
2200 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002202out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return err;
2204}
2205
2206struct nfs4_closedata {
2207 struct inode *inode;
2208 struct nfs4_state *state;
2209 struct nfs_closeargs arg;
2210 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002211 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002212 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002213 bool roc;
2214 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215};
2216
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002217static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002218{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002219 struct nfs4_closedata *calldata = data;
2220 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002221 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002222
Fred Isamanf7e89172011-01-06 11:36:32 +00002223 if (calldata->roc)
2224 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002225 nfs4_put_open_state(calldata->state);
2226 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002227 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002228 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002229 kfree(calldata);
2230}
2231
Trond Myklebust88069f72009-12-08 08:33:16 -05002232static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2233 fmode_t fmode)
2234{
2235 spin_lock(&state->owner->so_lock);
2236 if (!(fmode & FMODE_READ))
2237 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2238 if (!(fmode & FMODE_WRITE))
2239 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2240 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2241 spin_unlock(&state->owner->so_lock);
2242}
2243
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002244static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002246 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 struct nfs_server *server = NFS_SERVER(calldata->inode);
2249
Chuck Levera3ca5652012-03-01 17:00:40 -05002250 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002251 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2252 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 /* hmm. we are done with the inode, and in the process of freeing
2254 * the state_owner. we keep this around to process errors
2255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 switch (task->tk_status) {
2257 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002258 if (calldata->roc)
2259 pnfs_roc_set_barrier(state->inode,
2260 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002261 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002262 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002263 nfs4_close_clear_stateid_flags(state,
2264 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 break;
2266 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002267 case -NFS4ERR_OLD_STATEID:
2268 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002270 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002271 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002273 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2274 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002276 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002277 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002278 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002281static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002283 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002284 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002285 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002286 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002287
Chuck Levera3ca5652012-03-01 17:00:40 -05002288 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002289 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002290 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002291
Trond Myklebust88069f72009-12-08 08:33:16 -05002292 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2293 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002294 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002295 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002296 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002297 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002298 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2299 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2300 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002301 }
2302 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002303 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2304 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2305 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002306 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002307 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002308 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002309
2310 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002311 /* Note: exit _without_ calling nfs4_close_done */
2312 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002313 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002314 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002315
Fred Isamanf7e89172011-01-06 11:36:32 +00002316 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002317 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002318 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002319 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002320 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002321 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002322
Trond Myklebust516a6af2005-10-27 22:12:41 -04002323 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002324 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002325 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002326 &calldata->arg.seq_args,
2327 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002328 task) != 0)
2329 nfs_release_seqid(calldata->arg.seqid);
2330 else
2331 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002332out:
2333 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002336static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002337 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002338 .rpc_call_done = nfs4_close_done,
2339 .rpc_release = nfs4_free_closedata,
2340};
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342/*
2343 * It is possible for data to be read/written from a mem-mapped file
2344 * after the sys_close call (which hits the vfs layer as a flush).
2345 * This means that we can't safely call nfsv4 close on a file until
2346 * the inode is cleared. This in turn means that we are not good
2347 * NFSv4 citizens - we do not indicate to the server to update the file's
2348 * share state even when we are done with one of the three share
2349 * stateid's in the inode.
2350 *
2351 * NOTE: Caller must be holding the sp->so_owner semaphore!
2352 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002353int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002355 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002357 struct nfs4_state_owner *sp = state->owner;
2358 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002359 struct rpc_message msg = {
2360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2361 .rpc_cred = state->owner->so_cred,
2362 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002363 struct rpc_task_setup task_setup_data = {
2364 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002365 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002366 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002367 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002368 .flags = RPC_TASK_ASYNC,
2369 };
Trond Myklebust95121352005-10-18 14:20:12 -07002370 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002372 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002374 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002375 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002376 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002378 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002379 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002381 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002382 if (calldata->arg.seqid == NULL)
2383 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002384 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002385 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002386 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002387 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002388 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002389 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002390 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002391
Trond Myklebust1174dd12010-12-21 10:52:24 -05002392 msg.rpc_argp = &calldata->arg;
2393 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002394 task_setup_data.callback_data = calldata;
2395 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002396 if (IS_ERR(task))
2397 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002398 status = 0;
2399 if (wait)
2400 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002401 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002402 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002403out_free_calldata:
2404 kfree(calldata);
2405out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002406 nfs4_put_open_state(state);
2407 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002408 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
Trond Myklebust2b484292010-09-17 10:56:51 -04002411static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002412nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 struct nfs4_state *state;
2415
Trond Myklebust565277f2007-10-15 18:17:53 -04002416 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002417 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2418 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002419 if (IS_ERR(state))
2420 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002421 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002422 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424
Trond Myklebust1185a552009-12-03 15:54:02 -05002425static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002426{
2427 if (ctx->state == NULL)
2428 return;
2429 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002430 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002431 else
Al Viro643168c2011-06-22 18:20:23 -04002432 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2436{
Benny Halevy43652ad2009-04-01 09:21:54 -04002437 struct nfs4_server_caps_arg args = {
2438 .fhandle = fhandle,
2439 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 struct nfs4_server_caps_res res = {};
2441 struct rpc_message msg = {
2442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002443 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 .rpc_resp = &res,
2445 };
2446 int status;
2447
Bryan Schumaker7c513052011-03-24 17:12:24 +00002448 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if (status == 0) {
2450 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002451 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2452 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2453 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2454 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2455 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2457 server->caps |= NFS_CAP_ACLS;
2458 if (res.has_links != 0)
2459 server->caps |= NFS_CAP_HARDLINKS;
2460 if (res.has_symlinks != 0)
2461 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002462 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2463 server->caps |= NFS_CAP_FILEID;
2464 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2465 server->caps |= NFS_CAP_MODE;
2466 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2467 server->caps |= NFS_CAP_NLINK;
2468 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2469 server->caps |= NFS_CAP_OWNER;
2470 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2471 server->caps |= NFS_CAP_OWNER_GROUP;
2472 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2473 server->caps |= NFS_CAP_ATIME;
2474 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2475 server->caps |= NFS_CAP_CTIME;
2476 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2477 server->caps |= NFS_CAP_MTIME;
2478
Trond Myklebusta65318b2009-03-11 14:10:28 -04002479 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2480 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2481 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002483 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return status;
2487}
2488
Trond Myklebust55a97592006-06-09 09:34:19 -04002489int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491 struct nfs4_exception exception = { };
2492 int err;
2493 do {
2494 err = nfs4_handle_exception(server,
2495 _nfs4_server_capabilities(server, fhandle),
2496 &exception);
2497 } while (exception.retry);
2498 return err;
2499}
2500
2501static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2502 struct nfs_fsinfo *info)
2503{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 struct nfs4_lookup_root_arg args = {
2505 .bitmask = nfs4_fattr_bitmap,
2506 };
2507 struct nfs4_lookup_res res = {
2508 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002509 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 .fh = fhandle,
2511 };
2512 struct rpc_message msg = {
2513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2514 .rpc_argp = &args,
2515 .rpc_resp = &res,
2516 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002517
Trond Myklebust0e574af2005-10-27 22:12:38 -04002518 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002519 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
2522static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2523 struct nfs_fsinfo *info)
2524{
2525 struct nfs4_exception exception = { };
2526 int err;
2527 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002528 err = _nfs4_lookup_root(server, fhandle, info);
2529 switch (err) {
2530 case 0:
2531 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002532 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002533 default:
2534 err = nfs4_handle_exception(server, err, &exception);
2535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002537out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return err;
2539}
2540
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002541static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2542 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2543{
2544 struct rpc_auth *auth;
2545 int ret;
2546
2547 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002548 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002549 ret = -EIO;
2550 goto out;
2551 }
2552 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002553out:
2554 return ret;
2555}
2556
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002557static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002558 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002560 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002561 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002563 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002564 if (len < 0)
2565 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002566
2567 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002568 /* AUTH_UNIX is the default flavor if none was specified,
2569 * thus has already been tried. */
2570 if (flav_array[i] == RPC_AUTH_UNIX)
2571 continue;
2572
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002573 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002574 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002575 continue;
2576 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002577 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002578 /*
2579 * -EACCESS could mean that the user doesn't have correct permissions
2580 * to access the mount. It could also mean that we tried to mount
2581 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2582 * existing mount programs don't handle -EACCES very well so it should
2583 * be mapped to -EPERM instead.
2584 */
2585 if (status == -EACCES)
2586 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002587 return status;
2588}
2589
2590/*
2591 * get the file handle for the "/" directory on the server
2592 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002593int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2594 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002595{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002596 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002597 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002598 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2599 /*
2600 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2601 * by nfs4_map_errors() as this function exits.
2602 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002603 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (status == 0)
2605 status = nfs4_server_capabilities(server, fhandle);
2606 if (status == 0)
2607 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002608 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
Bryan Schumakerbae36242012-05-10 15:07:31 -04002611static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2612 struct nfs_fsinfo *info)
2613{
2614 int error;
2615 struct nfs_fattr *fattr = info->fattr;
2616
2617 error = nfs4_server_capabilities(server, mntfh);
2618 if (error < 0) {
2619 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2620 return error;
2621 }
2622
2623 error = nfs4_proc_getattr(server, mntfh, fattr);
2624 if (error < 0) {
2625 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2626 return error;
2627 }
2628
2629 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2630 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2631 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2632
2633 return error;
2634}
2635
Manoj Naik6b97fd32006-06-09 09:34:29 -04002636/*
2637 * Get locations and (maybe) other attributes of a referral.
2638 * Note that we'll actually follow the referral later when
2639 * we detect fsid mismatch in inode revalidation
2640 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002641static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2642 const struct qstr *name, struct nfs_fattr *fattr,
2643 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002644{
2645 int status = -ENOMEM;
2646 struct page *page = NULL;
2647 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002648
2649 page = alloc_page(GFP_KERNEL);
2650 if (page == NULL)
2651 goto out;
2652 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2653 if (locations == NULL)
2654 goto out;
2655
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002656 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002657 if (status != 0)
2658 goto out;
2659 /* Make sure server returned a different fsid for the referral */
2660 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002661 dprintk("%s: server did not return a different fsid for"
2662 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002663 status = -EIO;
2664 goto out;
2665 }
Andy Adamson533eb462011-06-13 18:25:56 -04002666 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2667 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002668
Andy Adamson533eb462011-06-13 18:25:56 -04002669 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002670 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002671 memset(fhandle, 0, sizeof(struct nfs_fh));
2672out:
2673 if (page)
2674 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002675 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002676 return status;
2677}
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2680{
2681 struct nfs4_getattr_arg args = {
2682 .fh = fhandle,
2683 .bitmask = server->attr_bitmask,
2684 };
2685 struct nfs4_getattr_res res = {
2686 .fattr = fattr,
2687 .server = server,
2688 };
2689 struct rpc_message msg = {
2690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2691 .rpc_argp = &args,
2692 .rpc_resp = &res,
2693 };
2694
Trond Myklebust0e574af2005-10-27 22:12:38 -04002695 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002696 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
2699static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2700{
2701 struct nfs4_exception exception = { };
2702 int err;
2703 do {
2704 err = nfs4_handle_exception(server,
2705 _nfs4_proc_getattr(server, fhandle, fattr),
2706 &exception);
2707 } while (exception.retry);
2708 return err;
2709}
2710
2711/*
2712 * The file is not closed if it is opened due to the a request to change
2713 * the size of the file. The open call will not be needed once the
2714 * VFS layer lookup-intents are implemented.
2715 *
2716 * Close is called when the inode is destroyed.
2717 * If we haven't opened the file for O_WRONLY, we
2718 * need to in the size_change case to obtain a stateid.
2719 *
2720 * Got race?
2721 * Because OPEN is always done by name in nfsv4, it is
2722 * possible that we opened a different file by the same
2723 * name. We can recognize this race condition, but we
2724 * can't do anything about it besides returning an error.
2725 *
2726 * This will be fixed with VFS changes (lookup-intent).
2727 */
2728static int
2729nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2730 struct iattr *sattr)
2731{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002732 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002733 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002734 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 int status;
2736
Benny Halevy8a1636c2010-07-14 15:43:57 -04002737 if (pnfs_ld_layoutret_on_setattr(inode))
2738 pnfs_return_layout(inode);
2739
Trond Myklebust0e574af2005-10-27 22:12:38 -04002740 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Andy Adamson26699402012-05-30 16:12:24 -04002742 /* Deal with open(O_TRUNC) */
2743 if (sattr->ia_valid & ATTR_OPEN)
2744 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2745
2746 /* Optimization: if the end result is no change, don't RPC */
2747 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2748 return 0;
2749
Trond Myklebustd5308382005-11-04 15:33:38 -05002750 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002751 if (sattr->ia_valid & ATTR_FILE) {
2752 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002753
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002754 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002755 if (ctx) {
2756 cred = ctx->cred;
2757 state = ctx->state;
2758 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002759 }
2760
2761 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002762 if (status == 0)
2763 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return status;
2765}
2766
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002767static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2768 const struct qstr *name, struct nfs_fh *fhandle,
2769 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002770{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002771 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002772 int status;
2773 struct nfs4_lookup_arg args = {
2774 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002775 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002776 .name = name,
2777 };
2778 struct nfs4_lookup_res res = {
2779 .server = server,
2780 .fattr = fattr,
2781 .fh = fhandle,
2782 };
2783 struct rpc_message msg = {
2784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2785 .rpc_argp = &args,
2786 .rpc_resp = &res,
2787 };
2788
2789 nfs_fattr_init(fattr);
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002792 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 dprintk("NFS reply lookup: %d\n", status);
2794 return status;
2795}
2796
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002797static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002798{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002799 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002800 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002801 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2802 fattr->nlink = 2;
2803}
2804
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002805static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2806 struct qstr *name, struct nfs_fh *fhandle,
2807 struct nfs_fattr *fattr)
2808{
2809 struct nfs4_exception exception = { };
2810 struct rpc_clnt *client = *clnt;
2811 int err;
2812 do {
2813 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2814 switch (err) {
2815 case -NFS4ERR_BADNAME:
2816 err = -ENOENT;
2817 goto out;
2818 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002819 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002820 goto out;
2821 case -NFS4ERR_WRONGSEC:
2822 err = -EPERM;
2823 if (client != *clnt)
2824 goto out;
2825
2826 client = nfs4_create_sec_client(client, dir, name);
2827 if (IS_ERR(client))
2828 return PTR_ERR(client);
2829
2830 exception.retry = 1;
2831 break;
2832 default:
2833 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2834 }
2835 } while (exception.retry);
2836
2837out:
2838 if (err == 0)
2839 *clnt = client;
2840 else if (client != *clnt)
2841 rpc_shutdown_client(client);
2842
2843 return err;
2844}
2845
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002846static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002847 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002849 int status;
2850 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002851
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002852 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2853 if (client != NFS_CLIENT(dir)) {
2854 rpc_shutdown_client(client);
2855 nfs_fixup_secinfo_attributes(fattr);
2856 }
2857 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858}
2859
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002860struct rpc_clnt *
2861nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2862 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2863{
2864 int status;
2865 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2866
2867 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2868 if (status < 0) {
2869 rpc_shutdown_client(client);
2870 return ERR_PTR(status);
2871 }
2872 return client;
2873}
2874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2876{
Trond Myklebust76b32992007-08-10 17:45:11 -04002877 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 struct nfs4_accessargs args = {
2879 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002880 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002882 struct nfs4_accessres res = {
2883 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002884 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 struct rpc_message msg = {
2886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2887 .rpc_argp = &args,
2888 .rpc_resp = &res,
2889 .rpc_cred = entry->cred,
2890 };
2891 int mode = entry->mask;
2892 int status;
2893
2894 /*
2895 * Determine which access bits we want to ask for...
2896 */
2897 if (mode & MAY_READ)
2898 args.access |= NFS4_ACCESS_READ;
2899 if (S_ISDIR(inode->i_mode)) {
2900 if (mode & MAY_WRITE)
2901 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2902 if (mode & MAY_EXEC)
2903 args.access |= NFS4_ACCESS_LOOKUP;
2904 } else {
2905 if (mode & MAY_WRITE)
2906 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2907 if (mode & MAY_EXEC)
2908 args.access |= NFS4_ACCESS_EXECUTE;
2909 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002910
2911 res.fattr = nfs_alloc_fattr();
2912 if (res.fattr == NULL)
2913 return -ENOMEM;
2914
Bryan Schumaker7c513052011-03-24 17:12:24 +00002915 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002917 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002918 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002920 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return status;
2922}
2923
2924static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2925{
2926 struct nfs4_exception exception = { };
2927 int err;
2928 do {
2929 err = nfs4_handle_exception(NFS_SERVER(inode),
2930 _nfs4_proc_access(inode, entry),
2931 &exception);
2932 } while (exception.retry);
2933 return err;
2934}
2935
2936/*
2937 * TODO: For the time being, we don't try to get any attributes
2938 * along with any of the zero-copy operations READ, READDIR,
2939 * READLINK, WRITE.
2940 *
2941 * In the case of the first three, we want to put the GETATTR
2942 * after the read-type operation -- this is because it is hard
2943 * to predict the length of a GETATTR response in v4, and thus
2944 * align the READ data correctly. This means that the GETATTR
2945 * may end up partially falling into the page cache, and we should
2946 * shift it into the 'tail' of the xdr_buf before processing.
2947 * To do this efficiently, we need to know the total length
2948 * of data received, which doesn't seem to be available outside
2949 * of the RPC layer.
2950 *
2951 * In the case of WRITE, we also want to put the GETATTR after
2952 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002953 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 *
2955 * Both of these changes to the XDR layer would in fact be quite
2956 * minor, but I decided to leave them for a subsequent patch.
2957 */
2958static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2959 unsigned int pgbase, unsigned int pglen)
2960{
2961 struct nfs4_readlink args = {
2962 .fh = NFS_FH(inode),
2963 .pgbase = pgbase,
2964 .pglen = pglen,
2965 .pages = &page,
2966 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002967 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 struct rpc_message msg = {
2969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2970 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002971 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 };
2973
Bryan Schumaker7c513052011-03-24 17:12:24 +00002974 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 -07002975}
2976
2977static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2978 unsigned int pgbase, unsigned int pglen)
2979{
2980 struct nfs4_exception exception = { };
2981 int err;
2982 do {
2983 err = nfs4_handle_exception(NFS_SERVER(inode),
2984 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2985 &exception);
2986 } while (exception.retry);
2987 return err;
2988}
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002991 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993static int
2994nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002995 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002997 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 int status = 0;
3000
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003001 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3002 if (IS_ERR(ctx))
3003 return PTR_ERR(ctx);
3004
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003005 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003006 state = nfs4_do_open(dir, dentry, ctx->mode,
3007 flags, sattr, ctx->cred,
3008 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003009 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (IS_ERR(state)) {
3011 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003014 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003015 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003016 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003018 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return status;
3020}
3021
3022static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3023{
Trond Myklebust16e42952005-10-27 22:12:44 -04003024 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003025 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003027 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003029 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003030 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003031 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3034 .rpc_argp = &args,
3035 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003037 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003038
Bryan Schumaker7c513052011-03-24 17:12:24 +00003039 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003040 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003041 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return status;
3043}
3044
3045static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3046{
3047 struct nfs4_exception exception = { };
3048 int err;
3049 do {
3050 err = nfs4_handle_exception(NFS_SERVER(dir),
3051 _nfs4_proc_remove(dir, name),
3052 &exception);
3053 } while (exception.retry);
3054 return err;
3055}
3056
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003057static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003059 struct nfs_server *server = NFS_SERVER(dir);
3060 struct nfs_removeargs *args = msg->rpc_argp;
3061 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003063 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003065 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003068static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3069{
3070 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3071 &data->args.seq_args,
3072 &data->res.seq_res,
3073 task))
3074 return;
3075 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003078static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003080 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3081
Trond Myklebust14516c32010-07-31 14:29:06 -04003082 if (!nfs4_sequence_done(task, &res->seq_res))
3083 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003084 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003085 return 0;
3086 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003087 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Jeff Laytond3d41522010-09-17 17:31:57 -04003090static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3091{
3092 struct nfs_server *server = NFS_SERVER(dir);
3093 struct nfs_renameargs *arg = msg->rpc_argp;
3094 struct nfs_renameres *res = msg->rpc_resp;
3095
3096 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003097 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003098 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003099}
3100
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003101static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3102{
3103 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3104 &data->args.seq_args,
3105 &data->res.seq_res,
3106 task))
3107 return;
3108 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003109}
3110
3111static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3112 struct inode *new_dir)
3113{
3114 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3115
3116 if (!nfs4_sequence_done(task, &res->seq_res))
3117 return 0;
3118 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3119 return 0;
3120
3121 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003122 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003123 return 1;
3124}
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3127 struct inode *new_dir, struct qstr *new_name)
3128{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003129 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003130 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 .old_dir = NFS_FH(old_dir),
3132 .new_dir = NFS_FH(new_dir),
3133 .old_name = old_name,
3134 .new_name = new_name,
3135 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003136 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003137 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003138 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 struct rpc_message msg = {
3140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3141 .rpc_argp = &arg,
3142 .rpc_resp = &res,
3143 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003144 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Bryan Schumaker7c513052011-03-24 17:12:24 +00003146 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (!status) {
3148 update_changeattr(old_dir, &res.old_cinfo);
3149 update_changeattr(new_dir, &res.new_cinfo);
3150 }
3151 return status;
3152}
3153
3154static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3155 struct inode *new_dir, struct qstr *new_name)
3156{
3157 struct nfs4_exception exception = { };
3158 int err;
3159 do {
3160 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3161 _nfs4_proc_rename(old_dir, old_name,
3162 new_dir, new_name),
3163 &exception);
3164 } while (exception.retry);
3165 return err;
3166}
3167
3168static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3169{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003170 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 struct nfs4_link_arg arg = {
3172 .fh = NFS_FH(inode),
3173 .dir_fh = NFS_FH(dir),
3174 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003175 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003177 struct nfs4_link_res res = {
3178 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003179 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 struct rpc_message msg = {
3181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3182 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003183 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003185 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Trond Myklebust136f2622010-04-16 16:22:49 -04003187 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003188 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003189 goto out;
3190
Bryan Schumaker7c513052011-03-24 17:12:24 +00003191 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003192 if (!status) {
3193 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003194 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003195 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003196out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003197 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 return status;
3199}
3200
3201static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3202{
3203 struct nfs4_exception exception = { };
3204 int err;
3205 do {
3206 err = nfs4_handle_exception(NFS_SERVER(inode),
3207 _nfs4_proc_link(inode, dir, name),
3208 &exception);
3209 } while (exception.retry);
3210 return err;
3211}
3212
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003213struct nfs4_createdata {
3214 struct rpc_message msg;
3215 struct nfs4_create_arg arg;
3216 struct nfs4_create_res res;
3217 struct nfs_fh fh;
3218 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003219};
3220
3221static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3222 struct qstr *name, struct iattr *sattr, u32 ftype)
3223{
3224 struct nfs4_createdata *data;
3225
3226 data = kzalloc(sizeof(*data), GFP_KERNEL);
3227 if (data != NULL) {
3228 struct nfs_server *server = NFS_SERVER(dir);
3229
3230 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3231 data->msg.rpc_argp = &data->arg;
3232 data->msg.rpc_resp = &data->res;
3233 data->arg.dir_fh = NFS_FH(dir);
3234 data->arg.server = server;
3235 data->arg.name = name;
3236 data->arg.attrs = sattr;
3237 data->arg.ftype = ftype;
3238 data->arg.bitmask = server->attr_bitmask;
3239 data->res.server = server;
3240 data->res.fh = &data->fh;
3241 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003242 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003243 }
3244 return data;
3245}
3246
3247static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3248{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003249 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003250 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003251 if (status == 0) {
3252 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003253 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3254 }
3255 return status;
3256}
3257
3258static void nfs4_free_createdata(struct nfs4_createdata *data)
3259{
3260 kfree(data);
3261}
3262
Chuck Lever4f390c12006-08-22 20:06:22 -04003263static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003264 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003266 struct nfs4_createdata *data;
3267 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Chuck Lever94a6d752006-08-22 20:06:23 -04003269 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003270 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003271
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003272 status = -ENOMEM;
3273 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3274 if (data == NULL)
3275 goto out;
3276
3277 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3278 data->arg.u.symlink.pages = &page;
3279 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003281 status = nfs4_do_create(dir, dentry, data);
3282
3283 nfs4_free_createdata(data);
3284out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return status;
3286}
3287
Chuck Lever4f390c12006-08-22 20:06:22 -04003288static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003289 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
3291 struct nfs4_exception exception = { };
3292 int err;
3293 do {
3294 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003295 _nfs4_proc_symlink(dir, dentry, page,
3296 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 &exception);
3298 } while (exception.retry);
3299 return err;
3300}
3301
3302static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3303 struct iattr *sattr)
3304{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003305 struct nfs4_createdata *data;
3306 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003308 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3309 if (data == NULL)
3310 goto out;
3311
3312 status = nfs4_do_create(dir, dentry, data);
3313
3314 nfs4_free_createdata(data);
3315out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 return status;
3317}
3318
3319static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3320 struct iattr *sattr)
3321{
3322 struct nfs4_exception exception = { };
3323 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003324
3325 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 do {
3327 err = nfs4_handle_exception(NFS_SERVER(dir),
3328 _nfs4_proc_mkdir(dir, dentry, sattr),
3329 &exception);
3330 } while (exception.retry);
3331 return err;
3332}
3333
3334static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003335 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
3337 struct inode *dir = dentry->d_inode;
3338 struct nfs4_readdir_arg args = {
3339 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003340 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 .pgbase = 0,
3342 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003343 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003344 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 };
3346 struct nfs4_readdir_res res;
3347 struct rpc_message msg = {
3348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3349 .rpc_argp = &args,
3350 .rpc_resp = &res,
3351 .rpc_cred = cred,
3352 };
3353 int status;
3354
Harvey Harrison3110ff82008-05-02 13:42:44 -07003355 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003356 dentry->d_parent->d_name.name,
3357 dentry->d_name.name,
3358 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003359 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003361 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 -05003362 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003363 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003364 status += args.pgbase;
3365 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003366
3367 nfs_invalidate_atime(dir);
3368
Harvey Harrison3110ff82008-05-02 13:42:44 -07003369 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 return status;
3371}
3372
3373static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003374 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375{
3376 struct nfs4_exception exception = { };
3377 int err;
3378 do {
3379 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3380 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003381 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 &exception);
3383 } while (exception.retry);
3384 return err;
3385}
3386
3387static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3388 struct iattr *sattr, dev_t rdev)
3389{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003390 struct nfs4_createdata *data;
3391 int mode = sattr->ia_mode;
3392 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003394 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3395 if (data == NULL)
3396 goto out;
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003399 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003401 data->arg.ftype = NF4BLK;
3402 data->arg.u.device.specdata1 = MAJOR(rdev);
3403 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 }
3405 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003406 data->arg.ftype = NF4CHR;
3407 data->arg.u.device.specdata1 = MAJOR(rdev);
3408 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003409 } else if (!S_ISSOCK(mode)) {
3410 status = -EINVAL;
3411 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003414 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003415out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003416 nfs4_free_createdata(data);
3417out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 return status;
3419}
3420
3421static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3422 struct iattr *sattr, dev_t rdev)
3423{
3424 struct nfs4_exception exception = { };
3425 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003426
3427 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 do {
3429 err = nfs4_handle_exception(NFS_SERVER(dir),
3430 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3431 &exception);
3432 } while (exception.retry);
3433 return err;
3434}
3435
3436static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3437 struct nfs_fsstat *fsstat)
3438{
3439 struct nfs4_statfs_arg args = {
3440 .fh = fhandle,
3441 .bitmask = server->attr_bitmask,
3442 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003443 struct nfs4_statfs_res res = {
3444 .fsstat = fsstat,
3445 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 struct rpc_message msg = {
3447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3448 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003449 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 };
3451
Trond Myklebust0e574af2005-10-27 22:12:38 -04003452 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003453 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
3455
3456static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3457{
3458 struct nfs4_exception exception = { };
3459 int err;
3460 do {
3461 err = nfs4_handle_exception(server,
3462 _nfs4_proc_statfs(server, fhandle, fsstat),
3463 &exception);
3464 } while (exception.retry);
3465 return err;
3466}
3467
3468static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3469 struct nfs_fsinfo *fsinfo)
3470{
3471 struct nfs4_fsinfo_arg args = {
3472 .fh = fhandle,
3473 .bitmask = server->attr_bitmask,
3474 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003475 struct nfs4_fsinfo_res res = {
3476 .fsinfo = fsinfo,
3477 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 struct rpc_message msg = {
3479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3480 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003481 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 };
3483
Bryan Schumaker7c513052011-03-24 17:12:24 +00003484 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
3487static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3488{
3489 struct nfs4_exception exception = { };
3490 int err;
3491
3492 do {
3493 err = nfs4_handle_exception(server,
3494 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3495 &exception);
3496 } while (exception.retry);
3497 return err;
3498}
3499
3500static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3501{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003502 int error;
3503
Trond Myklebust0e574af2005-10-27 22:12:38 -04003504 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003505 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003506 if (error == 0) {
3507 /* block layout checks this! */
3508 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003509 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003510 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003511
3512 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
3514
3515static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3516 struct nfs_pathconf *pathconf)
3517{
3518 struct nfs4_pathconf_arg args = {
3519 .fh = fhandle,
3520 .bitmask = server->attr_bitmask,
3521 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003522 struct nfs4_pathconf_res res = {
3523 .pathconf = pathconf,
3524 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 struct rpc_message msg = {
3526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3527 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003528 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 };
3530
3531 /* None of the pathconf attributes are mandatory to implement */
3532 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3533 memset(pathconf, 0, sizeof(*pathconf));
3534 return 0;
3535 }
3536
Trond Myklebust0e574af2005-10-27 22:12:38 -04003537 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003538 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539}
3540
3541static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3542 struct nfs_pathconf *pathconf)
3543{
3544 struct nfs4_exception exception = { };
3545 int err;
3546
3547 do {
3548 err = nfs4_handle_exception(server,
3549 _nfs4_proc_pathconf(server, fhandle, pathconf),
3550 &exception);
3551 } while (exception.retry);
3552 return err;
3553}
3554
Benny Halevyd20581a2011-05-22 19:52:03 +03003555void __nfs4_read_done_cb(struct nfs_read_data *data)
3556{
Fred Isamancd841602012-04-20 14:47:44 -04003557 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003558}
3559
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003560static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
Fred Isamancd841602012-04-20 14:47:44 -04003562 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003564 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003565 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003566 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003568
Benny Halevyd20581a2011-05-22 19:52:03 +03003569 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003571 renew_lease(server, data->timestamp);
3572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573}
3574
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003575static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3576{
3577
3578 dprintk("--> %s\n", __func__);
3579
3580 if (!nfs4_sequence_done(task, &data->res.seq_res))
3581 return -EAGAIN;
3582
Benny Halevyd20581a2011-05-22 19:52:03 +03003583 return data->read_done_cb ? data->read_done_cb(task, data) :
3584 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003585}
3586
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003587static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003590 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003591 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003592 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003595static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3596{
Fred Isamancd841602012-04-20 14:47:44 -04003597 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003598 &data->args.seq_args,
3599 &data->res.seq_res,
3600 task))
3601 return;
3602 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Fred Isamanb029bc92011-03-03 15:13:42 +00003605static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
Fred Isamancd841602012-04-20 14:47:44 -04003607 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003609 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003610 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003611 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003613 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003615 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003616 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
3619
Fred Isamanb029bc92011-03-03 15:13:42 +00003620static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3621{
3622 if (!nfs4_sequence_done(task, &data->res.seq_res))
3623 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003624 return data->write_done_cb ? data->write_done_cb(task, data) :
3625 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003626}
3627
Trond Myklebust5a37f852012-04-28 14:55:16 -04003628static
3629bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003630{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003631 const struct nfs_pgio_header *hdr = data->header;
3632
3633 /* Don't request attributes for pNFS or O_DIRECT writes */
3634 if (data->ds_clp != NULL || hdr->dreq != NULL)
3635 return false;
3636 /* Otherwise, request attributes if and only if we don't hold
3637 * a delegation
3638 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003639 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003640}
Fred Isamana69aef12011-03-03 15:13:47 +00003641
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003642static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643{
Fred Isamancd841602012-04-20 14:47:44 -04003644 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003645
Trond Myklebust5a37f852012-04-28 14:55:16 -04003646 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003647 data->args.bitmask = NULL;
3648 data->res.fattr = NULL;
3649 } else
3650 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003651
Fred Isamanb029bc92011-03-03 15:13:42 +00003652 if (!data->write_done_cb)
3653 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003654 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 data->timestamp = jiffies;
3656
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003657 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003658 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659}
3660
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003661static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3662{
Fred Isamancd841602012-04-20 14:47:44 -04003663 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003664 &data->args.seq_args,
3665 &data->res.seq_res,
3666 task))
3667 return;
3668 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669}
3670
Fred Isaman0b7c0152012-04-20 14:47:39 -04003671static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3672{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3674 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003675 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return;
3678 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003679}
3680
Fred Isaman0b7c0152012-04-20 14:47:39 -04003681static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682{
3683 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003684
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003685 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003686 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003687 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690}
3691
Fred Isaman0b7c0152012-04-20 14:47:39 -04003692static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003693{
3694 if (!nfs4_sequence_done(task, &data->res.seq_res))
3695 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003696 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003697}
3698
Fred Isaman0b7c0152012-04-20 14:47:39 -04003699static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003701 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003702
Fred Isaman0b7c0152012-04-20 14:47:39 -04003703 if (data->commit_done_cb == NULL)
3704 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003705 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003706 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003707 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
3709
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003710struct nfs4_renewdata {
3711 struct nfs_client *client;
3712 unsigned long timestamp;
3713};
3714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715/*
3716 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3717 * standalone procedure for queueing an asynchronous RENEW.
3718 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003719static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003720{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003721 struct nfs4_renewdata *data = calldata;
3722 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003723
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003724 if (atomic_read(&clp->cl_count) > 1)
3725 nfs4_schedule_state_renewal(clp);
3726 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003727 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003728}
3729
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003730static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003732 struct nfs4_renewdata *data = calldata;
3733 struct nfs_client *clp = data->client;
3734 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
3736 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003737 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003738 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3739 return;
3740 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003741 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003742 return;
3743 }
3744 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003746 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}
3748
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003749static const struct rpc_call_ops nfs4_renew_ops = {
3750 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003751 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003752};
3753
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003754static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
3756 struct rpc_message msg = {
3757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3758 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003759 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003761 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003763 if (renew_flags == 0)
3764 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003765 if (!atomic_inc_not_zero(&clp->cl_count))
3766 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003767 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003768 if (data == NULL)
3769 return -ENOMEM;
3770 data->client = clp;
3771 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003773 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774}
3775
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003776static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
3778 struct rpc_message msg = {
3779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3780 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003781 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 };
3783 unsigned long now = jiffies;
3784 int status;
3785
3786 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3787 if (status < 0)
3788 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003789 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return 0;
3791}
3792
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003793static inline int nfs4_server_supports_acls(struct nfs_server *server)
3794{
3795 return (server->caps & NFS_CAP_ACLS)
3796 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3797 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3798}
3799
Trond Myklebust21f498c2012-08-24 10:59:25 -04003800/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3801 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003802 * the stack.
3803 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003804#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003805
Neil Hormane9e3d722011-03-04 19:26:03 -05003806static int buf_to_pages_noslab(const void *buf, size_t buflen,
3807 struct page **pages, unsigned int *pgbase)
3808{
3809 struct page *newpage, **spages;
3810 int rc = 0;
3811 size_t len;
3812 spages = pages;
3813
3814 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003815 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003816 newpage = alloc_page(GFP_KERNEL);
3817
3818 if (newpage == NULL)
3819 goto unwind;
3820 memcpy(page_address(newpage), buf, len);
3821 buf += len;
3822 buflen -= len;
3823 *pages++ = newpage;
3824 rc++;
3825 } while (buflen != 0);
3826
3827 return rc;
3828
3829unwind:
3830 for(; rc > 0; rc--)
3831 __free_page(spages[rc-1]);
3832 return -ENOMEM;
3833}
3834
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003835struct nfs4_cached_acl {
3836 int cached;
3837 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003838 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003839};
3840
3841static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003842{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003843 struct nfs_inode *nfsi = NFS_I(inode);
3844
3845 spin_lock(&inode->i_lock);
3846 kfree(nfsi->nfs4_acl);
3847 nfsi->nfs4_acl = acl;
3848 spin_unlock(&inode->i_lock);
3849}
3850
3851static void nfs4_zap_acl_attr(struct inode *inode)
3852{
3853 nfs4_set_cached_acl(inode, NULL);
3854}
3855
3856static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3857{
3858 struct nfs_inode *nfsi = NFS_I(inode);
3859 struct nfs4_cached_acl *acl;
3860 int ret = -ENOENT;
3861
3862 spin_lock(&inode->i_lock);
3863 acl = nfsi->nfs4_acl;
3864 if (acl == NULL)
3865 goto out;
3866 if (buf == NULL) /* user is just asking for length */
3867 goto out_len;
3868 if (acl->cached == 0)
3869 goto out;
3870 ret = -ERANGE; /* see getxattr(2) man page */
3871 if (acl->len > buflen)
3872 goto out;
3873 memcpy(buf, acl->data, acl->len);
3874out_len:
3875 ret = acl->len;
3876out:
3877 spin_unlock(&inode->i_lock);
3878 return ret;
3879}
3880
Sachin Prabhu5794d212012-04-17 14:36:40 +01003881static 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 +00003882{
3883 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003884 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003885
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003886 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003887 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003888 if (acl == NULL)
3889 goto out;
3890 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003891 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003892 } else {
3893 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3894 if (acl == NULL)
3895 goto out;
3896 acl->cached = 0;
3897 }
3898 acl->len = acl_len;
3899out:
3900 nfs4_set_cached_acl(inode, acl);
3901}
3902
Andy Adamsonbf118a32011-12-07 11:55:27 -05003903/*
3904 * The getxattr API returns the required buffer length when called with a
3905 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3906 * the required buf. On a NULL buf, we send a page of data to the server
3907 * guessing that the ACL request can be serviced by a page. If so, we cache
3908 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3909 * the cache. If not so, we throw away the page, and cache the required
3910 * length. The next getxattr call will then produce another round trip to
3911 * the server, this time with the input buf of the required size.
3912 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003913static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003914{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003915 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003916 struct nfs_getaclargs args = {
3917 .fh = NFS_FH(inode),
3918 .acl_pages = pages,
3919 .acl_len = buflen,
3920 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003921 struct nfs_getaclres res = {
3922 .acl_len = buflen,
3923 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003924 struct rpc_message msg = {
3925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3926 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003927 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003928 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003929 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3930 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003931
Andy Adamsonbf118a32011-12-07 11:55:27 -05003932 /* As long as we're doing a round trip to the server anyway,
3933 * let's be prepared for a page of acl data. */
3934 if (npages == 0)
3935 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003936 if (npages > ARRAY_SIZE(pages))
3937 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003938
Andy Adamsonbf118a32011-12-07 11:55:27 -05003939 for (i = 0; i < npages; i++) {
3940 pages[i] = alloc_page(GFP_KERNEL);
3941 if (!pages[i])
3942 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003943 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003944
3945 /* for decoding across pages */
3946 res.acl_scratch = alloc_page(GFP_KERNEL);
3947 if (!res.acl_scratch)
3948 goto out_free;
3949
Andy Adamsonbf118a32011-12-07 11:55:27 -05003950 args.acl_len = npages * PAGE_SIZE;
3951 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003952
Peng Taode040be2012-01-10 22:42:47 +08003953 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003954 __func__, buf, buflen, npages, args.acl_len);
3955 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3956 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003957 if (ret)
3958 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003959
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003960 /* Handle the case where the passed-in buffer is too short */
3961 if (res.acl_flags & NFS4_ACL_TRUNC) {
3962 /* Did the user only issue a request for the acl length? */
3963 if (buf == NULL)
3964 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003965 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003966 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003967 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003968 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3969 if (buf)
3970 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3971out_ok:
3972 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003973out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003974 for (i = 0; i < npages; i++)
3975 if (pages[i])
3976 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003977 if (res.acl_scratch)
3978 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003979 return ret;
3980}
3981
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003982static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3983{
3984 struct nfs4_exception exception = { };
3985 ssize_t ret;
3986 do {
3987 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3988 if (ret >= 0)
3989 break;
3990 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3991 } while (exception.retry);
3992 return ret;
3993}
3994
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003995static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3996{
3997 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003998 int ret;
3999
4000 if (!nfs4_server_supports_acls(server))
4001 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004002 ret = nfs_revalidate_inode(server, inode);
4003 if (ret < 0)
4004 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004005 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4006 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004007 ret = nfs4_read_cached_acl(inode, buf, buflen);
4008 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004009 /* -ENOENT is returned if there is no ACL or if there is an ACL
4010 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004011 return ret;
4012 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004013}
4014
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004015static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004016{
4017 struct nfs_server *server = NFS_SERVER(inode);
4018 struct page *pages[NFS4ACL_MAXPAGES];
4019 struct nfs_setaclargs arg = {
4020 .fh = NFS_FH(inode),
4021 .acl_pages = pages,
4022 .acl_len = buflen,
4023 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004024 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004025 struct rpc_message msg = {
4026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4027 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004028 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004029 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004030 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004031 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004032
4033 if (!nfs4_server_supports_acls(server))
4034 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004035 if (npages > ARRAY_SIZE(pages))
4036 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004037 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4038 if (i < 0)
4039 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004040 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004041 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004042
4043 /*
4044 * Free each page after tx, so the only ref left is
4045 * held by the network stack
4046 */
4047 for (; i > 0; i--)
4048 put_page(pages[i-1]);
4049
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004050 /*
4051 * Acl update can result in inode attribute update.
4052 * so mark the attribute cache invalid.
4053 */
4054 spin_lock(&inode->i_lock);
4055 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4056 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004057 nfs_access_zap_cache(inode);
4058 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004059 return ret;
4060}
4061
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004062static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4063{
4064 struct nfs4_exception exception = { };
4065 int err;
4066 do {
4067 err = nfs4_handle_exception(NFS_SERVER(inode),
4068 __nfs4_proc_set_acl(inode, buf, buflen),
4069 &exception);
4070 } while (exception.retry);
4071 return err;
4072}
4073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004075nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004077 struct nfs_client *clp = server->nfs_client;
4078
4079 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 return 0;
4081 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004082 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004083 case -NFS4ERR_ADMIN_REVOKED:
4084 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004085 if (state == NULL)
4086 break;
4087 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004088 case -NFS4ERR_OPENMODE:
4089 if (state == NULL)
4090 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004091 nfs4_schedule_stateid_recovery(server, state);
4092 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004093 case -NFS4ERR_EXPIRED:
4094 if (state != NULL)
4095 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004097 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004098 nfs4_schedule_lease_recovery(clp);
4099 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004100#if defined(CONFIG_NFS_V4_1)
4101 case -NFS4ERR_BADSESSION:
4102 case -NFS4ERR_BADSLOT:
4103 case -NFS4ERR_BAD_HIGH_SLOT:
4104 case -NFS4ERR_DEADSESSION:
4105 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4106 case -NFS4ERR_SEQ_FALSE_RETRY:
4107 case -NFS4ERR_SEQ_MISORDERED:
4108 dprintk("%s ERROR %d, Reset session\n", __func__,
4109 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004110 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004111 task->tk_status = 0;
4112 return -EAGAIN;
4113#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004115 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004116 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004117 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4119 task->tk_status = 0;
4120 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004121 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 case -NFS4ERR_OLD_STATEID:
4123 task->tk_status = 0;
4124 return -EAGAIN;
4125 }
4126 task->tk_status = nfs4_map_errors(task->tk_status);
4127 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004128wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004129 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004130 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4131 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4132 task->tk_status = 0;
4133 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134}
4135
Chuck Leverf0920752012-05-21 22:45:41 -04004136static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4137 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004138{
4139 __be32 verf[2];
4140
Chuck Lever2c820d92012-05-21 22:45:33 -04004141 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4142 /* An impossible timestamp guarantees this value
4143 * will never match a generated boot time. */
4144 verf[0] = 0;
4145 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4146 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004147 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4148 verf[0] = (__be32)nn->boot_time.tv_sec;
4149 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004150 }
Chuck Levercd937102012-03-02 17:14:31 -05004151 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4152}
4153
Chuck Levere984a552012-09-14 17:24:21 -04004154static unsigned int
4155nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4156 char *buf, size_t len)
4157{
4158 unsigned int result;
4159
4160 rcu_read_lock();
4161 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4162 clp->cl_ipaddr,
4163 rpc_peeraddr2str(clp->cl_rpcclient,
4164 RPC_DISPLAY_ADDR),
4165 rpc_peeraddr2str(clp->cl_rpcclient,
4166 RPC_DISPLAY_PROTO));
4167 rcu_read_unlock();
4168 return result;
4169}
4170
4171static unsigned int
4172nfs4_init_uniform_client_string(const struct nfs_client *clp,
4173 char *buf, size_t len)
4174{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004175 char *nodename = clp->cl_rpcclient->cl_nodename;
4176
4177 if (nfs4_client_id_uniquifier[0] != '\0')
4178 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004179 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4180 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004181 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004182}
4183
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004184/**
4185 * nfs4_proc_setclientid - Negotiate client ID
4186 * @clp: state data structure
4187 * @program: RPC program for NFSv4 callback service
4188 * @port: IP port number for NFS4 callback service
4189 * @cred: RPC credential to use for this call
4190 * @res: where to place the result
4191 *
4192 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4193 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004194int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4195 unsigned short port, struct rpc_cred *cred,
4196 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
4198 nfs4_verifier sc_verifier;
4199 struct nfs4_setclientid setclientid = {
4200 .sc_verifier = &sc_verifier,
4201 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004202 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 };
4204 struct rpc_message msg = {
4205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4206 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004207 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004208 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004210 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
Chuck Leverde734832012-07-11 16:30:50 -04004212 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004213 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004214 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4215 setclientid.sc_name_len =
4216 nfs4_init_uniform_client_string(clp,
4217 setclientid.sc_name,
4218 sizeof(setclientid.sc_name));
4219 else
4220 setclientid.sc_name_len =
4221 nfs4_init_nonuniform_client_string(clp,
4222 setclientid.sc_name,
4223 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004224 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004225 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004226 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004227 sizeof(setclientid.sc_netid),
4228 rpc_peeraddr2str(clp->cl_rpcclient,
4229 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004230 rcu_read_unlock();
4231 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004232 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 clp->cl_ipaddr, port >> 8, port & 255);
4234
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004235 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4236 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4237 setclientid.sc_name_len, setclientid.sc_name);
4238 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4239 dprintk("NFS reply setclientid: %d\n", status);
4240 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241}
4242
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004243/**
4244 * nfs4_proc_setclientid_confirm - Confirm client ID
4245 * @clp: state data structure
4246 * @res: result of a previous SETCLIENTID
4247 * @cred: RPC credential to use for this call
4248 *
4249 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4250 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004251int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004252 struct nfs4_setclientid_res *arg,
4253 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
4255 struct nfs_fsinfo fsinfo;
4256 struct rpc_message msg = {
4257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004258 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004260 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 };
4262 unsigned long now;
4263 int status;
4264
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004265 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4266 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4267 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004269 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 if (status == 0) {
4271 spin_lock(&clp->cl_lock);
4272 clp->cl_lease_time = fsinfo.lease_time * HZ;
4273 clp->cl_last_renewal = now;
4274 spin_unlock(&clp->cl_lock);
4275 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004276 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 return status;
4278}
4279
Trond Myklebustfe650402006-01-03 09:55:18 +01004280struct nfs4_delegreturndata {
4281 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004282 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004283 struct nfs_fh fh;
4284 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004285 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004286 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004287 int rpc_status;
4288};
4289
Trond Myklebustfe650402006-01-03 09:55:18 +01004290static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4291{
4292 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004293
Trond Myklebust14516c32010-07-31 14:29:06 -04004294 if (!nfs4_sequence_done(task, &data->res.seq_res))
4295 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004296
Ricardo Labiaga79708862009-12-07 09:23:21 -05004297 switch (task->tk_status) {
4298 case -NFS4ERR_STALE_STATEID:
4299 case -NFS4ERR_EXPIRED:
4300 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004301 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004302 break;
4303 default:
4304 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4305 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004306 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004307 return;
4308 }
4309 }
4310 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004311}
4312
4313static void nfs4_delegreturn_release(void *calldata)
4314{
Trond Myklebustfe650402006-01-03 09:55:18 +01004315 kfree(calldata);
4316}
4317
Andy Adamson938e1012009-04-01 09:22:28 -04004318#if defined(CONFIG_NFS_V4_1)
4319static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4320{
4321 struct nfs4_delegreturndata *d_data;
4322
4323 d_data = (struct nfs4_delegreturndata *)data;
4324
Trond Myklebust035168a2010-06-16 09:52:26 -04004325 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004326 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004327 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004328 return;
4329 rpc_call_start(task);
4330}
4331#endif /* CONFIG_NFS_V4_1 */
4332
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004333static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004334#if defined(CONFIG_NFS_V4_1)
4335 .rpc_call_prepare = nfs4_delegreturn_prepare,
4336#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004337 .rpc_call_done = nfs4_delegreturn_done,
4338 .rpc_release = nfs4_delegreturn_release,
4339};
4340
Trond Myklebuste6f81072008-01-24 18:14:34 -05004341static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004342{
4343 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004344 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004345 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004346 struct rpc_message msg = {
4347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4348 .rpc_cred = cred,
4349 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004350 struct rpc_task_setup task_setup_data = {
4351 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004352 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004353 .callback_ops = &nfs4_delegreturn_ops,
4354 .flags = RPC_TASK_ASYNC,
4355 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004356 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004357
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004358 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004359 if (data == NULL)
4360 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004361 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004362 data->args.fhandle = &data->fh;
4363 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004364 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004365 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004366 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004367 data->res.fattr = &data->fattr;
4368 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004369 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004370 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004371 data->rpc_status = 0;
4372
Trond Myklebustc970aa82007-07-14 15:39:59 -04004373 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004374 msg.rpc_argp = &data->args;
4375 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004376 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004377 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004378 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004379 if (!issync)
4380 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004381 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004382 if (status != 0)
4383 goto out;
4384 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004385 if (status == 0)
4386 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4387 else
4388 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004389out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004390 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004391 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392}
4393
Trond Myklebuste6f81072008-01-24 18:14:34 -05004394int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395{
4396 struct nfs_server *server = NFS_SERVER(inode);
4397 struct nfs4_exception exception = { };
4398 int err;
4399 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004400 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 switch (err) {
4402 case -NFS4ERR_STALE_STATEID:
4403 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 case 0:
4405 return 0;
4406 }
4407 err = nfs4_handle_exception(server, err, &exception);
4408 } while (exception.retry);
4409 return err;
4410}
4411
4412#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4413#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4414
4415/*
4416 * sleep, with exponential backoff, and retry the LOCK operation.
4417 */
4418static unsigned long
4419nfs4_set_lock_task_retry(unsigned long timeout)
4420{
Jeff Laytond3103102011-12-01 22:44:39 +01004421 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 timeout <<= 1;
4423 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4424 return NFS4_LOCK_MAXTIMEOUT;
4425 return timeout;
4426}
4427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4429{
4430 struct inode *inode = state->inode;
4431 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004432 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004433 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004435 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004437 struct nfs_lockt_res res = {
4438 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 };
4440 struct rpc_message msg = {
4441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4442 .rpc_argp = &arg,
4443 .rpc_resp = &res,
4444 .rpc_cred = state->owner->so_cred,
4445 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 struct nfs4_lock_state *lsp;
4447 int status;
4448
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004449 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004450 status = nfs4_set_lock_state(state, request);
4451 if (status != 0)
4452 goto out;
4453 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004454 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004455 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004456 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004457 switch (status) {
4458 case 0:
4459 request->fl_type = F_UNLCK;
4460 break;
4461 case -NFS4ERR_DENIED:
4462 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004464 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004465out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return status;
4467}
4468
4469static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4470{
4471 struct nfs4_exception exception = { };
4472 int err;
4473
4474 do {
4475 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4476 _nfs4_proc_getlk(state, cmd, request),
4477 &exception);
4478 } while (exception.retry);
4479 return err;
4480}
4481
4482static int do_vfs_lock(struct file *file, struct file_lock *fl)
4483{
4484 int res = 0;
4485 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4486 case FL_POSIX:
4487 res = posix_lock_file_wait(file, fl);
4488 break;
4489 case FL_FLOCK:
4490 res = flock_lock_file_wait(file, fl);
4491 break;
4492 default:
4493 BUG();
4494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 return res;
4496}
4497
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004498struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004499 struct nfs_locku_args arg;
4500 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004501 struct nfs4_lock_state *lsp;
4502 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004503 struct file_lock fl;
4504 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004505 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004506};
4507
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004508static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4509 struct nfs_open_context *ctx,
4510 struct nfs4_lock_state *lsp,
4511 struct nfs_seqid *seqid)
4512{
4513 struct nfs4_unlockdata *p;
4514 struct inode *inode = lsp->ls_state->inode;
4515
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004516 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004517 if (p == NULL)
4518 return NULL;
4519 p->arg.fh = NFS_FH(inode);
4520 p->arg.fl = &p->fl;
4521 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004522 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004523 p->arg.stateid = &lsp->ls_stateid;
4524 p->lsp = lsp;
4525 atomic_inc(&lsp->ls_count);
4526 /* Ensure we don't close file until we're done freeing locks! */
4527 p->ctx = get_nfs_open_context(ctx);
4528 memcpy(&p->fl, fl, sizeof(p->fl));
4529 p->server = NFS_SERVER(inode);
4530 return p;
4531}
4532
Trond Myklebust06f814a2006-01-03 09:55:07 +01004533static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004534{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004535 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004536 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004537 nfs4_put_lock_state(calldata->lsp);
4538 put_nfs_open_context(calldata->ctx);
4539 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004540}
4541
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004542static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004543{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004544 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004545
Trond Myklebust14516c32010-07-31 14:29:06 -04004546 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4547 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004548 switch (task->tk_status) {
4549 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004550 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4551 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004552 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004553 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004554 case -NFS4ERR_BAD_STATEID:
4555 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004556 case -NFS4ERR_STALE_STATEID:
4557 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004558 break;
4559 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004560 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004561 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004562 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004563 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004564}
4565
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004566static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004567{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004568 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004570 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004571 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004572 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004573 /* Note: exit _without_ running nfs4_locku_done */
4574 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004575 return;
4576 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004577 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004578 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004579 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004580 &calldata->res.seq_res,
4581 task) != 0)
4582 nfs_release_seqid(calldata->arg.seqid);
4583 else
4584 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585}
4586
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004587static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004588 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004589 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004590 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004591};
4592
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4594 struct nfs_open_context *ctx,
4595 struct nfs4_lock_state *lsp,
4596 struct nfs_seqid *seqid)
4597{
4598 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004599 struct rpc_message msg = {
4600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4601 .rpc_cred = ctx->cred,
4602 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004603 struct rpc_task_setup task_setup_data = {
4604 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004605 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004606 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004607 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004608 .flags = RPC_TASK_ASYNC,
4609 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610
Frank Filz137d6ac2007-07-09 15:32:29 -07004611 /* Ensure this is an unlock - when canceling a lock, the
4612 * canceled lock is passed in, and it won't be an unlock.
4613 */
4614 fl->fl_type = F_UNLCK;
4615
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004616 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4617 if (data == NULL) {
4618 nfs_free_seqid(seqid);
4619 return ERR_PTR(-ENOMEM);
4620 }
4621
Trond Myklebust9d12b212012-01-17 22:04:25 -05004622 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004623 msg.rpc_argp = &data->arg;
4624 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004625 task_setup_data.callback_data = data;
4626 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004627}
4628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4630{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004631 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004632 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004633 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004634 struct rpc_task *task;
4635 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004636 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Trond Myklebust9b073572006-06-29 16:38:34 -04004638 status = nfs4_set_lock_state(state, request);
4639 /* Unlock _before_ we do the RPC call */
4640 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004641 down_read(&nfsi->rwsem);
4642 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4643 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004644 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004645 }
4646 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004647 if (status != 0)
4648 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004649 /* Is this a delegated lock? */
4650 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004651 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004652 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004653 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004654 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004655 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004656 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004657 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004658 status = PTR_ERR(task);
4659 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004660 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004661 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004662 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004663out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004664 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004665 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666}
4667
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004668struct nfs4_lockdata {
4669 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004670 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004671 struct nfs4_lock_state *lsp;
4672 struct nfs_open_context *ctx;
4673 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004674 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004675 int rpc_status;
4676 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004677 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004678};
4679
4680static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004681 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4682 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004683{
4684 struct nfs4_lockdata *p;
4685 struct inode *inode = lsp->ls_state->inode;
4686 struct nfs_server *server = NFS_SERVER(inode);
4687
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004688 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004689 if (p == NULL)
4690 return NULL;
4691
4692 p->arg.fh = NFS_FH(inode);
4693 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004694 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004695 if (p->arg.open_seqid == NULL)
4696 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004697 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004698 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004699 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004700 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004701 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004702 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004703 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004704 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004705 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004706 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004707 atomic_inc(&lsp->ls_count);
4708 p->ctx = get_nfs_open_context(ctx);
4709 memcpy(&p->fl, fl, sizeof(p->fl));
4710 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004711out_free_seqid:
4712 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004713out_free:
4714 kfree(p);
4715 return NULL;
4716}
4717
4718static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4719{
4720 struct nfs4_lockdata *data = calldata;
4721 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
Harvey Harrison3110ff82008-05-02 13:42:44 -07004723 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004724 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4725 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004726 /* Do we need to do an open_to_lock_owner? */
4727 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004728 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004729 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004730 data->arg.open_stateid = &state->stateid;
4731 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004732 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004733 } else
4734 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004735 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004736 if (nfs4_setup_sequence(data->server,
4737 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004738 &data->res.seq_res,
4739 task) == 0) {
4740 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004741 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004742 }
4743 nfs_release_seqid(data->arg.open_seqid);
4744out_release_lock_seqid:
4745 nfs_release_seqid(data->arg.lock_seqid);
4746 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004747}
4748
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004749static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4750{
4751 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4752 nfs4_lock_prepare(task, calldata);
4753}
4754
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004755static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4756{
4757 struct nfs4_lockdata *data = calldata;
4758
Harvey Harrison3110ff82008-05-02 13:42:44 -07004759 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004760
Trond Myklebust14516c32010-07-31 14:29:06 -04004761 if (!nfs4_sequence_done(task, &data->res.seq_res))
4762 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004763
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004764 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004765 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004766 if (data->rpc_status == 0)
4767 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4768 else
4769 goto out;
4770 }
4771 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004772 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004773 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004774 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004775 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004776out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004777 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004778}
4779
4780static void nfs4_lock_release(void *calldata)
4781{
4782 struct nfs4_lockdata *data = calldata;
4783
Harvey Harrison3110ff82008-05-02 13:42:44 -07004784 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004785 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004786 if (data->cancelled != 0) {
4787 struct rpc_task *task;
4788 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4789 data->arg.lock_seqid);
4790 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004791 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004792 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004793 } else
4794 nfs_free_seqid(data->arg.lock_seqid);
4795 nfs4_put_lock_state(data->lsp);
4796 put_nfs_open_context(data->ctx);
4797 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004798 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004799}
4800
4801static const struct rpc_call_ops nfs4_lock_ops = {
4802 .rpc_call_prepare = nfs4_lock_prepare,
4803 .rpc_call_done = nfs4_lock_done,
4804 .rpc_release = nfs4_lock_release,
4805};
4806
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004807static const struct rpc_call_ops nfs4_recover_lock_ops = {
4808 .rpc_call_prepare = nfs4_recover_lock_prepare,
4809 .rpc_call_done = nfs4_lock_done,
4810 .rpc_release = nfs4_lock_release,
4811};
4812
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004813static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4814{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004815 switch (error) {
4816 case -NFS4ERR_ADMIN_REVOKED:
4817 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004818 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004819 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004820 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004821 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004822 break;
4823 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004824 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004825 case -NFS4ERR_EXPIRED:
4826 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004827 };
4828}
4829
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004830static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004831{
4832 struct nfs4_lockdata *data;
4833 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004834 struct rpc_message msg = {
4835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4836 .rpc_cred = state->owner->so_cred,
4837 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004838 struct rpc_task_setup task_setup_data = {
4839 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004840 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004841 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004842 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004843 .flags = RPC_TASK_ASYNC,
4844 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004845 int ret;
4846
Harvey Harrison3110ff82008-05-02 13:42:44 -07004847 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004848 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004849 fl->fl_u.nfs4_fl.owner,
4850 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004851 if (data == NULL)
4852 return -ENOMEM;
4853 if (IS_SETLKW(cmd))
4854 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004855 if (recovery_type > NFS_LOCK_NEW) {
4856 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004857 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004858 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4859 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004860 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004861 msg.rpc_argp = &data->arg;
4862 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004863 task_setup_data.callback_data = data;
4864 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004865 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004866 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004867 ret = nfs4_wait_for_completion_rpc_task(task);
4868 if (ret == 0) {
4869 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004870 if (ret)
4871 nfs4_handle_setlk_error(data->server, data->lsp,
4872 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004873 } else
4874 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004875 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004876 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004877 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878}
4879
4880static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4881{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004882 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004883 struct nfs4_exception exception = {
4884 .inode = state->inode,
4885 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004886 int err;
4887
4888 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004889 /* Cache the lock if possible... */
4890 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4891 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004892 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004893 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004894 break;
4895 nfs4_handle_exception(server, err, &exception);
4896 } while (exception.retry);
4897 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898}
4899
4900static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4901{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004902 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004903 struct nfs4_exception exception = {
4904 .inode = state->inode,
4905 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004906 int err;
4907
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004908 err = nfs4_set_lock_state(state, request);
4909 if (err != 0)
4910 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004911 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004912 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4913 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004914 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004915 switch (err) {
4916 default:
4917 goto out;
4918 case -NFS4ERR_GRACE:
4919 case -NFS4ERR_DELAY:
4920 nfs4_handle_exception(server, err, &exception);
4921 err = 0;
4922 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004923 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004924out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004925 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926}
4927
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004928#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004929/**
4930 * nfs41_check_expired_locks - possibly free a lock stateid
4931 *
4932 * @state: NFSv4 state for an inode
4933 *
4934 * Returns NFS_OK if recovery for this stateid is now finished.
4935 * Otherwise a negative NFS4ERR value is returned.
4936 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004937static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004938{
Chuck Levereb64cf92012-07-11 16:30:05 -04004939 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004940 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004941 struct nfs_server *server = NFS_SERVER(state->inode);
4942
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004943 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004944 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004945 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4946 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004947 /* Free the stateid unless the server
4948 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004949 if (status != -NFS4ERR_BAD_STATEID)
4950 nfs41_free_stateid(server,
4951 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004952 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004953 ret = status;
4954 }
4955 }
4956 };
4957
4958 return ret;
4959}
4960
4961static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4962{
4963 int status = NFS_OK;
4964
4965 if (test_bit(LK_STATE_IN_USE, &state->flags))
4966 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004967 if (status != NFS_OK)
4968 status = nfs4_lock_expired(state, request);
4969 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004970}
4971#endif
4972
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4974{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004975 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004976 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004977 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004979 if ((fl_flags & FL_POSIX) &&
4980 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4981 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004982 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004983 status = nfs4_set_lock_state(state, request);
4984 if (status != 0)
4985 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004986 request->fl_flags |= FL_ACCESS;
4987 status = do_vfs_lock(request->fl_file, request);
4988 if (status < 0)
4989 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004990 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004991 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004992 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004993 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004994 request->fl_flags = fl_flags & ~FL_SLEEP;
4995 status = do_vfs_lock(request->fl_file, request);
4996 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004997 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004998 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004999 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04005000 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005001 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005002 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005003 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005004 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5005 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005006out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005007 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005008out:
5009 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 return status;
5011}
5012
5013static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5014{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005015 struct nfs4_exception exception = {
5016 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005017 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005018 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 int err;
5020
5021 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005022 err = _nfs4_proc_setlk(state, cmd, request);
5023 if (err == -NFS4ERR_DENIED)
5024 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005026 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 } while (exception.retry);
5028 return err;
5029}
5030
5031static int
5032nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5033{
5034 struct nfs_open_context *ctx;
5035 struct nfs4_state *state;
5036 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5037 int status;
5038
5039 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005040 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 state = ctx->state;
5042
5043 if (request->fl_start < 0 || request->fl_end < 0)
5044 return -EINVAL;
5045
Trond Myklebustd9531262009-07-21 19:22:38 -04005046 if (IS_GETLK(cmd)) {
5047 if (state != NULL)
5048 return nfs4_proc_getlk(state, F_GETLK, request);
5049 return 0;
5050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
5052 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5053 return -EINVAL;
5054
Trond Myklebustd9531262009-07-21 19:22:38 -04005055 if (request->fl_type == F_UNLCK) {
5056 if (state != NULL)
5057 return nfs4_proc_unlck(state, cmd, request);
5058 return 0;
5059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
Trond Myklebustd9531262009-07-21 19:22:38 -04005061 if (state == NULL)
5062 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005063 /*
5064 * Don't rely on the VFS having checked the file open mode,
5065 * since it won't do this for flock() locks.
5066 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005067 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005068 case F_RDLCK:
5069 if (!(filp->f_mode & FMODE_READ))
5070 return -EBADF;
5071 break;
5072 case F_WRLCK:
5073 if (!(filp->f_mode & FMODE_WRITE))
5074 return -EBADF;
5075 }
5076
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 do {
5078 status = nfs4_proc_setlk(state, cmd, request);
5079 if ((status != -EAGAIN) || IS_SETLK(cmd))
5080 break;
5081 timeout = nfs4_set_lock_task_retry(timeout);
5082 status = -ERESTARTSYS;
5083 if (signalled())
5084 break;
5085 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 return status;
5087}
5088
Trond Myklebust888e6942005-11-04 15:38:11 -05005089int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5090{
5091 struct nfs_server *server = NFS_SERVER(state->inode);
5092 struct nfs4_exception exception = { };
5093 int err;
5094
5095 err = nfs4_set_lock_state(state, fl);
5096 if (err != 0)
5097 goto out;
5098 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005099 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005100 switch (err) {
5101 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005102 printk(KERN_ERR "NFS: %s: unhandled error "
5103 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005104 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005105 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005106 goto out;
5107 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005108 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005109 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005110 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005111 nfs4_schedule_lease_recovery(server->nfs_client);
5112 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005113 case -NFS4ERR_BADSESSION:
5114 case -NFS4ERR_BADSLOT:
5115 case -NFS4ERR_BAD_HIGH_SLOT:
5116 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5117 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005118 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005119 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005120 case -ERESTARTSYS:
5121 /*
5122 * The show must go on: exit, but mark the
5123 * stateid as needing recovery.
5124 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005125 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005126 case -NFS4ERR_ADMIN_REVOKED:
5127 case -NFS4ERR_BAD_STATEID:
5128 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005129 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005130 err = 0;
5131 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005132 case -EKEYEXPIRED:
5133 /*
5134 * User RPCSEC_GSS context has expired.
5135 * We cannot recover this stateid now, so
5136 * skip it and allow recovery thread to
5137 * proceed.
5138 */
5139 err = 0;
5140 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005141 case -ENOMEM:
5142 case -NFS4ERR_DENIED:
5143 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5144 err = 0;
5145 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005146 case -NFS4ERR_DELAY:
5147 break;
5148 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005149 err = nfs4_handle_exception(server, err, &exception);
5150 } while (exception.retry);
5151out:
5152 return err;
5153}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005154
Trond Myklebustcf470c32012-03-07 13:49:12 -05005155struct nfs_release_lockowner_data {
5156 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005157 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005158 struct nfs_release_lockowner_args args;
5159};
5160
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005161static void nfs4_release_lockowner_release(void *calldata)
5162{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005163 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005164 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005165 kfree(calldata);
5166}
5167
Trond Myklebust17280172012-03-11 13:11:00 -04005168static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005169 .rpc_release = nfs4_release_lockowner_release,
5170};
5171
Trond Myklebustcf470c32012-03-07 13:49:12 -05005172int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005173{
5174 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005175 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005176 struct rpc_message msg = {
5177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5178 };
5179
5180 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005181 return -EINVAL;
5182 data = kmalloc(sizeof(*data), GFP_NOFS);
5183 if (!data)
5184 return -ENOMEM;
5185 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005186 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005187 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5188 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5189 data->args.lock_owner.s_dev = server->s_dev;
5190 msg.rpc_argp = &data->args;
5191 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5192 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005193}
5194
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005195#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5196
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005197static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5198 const void *buf, size_t buflen,
5199 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005200{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005201 if (strcmp(key, "") != 0)
5202 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005203
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005204 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005205}
5206
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005207static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5208 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005209{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005210 if (strcmp(key, "") != 0)
5211 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005212
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005213 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005214}
5215
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005216static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5217 size_t list_len, const char *name,
5218 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005219{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005220 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005221
J. Bruce Fields096455a2006-03-20 23:23:42 -05005222 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5223 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005224
5225 if (list && len <= list_len)
5226 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005227 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005228}
5229
Andy Adamson533eb462011-06-13 18:25:56 -04005230/*
5231 * nfs_fhget will use either the mounted_on_fileid or the fileid
5232 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005233static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5234{
Andy Adamson533eb462011-06-13 18:25:56 -04005235 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5236 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5237 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005238 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005239 return;
5240
5241 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005242 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005243 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5244 fattr->nlink = 2;
5245}
5246
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005247static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5248 const struct qstr *name,
5249 struct nfs4_fs_locations *fs_locations,
5250 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005251{
5252 struct nfs_server *server = NFS_SERVER(dir);
5253 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005254 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005255 };
5256 struct nfs4_fs_locations_arg args = {
5257 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005258 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005259 .page = page,
5260 .bitmask = bitmask,
5261 };
Benny Halevy22958462009-04-01 09:22:02 -04005262 struct nfs4_fs_locations_res res = {
5263 .fs_locations = fs_locations,
5264 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005265 struct rpc_message msg = {
5266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5267 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005268 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005269 };
5270 int status;
5271
Harvey Harrison3110ff82008-05-02 13:42:44 -07005272 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005273
5274 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5275 * is not supported */
5276 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5277 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5278 else
5279 bitmask[0] |= FATTR4_WORD0_FILEID;
5280
Trond Myklebustc228fd32007-01-13 02:28:11 -05005281 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005282 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005283 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005284 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005285 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005286 return status;
5287}
5288
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005289int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5290 const struct qstr *name,
5291 struct nfs4_fs_locations *fs_locations,
5292 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005293{
5294 struct nfs4_exception exception = { };
5295 int err;
5296 do {
5297 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005298 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005299 &exception);
5300 } while (exception.retry);
5301 return err;
5302}
5303
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005304static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5305{
5306 int status;
5307 struct nfs4_secinfo_arg args = {
5308 .dir_fh = NFS_FH(dir),
5309 .name = name,
5310 };
5311 struct nfs4_secinfo_res res = {
5312 .flavors = flavors,
5313 };
5314 struct rpc_message msg = {
5315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5316 .rpc_argp = &args,
5317 .rpc_resp = &res,
5318 };
5319
5320 dprintk("NFS call secinfo %s\n", name->name);
5321 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5322 dprintk("NFS reply secinfo: %d\n", status);
5323 return status;
5324}
5325
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005326int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5327 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005328{
5329 struct nfs4_exception exception = { };
5330 int err;
5331 do {
5332 err = nfs4_handle_exception(NFS_SERVER(dir),
5333 _nfs4_proc_secinfo(dir, name, flavors),
5334 &exception);
5335 } while (exception.retry);
5336 return err;
5337}
5338
Andy Adamson557134a2009-04-01 09:21:53 -04005339#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005340/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005341 * Check the exchange flags returned by the server for invalid flags, having
5342 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5343 * DS flags set.
5344 */
5345static int nfs4_check_cl_exchange_flags(u32 flags)
5346{
5347 if (flags & ~EXCHGID4_FLAG_MASK_R)
5348 goto out_inval;
5349 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5350 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5351 goto out_inval;
5352 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5353 goto out_inval;
5354 return NFS_OK;
5355out_inval:
5356 return -NFS4ERR_INVAL;
5357}
5358
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005359static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005360nfs41_same_server_scope(struct nfs41_server_scope *a,
5361 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005362{
5363 if (a->server_scope_sz == b->server_scope_sz &&
5364 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5365 return true;
5366
5367 return false;
5368}
5369
Andy Adamson357f54d2010-12-14 10:11:57 -05005370/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005371 * nfs4_proc_bind_conn_to_session()
5372 *
5373 * The 4.1 client currently uses the same TCP connection for the
5374 * fore and backchannel.
5375 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005376int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005377{
5378 int status;
5379 struct nfs41_bind_conn_to_session_res res;
5380 struct rpc_message msg = {
5381 .rpc_proc =
5382 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5383 .rpc_argp = clp,
5384 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005385 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005386 };
5387
5388 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005389
5390 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5391 if (unlikely(res.session == NULL)) {
5392 status = -ENOMEM;
5393 goto out;
5394 }
5395
5396 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5397 if (status == 0) {
5398 if (memcmp(res.session->sess_id.data,
5399 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5400 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5401 status = -EIO;
5402 goto out_session;
5403 }
5404 if (res.dir != NFS4_CDFS4_BOTH) {
5405 dprintk("NFS: %s: Unexpected direction from server\n",
5406 __func__);
5407 status = -EIO;
5408 goto out_session;
5409 }
5410 if (res.use_conn_in_rdma_mode) {
5411 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5412 __func__);
5413 status = -EIO;
5414 goto out_session;
5415 }
5416 }
5417out_session:
5418 kfree(res.session);
5419out:
5420 dprintk("<-- %s status= %d\n", __func__, status);
5421 return status;
5422}
5423
5424/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005425 * nfs4_proc_exchange_id()
5426 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005427 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5428 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005429 * Since the clientid has expired, all compounds using sessions
5430 * associated with the stale clientid will be returning
5431 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5432 * be in some phase of session reset.
5433 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005434int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005435{
5436 nfs4_verifier verifier;
5437 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005438 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005439 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005440 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005441 };
5442 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005443 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005444 };
5445 int status;
5446 struct rpc_message msg = {
5447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5448 .rpc_argp = &args,
5449 .rpc_resp = &res,
5450 .rpc_cred = cred,
5451 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005452
Chuck Leverf0920752012-05-21 22:45:41 -04005453 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005454 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5455 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005456 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5457 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5458 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005459
Chuck Leveracdeb692012-05-21 22:46:16 -04005460 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005461 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005462 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005463 status = -ENOMEM;
5464 goto out;
5465 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005466
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005467 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005468 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005469 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005470 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005471 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005472 }
5473
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005474 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005475 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005476 status = -ENOMEM;
5477 goto out_server_scope;
5478 }
5479
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005480 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005481 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005482 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005483
Chuck Lever177313f2012-05-21 22:44:58 -04005484 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005485 clp->cl_clientid = res.clientid;
5486 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5487 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5488 clp->cl_seqid = res.seqid;
5489
Chuck Leveracdeb692012-05-21 22:46:16 -04005490 kfree(clp->cl_serverowner);
5491 clp->cl_serverowner = res.server_owner;
5492 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005493
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005494 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005495 kfree(clp->cl_implid);
5496 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005497
Chuck Lever177313f2012-05-21 22:44:58 -04005498 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005499 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005500 res.server_scope)) {
5501 dprintk("%s: server_scope mismatch detected\n",
5502 __func__);
5503 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005504 kfree(clp->cl_serverscope);
5505 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005506 }
5507
Chuck Lever177313f2012-05-21 22:44:58 -04005508 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005509 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005510 goto out;
5511 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005512 } else
5513 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005514
Chuck Leveracdeb692012-05-21 22:46:16 -04005515out_server_owner:
5516 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005517out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005518 kfree(res.server_scope);
5519out:
Chuck Lever177313f2012-05-21 22:44:58 -04005520 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005521 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005522 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005523 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005524 clp->cl_implid->date.seconds,
5525 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005526 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005527 return status;
5528}
5529
Trond Myklebust66245532012-05-25 17:18:09 -04005530static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5531 struct rpc_cred *cred)
5532{
5533 struct rpc_message msg = {
5534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5535 .rpc_argp = clp,
5536 .rpc_cred = cred,
5537 };
5538 int status;
5539
5540 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5541 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005542 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005543 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5544 return status;
5545}
5546
5547static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5548 struct rpc_cred *cred)
5549{
5550 unsigned int loop;
5551 int ret;
5552
5553 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5554 ret = _nfs4_proc_destroy_clientid(clp, cred);
5555 switch (ret) {
5556 case -NFS4ERR_DELAY:
5557 case -NFS4ERR_CLIENTID_BUSY:
5558 ssleep(1);
5559 break;
5560 default:
5561 return ret;
5562 }
5563 }
5564 return 0;
5565}
5566
5567int nfs4_destroy_clientid(struct nfs_client *clp)
5568{
5569 struct rpc_cred *cred;
5570 int ret = 0;
5571
5572 if (clp->cl_mvops->minor_version < 1)
5573 goto out;
5574 if (clp->cl_exchange_flags == 0)
5575 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005576 if (clp->cl_preserve_clid)
5577 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005578 cred = nfs4_get_exchange_id_cred(clp);
5579 ret = nfs4_proc_destroy_clientid(clp, cred);
5580 if (cred)
5581 put_rpccred(cred);
5582 switch (ret) {
5583 case 0:
5584 case -NFS4ERR_STALE_CLIENTID:
5585 clp->cl_exchange_flags = 0;
5586 }
5587out:
5588 return ret;
5589}
5590
Andy Adamson2050f0c2009-04-01 09:22:30 -04005591struct nfs4_get_lease_time_data {
5592 struct nfs4_get_lease_time_args *args;
5593 struct nfs4_get_lease_time_res *res;
5594 struct nfs_client *clp;
5595};
5596
5597static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5598 void *calldata)
5599{
5600 int ret;
5601 struct nfs4_get_lease_time_data *data =
5602 (struct nfs4_get_lease_time_data *)calldata;
5603
5604 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005605 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005606 /* just setup sequence, do not trigger session recovery
5607 since we're invoked within one */
5608 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005609 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005610 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005611
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005612 if (ret != -EAGAIN)
5613 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005614 dprintk("<-- %s\n", __func__);
5615}
5616
5617/*
5618 * Called from nfs4_state_manager thread for session setup, so don't recover
5619 * from sequence operation or clientid errors.
5620 */
5621static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5622{
5623 struct nfs4_get_lease_time_data *data =
5624 (struct nfs4_get_lease_time_data *)calldata;
5625
5626 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005627 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5628 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005629 switch (task->tk_status) {
5630 case -NFS4ERR_DELAY:
5631 case -NFS4ERR_GRACE:
5632 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5633 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5634 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005635 /* fall through */
5636 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005637 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005638 return;
5639 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005640 dprintk("<-- %s\n", __func__);
5641}
5642
Trond Myklebust17280172012-03-11 13:11:00 -04005643static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005644 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5645 .rpc_call_done = nfs4_get_lease_time_done,
5646};
5647
5648int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5649{
5650 struct rpc_task *task;
5651 struct nfs4_get_lease_time_args args;
5652 struct nfs4_get_lease_time_res res = {
5653 .lr_fsinfo = fsinfo,
5654 };
5655 struct nfs4_get_lease_time_data data = {
5656 .args = &args,
5657 .res = &res,
5658 .clp = clp,
5659 };
5660 struct rpc_message msg = {
5661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5662 .rpc_argp = &args,
5663 .rpc_resp = &res,
5664 };
5665 struct rpc_task_setup task_setup = {
5666 .rpc_client = clp->cl_rpcclient,
5667 .rpc_message = &msg,
5668 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005669 .callback_data = &data,
5670 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005671 };
5672 int status;
5673
Trond Myklebust9d12b212012-01-17 22:04:25 -05005674 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005675 dprintk("--> %s\n", __func__);
5676 task = rpc_run_task(&task_setup);
5677
5678 if (IS_ERR(task))
5679 status = PTR_ERR(task);
5680 else {
5681 status = task->tk_status;
5682 rpc_put_task(task);
5683 }
5684 dprintk("<-- %s return %d\n", __func__, status);
5685
5686 return status;
5687}
5688
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005689struct nfs4_slot *nfs4_alloc_slots(struct nfs4_slot_table *table,
5690 u32 max_slots, gfp_t gfp_flags)
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005691{
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005692 struct nfs4_slot *tbl;
5693 u32 i;
5694
5695 tbl = kmalloc_array(max_slots, sizeof(*tbl), gfp_flags);
5696 if (tbl != NULL) {
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005697 for (i = 0; i < max_slots; i++) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005698 tbl[i].table = table;
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005699 tbl[i].slot_nr = i;
5700 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005701 }
5702 return tbl;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005703}
5704
5705static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5706 struct nfs4_slot *new,
5707 u32 max_slots,
5708 u32 ivalue)
5709{
5710 struct nfs4_slot *old = NULL;
5711 u32 i;
5712
5713 spin_lock(&tbl->slot_tbl_lock);
5714 if (new) {
5715 old = tbl->slots;
5716 tbl->slots = new;
5717 tbl->max_slots = max_slots;
5718 }
Trond Myklebustf9b1ef52012-10-29 16:48:40 -04005719 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebust464ee9f2012-11-20 12:49:27 -05005720 tbl->target_highest_slotid = max_slots - 1;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005721 for (i = 0; i < tbl->max_slots; i++)
5722 tbl->slots[i].seq_nr = ivalue;
5723 spin_unlock(&tbl->slot_tbl_lock);
5724 kfree(old);
5725}
5726
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005727/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005728 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005729 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005730static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5731 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005732{
Andy Adamson104aeba2010-01-14 17:45:10 -05005733 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005734 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005735
Andy Adamson104aeba2010-01-14 17:45:10 -05005736 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5737 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005738
Andy Adamson104aeba2010-01-14 17:45:10 -05005739 /* Does the newly negotiated max_reqs match the existing slot table? */
5740 if (max_reqs != tbl->max_slots) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005741 new = nfs4_alloc_slots(tbl, max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005742 if (!new)
5743 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005744 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005745 ret = 0;
5746
5747 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005748 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5749 tbl, tbl->slots, tbl->max_slots);
5750out:
5751 dprintk("<-- %s: return %d\n", __func__, ret);
5752 return ret;
5753}
5754
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005755/* Destroy the slot table */
5756static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5757{
5758 if (session->fc_slot_table.slots != NULL) {
5759 kfree(session->fc_slot_table.slots);
5760 session->fc_slot_table.slots = NULL;
5761 }
5762 if (session->bc_slot_table.slots != NULL) {
5763 kfree(session->bc_slot_table.slots);
5764 session->bc_slot_table.slots = NULL;
5765 }
5766 return;
5767}
5768
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005769/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005770 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005771 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005772static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005773{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005774 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005775 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005776
Andy Adamsonaacd5532011-11-09 13:58:21 -05005777 dprintk("--> %s\n", __func__);
5778 /* Fore channel */
5779 tbl = &ses->fc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005780 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005781 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5782 if (status) /* -ENOMEM */
5783 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005784 /* Back channel */
5785 tbl = &ses->bc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005786 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005787 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5788 if (status && tbl->slots == NULL)
5789 /* Fore and back channel share a connection so get
5790 * both slot tables or neither */
5791 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005792 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005793}
5794
5795struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5796{
5797 struct nfs4_session *session;
5798 struct nfs4_slot_table *tbl;
5799
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005800 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005801 if (!session)
5802 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005803
Andy Adamson557134a2009-04-01 09:21:53 -04005804 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005805 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005806 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005807 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005808 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005809
5810 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005811 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005812 spin_lock_init(&tbl->slot_tbl_lock);
5813 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005814 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005815
Trond Myklebust1055d762010-06-16 09:52:27 -04005816 session->session_state = 1<<NFS4_SESSION_INITING;
5817
Andy Adamson557134a2009-04-01 09:21:53 -04005818 session->clp = clp;
5819 return session;
5820}
5821
5822void nfs4_destroy_session(struct nfs4_session *session)
5823{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005824 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005825 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005826
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005827 cred = nfs4_get_exchange_id_cred(session->clp);
5828 nfs4_proc_destroy_session(session, cred);
5829 if (cred)
5830 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005831
5832 rcu_read_lock();
5833 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5834 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005835 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005836 __func__, xprt);
5837 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005838 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005839 kfree(session);
5840}
5841
Andy Adamsonfc931582009-04-01 09:22:31 -04005842/*
5843 * Initialize the values to be used by the client in CREATE_SESSION
5844 * If nfs4_init_session set the fore channel request and response sizes,
5845 * use them.
5846 *
5847 * Set the back channel max_resp_sz_cached to zero to force the client to
5848 * always set csa_cachethis to FALSE because the current implementation
5849 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5850 */
5851static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5852{
5853 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005854 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5855 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005856
5857 if (mxrqst_sz == 0)
5858 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5859 if (mxresp_sz == 0)
5860 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5861 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005862 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5863 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005864 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005865 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005866
5867 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005868 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005869 __func__,
5870 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005871 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005872
5873 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005874 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5875 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5876 args->bc_attrs.max_resp_sz_cached = 0;
5877 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5878 args->bc_attrs.max_reqs = 1;
5879
5880 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5881 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5882 __func__,
5883 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5884 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5885 args->bc_attrs.max_reqs);
5886}
5887
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005888static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005889{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005890 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5891 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5892
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005893 if (rcvd->max_resp_sz > sent->max_resp_sz)
5894 return -EINVAL;
5895 /*
5896 * Our requested max_ops is the minimum we need; we're not
5897 * prepared to break up compounds into smaller pieces than that.
5898 * So, no point even trying to continue if the server won't
5899 * cooperate:
5900 */
5901 if (rcvd->max_ops < sent->max_ops)
5902 return -EINVAL;
5903 if (rcvd->max_reqs == 0)
5904 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005905 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5906 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005907 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005908}
5909
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005910static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5911{
5912 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5913 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005914
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005915 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5916 return -EINVAL;
5917 if (rcvd->max_resp_sz < sent->max_resp_sz)
5918 return -EINVAL;
5919 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5920 return -EINVAL;
5921 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005922 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005923 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005924 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005925 return -EINVAL;
5926 return 0;
5927}
Andy Adamson8d353012009-04-01 09:22:32 -04005928
Andy Adamson8d353012009-04-01 09:22:32 -04005929static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5930 struct nfs4_session *session)
5931{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005932 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005933
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005934 ret = nfs4_verify_fore_channel_attrs(args, session);
5935 if (ret)
5936 return ret;
5937 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005938}
5939
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005940static int _nfs4_proc_create_session(struct nfs_client *clp,
5941 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005942{
5943 struct nfs4_session *session = clp->cl_session;
5944 struct nfs41_create_session_args args = {
5945 .client = clp,
5946 .cb_program = NFS4_CALLBACK,
5947 };
5948 struct nfs41_create_session_res res = {
5949 .client = clp,
5950 };
5951 struct rpc_message msg = {
5952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5953 .rpc_argp = &args,
5954 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005955 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005956 };
5957 int status;
5958
5959 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005960 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005961
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005962 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005963
Trond Myklebust43095d32012-11-20 11:13:12 -05005964 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005965 /* Verify the session's negotiated channel_attrs values */
5966 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005967 /* Increment the clientid slot sequence id */
5968 clp->cl_seqid++;
5969 }
5970
5971 return status;
5972}
5973
5974/*
5975 * Issues a CREATE_SESSION operation to the server.
5976 * It is the responsibility of the caller to verify the session is
5977 * expired before calling this routine.
5978 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005979int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005980{
5981 int status;
5982 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005983 struct nfs4_session *session = clp->cl_session;
5984
5985 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5986
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005987 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005988 if (status)
5989 goto out;
5990
Andy Adamsonaacd5532011-11-09 13:58:21 -05005991 /* Init or reset the session slot tables */
5992 status = nfs4_setup_session_slot_tables(session);
5993 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005994 if (status)
5995 goto out;
5996
5997 ptr = (unsigned *)&session->sess_id.data[0];
5998 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5999 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006000out:
6001 dprintk("<-- %s\n", __func__);
6002 return status;
6003}
6004
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006005/*
6006 * Issue the over-the-wire RPC DESTROY_SESSION.
6007 * The caller must serialize access to this routine.
6008 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006009int nfs4_proc_destroy_session(struct nfs4_session *session,
6010 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006011{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006012 struct rpc_message msg = {
6013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6014 .rpc_argp = session,
6015 .rpc_cred = cred,
6016 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006017 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006018
6019 dprintk("--> nfs4_proc_destroy_session\n");
6020
6021 /* session is still being setup */
6022 if (session->clp->cl_cons_state != NFS_CS_READY)
6023 return status;
6024
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006025 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006026
6027 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006028 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006029 "Session has been destroyed regardless...\n", status);
6030
6031 dprintk("<-- nfs4_proc_destroy_session\n");
6032 return status;
6033}
6034
Trond Myklebust7b38c362012-05-23 13:23:31 -04006035/*
6036 * With sessions, the client is not marked ready until after a
6037 * successful EXCHANGE_ID and CREATE_SESSION.
6038 *
6039 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6040 * other versions of NFS can be tried.
6041 */
6042static int nfs41_check_session_ready(struct nfs_client *clp)
6043{
6044 int ret;
6045
6046 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6047 ret = nfs4_client_recover_expired_lease(clp);
6048 if (ret)
6049 return ret;
6050 }
6051 if (clp->cl_cons_state < NFS_CS_READY)
6052 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006053 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006054 return 0;
6055}
6056
Trond Myklebustfccba802009-07-21 16:48:07 -04006057int nfs4_init_session(struct nfs_server *server)
6058{
6059 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006060 struct nfs4_session *session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006061 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6062 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
Trond Myklebustfccba802009-07-21 16:48:07 -04006063
6064 if (!nfs4_has_session(clp))
6065 return 0;
6066
Trond Myklebustae72ae672012-11-20 11:02:55 -05006067 if (server->rsize != 0)
6068 target_max_resp_sz = server->rsize;
6069 target_max_resp_sz += nfs41_maxread_overhead;
6070
6071 if (server->wsize != 0)
6072 target_max_rqst_sz = server->wsize;
6073 target_max_rqst_sz += nfs41_maxwrite_overhead;
6074
Trond Myklebust1055d762010-06-16 09:52:27 -04006075 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006076 spin_lock(&clp->cl_lock);
6077 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebustae72ae672012-11-20 11:02:55 -05006078 /* Initialise targets and channel attributes */
6079 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6080 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6081 session->fc_target_max_resp_sz = target_max_resp_sz;
6082 session->fc_attrs.max_resp_sz = target_max_resp_sz;
Trond Myklebust688a9022012-11-20 10:53:39 -05006083 } else {
6084 /* Just adjust the targets */
6085 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6086 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6087 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6088 }
6089 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6090 session->fc_target_max_resp_sz = target_max_resp_sz;
6091 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6092 }
Trond Myklebust7b38c362012-05-23 13:23:31 -04006093 }
6094 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006095
Trond Myklebust688a9022012-11-20 10:53:39 -05006096 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6097 nfs4_schedule_lease_recovery(clp);
6098
Trond Myklebust7b38c362012-05-23 13:23:31 -04006099 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006100}
6101
Trond Myklebust7b38c362012-05-23 13:23:31 -04006102int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006103{
6104 struct nfs4_session *session = clp->cl_session;
6105 int ret;
6106
Trond Myklebust7b38c362012-05-23 13:23:31 -04006107 spin_lock(&clp->cl_lock);
6108 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6109 /*
6110 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6111 * DS lease to be equal to the MDS lease.
6112 */
6113 clp->cl_lease_time = lease_time;
6114 clp->cl_last_renewal = jiffies;
6115 }
6116 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006117
Trond Myklebust7b38c362012-05-23 13:23:31 -04006118 ret = nfs41_check_session_ready(clp);
6119 if (ret)
6120 return ret;
6121 /* Test for the DS role */
6122 if (!is_ds_client(clp))
6123 return -ENODEV;
6124 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006125}
6126EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6127
6128
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006129/*
6130 * Renew the cl_session lease.
6131 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006132struct nfs4_sequence_data {
6133 struct nfs_client *clp;
6134 struct nfs4_sequence_args args;
6135 struct nfs4_sequence_res res;
6136};
6137
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006138static void nfs41_sequence_release(void *data)
6139{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006140 struct nfs4_sequence_data *calldata = data;
6141 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006142
Alexandros Batsakis71358402010-02-05 03:45:05 -08006143 if (atomic_read(&clp->cl_count) > 1)
6144 nfs4_schedule_state_renewal(clp);
6145 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006146 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006147}
6148
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006149static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6150{
6151 switch(task->tk_status) {
6152 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006153 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6154 return -EAGAIN;
6155 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006156 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006157 }
6158 return 0;
6159}
6160
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006161static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006162{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006163 struct nfs4_sequence_data *calldata = data;
6164 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006165
Trond Myklebust14516c32010-07-31 14:29:06 -04006166 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6167 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006168
6169 if (task->tk_status < 0) {
6170 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006171 if (atomic_read(&clp->cl_count) == 1)
6172 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006173
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006174 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6175 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006176 return;
6177 }
6178 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006179 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006180out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006181 dprintk("<-- %s\n", __func__);
6182}
6183
6184static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6185{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006186 struct nfs4_sequence_data *calldata = data;
6187 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006188 struct nfs4_sequence_args *args;
6189 struct nfs4_sequence_res *res;
6190
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006191 args = task->tk_msg.rpc_argp;
6192 res = task->tk_msg.rpc_resp;
6193
Trond Myklebust9d12b212012-01-17 22:04:25 -05006194 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006195 return;
6196 rpc_call_start(task);
6197}
6198
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006199static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6200{
6201 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6202 nfs41_sequence_prepare(task, data);
6203}
6204
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006205static const struct rpc_call_ops nfs41_sequence_ops = {
6206 .rpc_call_done = nfs41_sequence_call_done,
6207 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006208 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006209};
6210
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006211static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6212 .rpc_call_done = nfs41_sequence_call_done,
6213 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6214 .rpc_release = nfs41_sequence_release,
6215};
6216
6217static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6218 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006219{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006220 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006221 struct rpc_message msg = {
6222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6223 .rpc_cred = cred,
6224 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006225 struct rpc_task_setup task_setup_data = {
6226 .rpc_client = clp->cl_rpcclient,
6227 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006228 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006229 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6230 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006231
Alexandros Batsakis71358402010-02-05 03:45:05 -08006232 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006233 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006234 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006235 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006236 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006237 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006238 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006239 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006240 msg.rpc_argp = &calldata->args;
6241 msg.rpc_resp = &calldata->res;
6242 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006243 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006244
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006245 return rpc_run_task(&task_setup_data);
6246}
6247
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006248static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006249{
6250 struct rpc_task *task;
6251 int ret = 0;
6252
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006253 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6254 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006255 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006256 if (IS_ERR(task))
6257 ret = PTR_ERR(task);
6258 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006259 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006260 dprintk("<-- %s status=%d\n", __func__, ret);
6261 return ret;
6262}
6263
6264static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6265{
6266 struct rpc_task *task;
6267 int ret;
6268
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006269 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006270 if (IS_ERR(task)) {
6271 ret = PTR_ERR(task);
6272 goto out;
6273 }
6274 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006275 if (!ret) {
6276 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6277
6278 if (task->tk_status == 0)
6279 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006280 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006281 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006282 rpc_put_task(task);
6283out:
6284 dprintk("<-- %s status=%d\n", __func__, ret);
6285 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006286}
6287
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006288struct nfs4_reclaim_complete_data {
6289 struct nfs_client *clp;
6290 struct nfs41_reclaim_complete_args arg;
6291 struct nfs41_reclaim_complete_res res;
6292};
6293
6294static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6295{
6296 struct nfs4_reclaim_complete_data *calldata = data;
6297
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006298 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006299 if (nfs41_setup_sequence(calldata->clp->cl_session,
6300 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006301 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006302 return;
6303
6304 rpc_call_start(task);
6305}
6306
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006307static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6308{
6309 switch(task->tk_status) {
6310 case 0:
6311 case -NFS4ERR_COMPLETE_ALREADY:
6312 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6313 break;
6314 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006315 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006316 /* fall through */
6317 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006318 return -EAGAIN;
6319 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006320 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006321 }
6322 return 0;
6323}
6324
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006325static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6326{
6327 struct nfs4_reclaim_complete_data *calldata = data;
6328 struct nfs_client *clp = calldata->clp;
6329 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6330
6331 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006332 if (!nfs41_sequence_done(task, res))
6333 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006334
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006335 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6336 rpc_restart_call_prepare(task);
6337 return;
6338 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006339 dprintk("<-- %s\n", __func__);
6340}
6341
6342static void nfs4_free_reclaim_complete_data(void *data)
6343{
6344 struct nfs4_reclaim_complete_data *calldata = data;
6345
6346 kfree(calldata);
6347}
6348
6349static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6350 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6351 .rpc_call_done = nfs4_reclaim_complete_done,
6352 .rpc_release = nfs4_free_reclaim_complete_data,
6353};
6354
6355/*
6356 * Issue a global reclaim complete.
6357 */
6358static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6359{
6360 struct nfs4_reclaim_complete_data *calldata;
6361 struct rpc_task *task;
6362 struct rpc_message msg = {
6363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6364 };
6365 struct rpc_task_setup task_setup_data = {
6366 .rpc_client = clp->cl_rpcclient,
6367 .rpc_message = &msg,
6368 .callback_ops = &nfs4_reclaim_complete_call_ops,
6369 .flags = RPC_TASK_ASYNC,
6370 };
6371 int status = -ENOMEM;
6372
6373 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006374 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006375 if (calldata == NULL)
6376 goto out;
6377 calldata->clp = clp;
6378 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006379
Trond Myklebust9d12b212012-01-17 22:04:25 -05006380 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006381 msg.rpc_argp = &calldata->arg;
6382 msg.rpc_resp = &calldata->res;
6383 task_setup_data.callback_data = calldata;
6384 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006385 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006386 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006387 goto out;
6388 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006389 status = nfs4_wait_for_completion_rpc_task(task);
6390 if (status == 0)
6391 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006392 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006393 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006394out:
6395 dprintk("<-- %s status=%d\n", __func__, status);
6396 return status;
6397}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006398
6399static void
6400nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6401{
6402 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006403 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006404
6405 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006406 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6407 * right now covering the LAYOUTGET we are about to send.
6408 * However, that is not so catastrophic, and there seems
6409 * to be no way to prevent it completely.
6410 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006411 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006412 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006413 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006414 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6415 NFS_I(lgp->args.inode)->layout,
6416 lgp->args.ctx->state)) {
6417 rpc_exit(task, NFS4_OK);
6418 return;
6419 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006420 rpc_call_start(task);
6421}
6422
6423static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6424{
6425 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006426 struct inode *inode = lgp->args.inode;
6427 struct nfs_server *server = NFS_SERVER(inode);
6428 struct pnfs_layout_hdr *lo;
6429 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006430
6431 dprintk("--> %s\n", __func__);
6432
6433 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006434 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006435
6436 switch (task->tk_status) {
6437 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006438 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006439 case -NFS4ERR_LAYOUTTRYLATER:
6440 case -NFS4ERR_RECALLCONFLICT:
6441 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006442 break;
6443 case -NFS4ERR_EXPIRED:
6444 case -NFS4ERR_BAD_STATEID:
6445 spin_lock(&inode->i_lock);
6446 lo = NFS_I(inode)->layout;
6447 if (!lo || list_empty(&lo->plh_segs)) {
6448 spin_unlock(&inode->i_lock);
6449 /* If the open stateid was bad, then recover it. */
6450 state = lgp->args.ctx->state;
6451 } else {
6452 LIST_HEAD(head);
6453
6454 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6455 spin_unlock(&inode->i_lock);
6456 /* Mark the bad layout state as invalid, then
6457 * retry using the open stateid. */
6458 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006459 }
6460 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006461 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6462 rpc_restart_call_prepare(task);
6463out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006464 dprintk("<-- %s\n", __func__);
6465}
6466
Idan Kedar85541162012-08-02 11:47:10 +03006467static size_t max_response_pages(struct nfs_server *server)
6468{
6469 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6470 return nfs_page_array_len(0, max_resp_sz);
6471}
6472
6473static void nfs4_free_pages(struct page **pages, size_t size)
6474{
6475 int i;
6476
6477 if (!pages)
6478 return;
6479
6480 for (i = 0; i < size; i++) {
6481 if (!pages[i])
6482 break;
6483 __free_page(pages[i]);
6484 }
6485 kfree(pages);
6486}
6487
6488static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6489{
6490 struct page **pages;
6491 int i;
6492
6493 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6494 if (!pages) {
6495 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6496 return NULL;
6497 }
6498
6499 for (i = 0; i < size; i++) {
6500 pages[i] = alloc_page(gfp_flags);
6501 if (!pages[i]) {
6502 dprintk("%s: failed to allocate page\n", __func__);
6503 nfs4_free_pages(pages, size);
6504 return NULL;
6505 }
6506 }
6507
6508 return pages;
6509}
6510
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006511static void nfs4_layoutget_release(void *calldata)
6512{
6513 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006514 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6515 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006516
6517 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006518 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006519 put_nfs_open_context(lgp->args.ctx);
6520 kfree(calldata);
6521 dprintk("<-- %s\n", __func__);
6522}
6523
6524static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6525 .rpc_call_prepare = nfs4_layoutget_prepare,
6526 .rpc_call_done = nfs4_layoutget_done,
6527 .rpc_release = nfs4_layoutget_release,
6528};
6529
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006530struct pnfs_layout_segment *
6531nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006532{
6533 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006534 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006535 struct rpc_task *task;
6536 struct rpc_message msg = {
6537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6538 .rpc_argp = &lgp->args,
6539 .rpc_resp = &lgp->res,
6540 };
6541 struct rpc_task_setup task_setup_data = {
6542 .rpc_client = server->client,
6543 .rpc_message = &msg,
6544 .callback_ops = &nfs4_layoutget_call_ops,
6545 .callback_data = lgp,
6546 .flags = RPC_TASK_ASYNC,
6547 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006548 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006549 int status = 0;
6550
6551 dprintk("--> %s\n", __func__);
6552
Idan Kedar85541162012-08-02 11:47:10 +03006553 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6554 if (!lgp->args.layout.pages) {
6555 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006556 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006557 }
6558 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6559
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006560 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006561 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006562 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006563 task = rpc_run_task(&task_setup_data);
6564 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006565 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006566 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006567 if (status == 0)
6568 status = task->tk_status;
6569 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006570 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006571 rpc_put_task(task);
6572 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006573 if (status)
6574 return ERR_PTR(status);
6575 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006576}
6577
Benny Halevycbe82602011-05-22 19:52:37 +03006578static void
6579nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6580{
6581 struct nfs4_layoutreturn *lrp = calldata;
6582
6583 dprintk("--> %s\n", __func__);
6584 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006585 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006586 return;
6587 rpc_call_start(task);
6588}
6589
6590static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6591{
6592 struct nfs4_layoutreturn *lrp = calldata;
6593 struct nfs_server *server;
6594
6595 dprintk("--> %s\n", __func__);
6596
6597 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6598 return;
6599
6600 server = NFS_SERVER(lrp->args.inode);
6601 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006602 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006603 return;
6604 }
Benny Halevycbe82602011-05-22 19:52:37 +03006605 dprintk("<-- %s\n", __func__);
6606}
6607
6608static void nfs4_layoutreturn_release(void *calldata)
6609{
6610 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006611 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006612
6613 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006614 spin_lock(&lo->plh_inode->i_lock);
6615 if (lrp->res.lrs_present)
6616 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6617 lo->plh_block_lgets--;
6618 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006619 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006620 kfree(calldata);
6621 dprintk("<-- %s\n", __func__);
6622}
6623
6624static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6625 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6626 .rpc_call_done = nfs4_layoutreturn_done,
6627 .rpc_release = nfs4_layoutreturn_release,
6628};
6629
6630int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6631{
6632 struct rpc_task *task;
6633 struct rpc_message msg = {
6634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6635 .rpc_argp = &lrp->args,
6636 .rpc_resp = &lrp->res,
6637 };
6638 struct rpc_task_setup task_setup_data = {
6639 .rpc_client = lrp->clp->cl_rpcclient,
6640 .rpc_message = &msg,
6641 .callback_ops = &nfs4_layoutreturn_call_ops,
6642 .callback_data = lrp,
6643 };
6644 int status;
6645
6646 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006647 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006648 task = rpc_run_task(&task_setup_data);
6649 if (IS_ERR(task))
6650 return PTR_ERR(task);
6651 status = task->tk_status;
6652 dprintk("<-- %s status=%d\n", __func__, status);
6653 rpc_put_task(task);
6654 return status;
6655}
6656
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006657/*
6658 * Retrieve the list of Data Server devices from the MDS.
6659 */
6660static int _nfs4_getdevicelist(struct nfs_server *server,
6661 const struct nfs_fh *fh,
6662 struct pnfs_devicelist *devlist)
6663{
6664 struct nfs4_getdevicelist_args args = {
6665 .fh = fh,
6666 .layoutclass = server->pnfs_curr_ld->id,
6667 };
6668 struct nfs4_getdevicelist_res res = {
6669 .devlist = devlist,
6670 };
6671 struct rpc_message msg = {
6672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6673 .rpc_argp = &args,
6674 .rpc_resp = &res,
6675 };
6676 int status;
6677
6678 dprintk("--> %s\n", __func__);
6679 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6680 &res.seq_res, 0);
6681 dprintk("<-- %s status=%d\n", __func__, status);
6682 return status;
6683}
6684
6685int nfs4_proc_getdevicelist(struct nfs_server *server,
6686 const struct nfs_fh *fh,
6687 struct pnfs_devicelist *devlist)
6688{
6689 struct nfs4_exception exception = { };
6690 int err;
6691
6692 do {
6693 err = nfs4_handle_exception(server,
6694 _nfs4_getdevicelist(server, fh, devlist),
6695 &exception);
6696 } while (exception.retry);
6697
6698 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6699 err, devlist->num_devs);
6700
6701 return err;
6702}
6703EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6704
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006705static int
6706_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6707{
6708 struct nfs4_getdeviceinfo_args args = {
6709 .pdev = pdev,
6710 };
6711 struct nfs4_getdeviceinfo_res res = {
6712 .pdev = pdev,
6713 };
6714 struct rpc_message msg = {
6715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6716 .rpc_argp = &args,
6717 .rpc_resp = &res,
6718 };
6719 int status;
6720
6721 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006722 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006723 dprintk("<-- %s status=%d\n", __func__, status);
6724
6725 return status;
6726}
6727
6728int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6729{
6730 struct nfs4_exception exception = { };
6731 int err;
6732
6733 do {
6734 err = nfs4_handle_exception(server,
6735 _nfs4_proc_getdeviceinfo(server, pdev),
6736 &exception);
6737 } while (exception.retry);
6738 return err;
6739}
6740EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6741
Andy Adamson863a3c62011-03-23 13:27:54 +00006742static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6743{
6744 struct nfs4_layoutcommit_data *data = calldata;
6745 struct nfs_server *server = NFS_SERVER(data->args.inode);
6746
6747 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006748 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006749 return;
6750 rpc_call_start(task);
6751}
6752
6753static void
6754nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6755{
6756 struct nfs4_layoutcommit_data *data = calldata;
6757 struct nfs_server *server = NFS_SERVER(data->args.inode);
6758
6759 if (!nfs4_sequence_done(task, &data->res.seq_res))
6760 return;
6761
6762 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006763 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6764 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6765 case -NFS4ERR_BADLAYOUT: /* no layout */
6766 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006767 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006768 break;
6769 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006770 nfs_post_op_update_inode_force_wcc(data->args.inode,
6771 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006772 break;
6773 default:
6774 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6775 rpc_restart_call_prepare(task);
6776 return;
6777 }
6778 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006779}
6780
6781static void nfs4_layoutcommit_release(void *calldata)
6782{
6783 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006784 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006785 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006786
Andy Adamsondb29c082011-07-30 20:52:38 -04006787 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006788 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006789 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6790 list_del_init(&lseg->pls_lc_list);
6791 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6792 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006793 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006794 }
Peng Tao92407e72011-10-23 20:21:17 -07006795
6796 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6797 smp_mb__after_clear_bit();
6798 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6799
Andy Adamson863a3c62011-03-23 13:27:54 +00006800 put_rpccred(data->cred);
6801 kfree(data);
6802}
6803
6804static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6805 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6806 .rpc_call_done = nfs4_layoutcommit_done,
6807 .rpc_release = nfs4_layoutcommit_release,
6808};
6809
6810int
Andy Adamsonef311532011-03-12 02:58:10 -05006811nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006812{
6813 struct rpc_message msg = {
6814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6815 .rpc_argp = &data->args,
6816 .rpc_resp = &data->res,
6817 .rpc_cred = data->cred,
6818 };
6819 struct rpc_task_setup task_setup_data = {
6820 .task = &data->task,
6821 .rpc_client = NFS_CLIENT(data->args.inode),
6822 .rpc_message = &msg,
6823 .callback_ops = &nfs4_layoutcommit_ops,
6824 .callback_data = data,
6825 .flags = RPC_TASK_ASYNC,
6826 };
6827 struct rpc_task *task;
6828 int status = 0;
6829
6830 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6831 "lbw: %llu inode %lu\n",
6832 data->task.tk_pid, sync,
6833 data->args.lastbytewritten,
6834 data->args.inode->i_ino);
6835
Trond Myklebust9d12b212012-01-17 22:04:25 -05006836 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006837 task = rpc_run_task(&task_setup_data);
6838 if (IS_ERR(task))
6839 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006840 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006841 goto out;
6842 status = nfs4_wait_for_completion_rpc_task(task);
6843 if (status != 0)
6844 goto out;
6845 status = task->tk_status;
6846out:
6847 dprintk("%s: status %d\n", __func__, status);
6848 rpc_put_task(task);
6849 return status;
6850}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006851
6852static int
6853_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6854 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6855{
6856 struct nfs41_secinfo_no_name_args args = {
6857 .style = SECINFO_STYLE_CURRENT_FH,
6858 };
6859 struct nfs4_secinfo_res res = {
6860 .flavors = flavors,
6861 };
6862 struct rpc_message msg = {
6863 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6864 .rpc_argp = &args,
6865 .rpc_resp = &res,
6866 };
6867 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6868}
6869
6870static int
6871nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6872 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6873{
6874 struct nfs4_exception exception = { };
6875 int err;
6876 do {
6877 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6878 switch (err) {
6879 case 0:
6880 case -NFS4ERR_WRONGSEC:
6881 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006882 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006883 default:
6884 err = nfs4_handle_exception(server, err, &exception);
6885 }
6886 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006887out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006888 return err;
6889}
6890
6891static int
6892nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6893 struct nfs_fsinfo *info)
6894{
6895 int err;
6896 struct page *page;
6897 rpc_authflavor_t flavor;
6898 struct nfs4_secinfo_flavors *flavors;
6899
6900 page = alloc_page(GFP_KERNEL);
6901 if (!page) {
6902 err = -ENOMEM;
6903 goto out;
6904 }
6905
6906 flavors = page_address(page);
6907 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6908
6909 /*
6910 * Fall back on "guess and check" method if
6911 * the server doesn't support SECINFO_NO_NAME
6912 */
6913 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6914 err = nfs4_find_root_sec(server, fhandle, info);
6915 goto out_freepage;
6916 }
6917 if (err)
6918 goto out_freepage;
6919
6920 flavor = nfs_find_best_sec(flavors);
6921 if (err == 0)
6922 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6923
6924out_freepage:
6925 put_page(page);
6926 if (err == -EACCES)
6927 return -EPERM;
6928out:
6929 return err;
6930}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006931
6932static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006933{
6934 int status;
6935 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006936 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006937 };
6938 struct nfs41_test_stateid_res res;
6939 struct rpc_message msg = {
6940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6941 .rpc_argp = &args,
6942 .rpc_resp = &res,
6943 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006944
Chuck Lever38527b12012-07-11 16:30:23 -04006945 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006946 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006947 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006948 if (status != NFS_OK) {
6949 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006950 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006951 }
6952 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006953 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006954}
6955
Chuck Lever38527b12012-07-11 16:30:23 -04006956/**
6957 * nfs41_test_stateid - perform a TEST_STATEID operation
6958 *
6959 * @server: server / transport on which to perform the operation
6960 * @stateid: state ID to test
6961 *
6962 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6963 * Otherwise a negative NFS4ERR value is returned if the operation
6964 * failed or the state ID is not currently valid.
6965 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006966static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006967{
6968 struct nfs4_exception exception = { };
6969 int err;
6970 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006971 err = _nfs41_test_stateid(server, stateid);
6972 if (err != -NFS4ERR_DELAY)
6973 break;
6974 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006975 } while (exception.retry);
6976 return err;
6977}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006978
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006979static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006980{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006981 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006982 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006983 };
6984 struct nfs41_free_stateid_res res;
6985 struct rpc_message msg = {
6986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6987 .rpc_argp = &args,
6988 .rpc_resp = &res,
6989 };
Chuck Lever38527b12012-07-11 16:30:23 -04006990 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006991
Chuck Lever38527b12012-07-11 16:30:23 -04006992 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006993 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006994 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006995 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006996 dprintk("NFS reply free_stateid: %d\n", status);
6997 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006998}
6999
Chuck Lever38527b12012-07-11 16:30:23 -04007000/**
7001 * nfs41_free_stateid - perform a FREE_STATEID operation
7002 *
7003 * @server: server / transport on which to perform the operation
7004 * @stateid: state ID to release
7005 *
7006 * Returns NFS_OK if the server freed "stateid". Otherwise a
7007 * negative NFS4ERR value is returned.
7008 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007009static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007010{
7011 struct nfs4_exception exception = { };
7012 int err;
7013 do {
Chuck Lever377e5072012-07-11 16:29:45 -04007014 err = _nfs4_free_stateid(server, stateid);
7015 if (err != -NFS4ERR_DELAY)
7016 break;
7017 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007018 } while (exception.retry);
7019 return err;
7020}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007021
7022static bool nfs41_match_stateid(const nfs4_stateid *s1,
7023 const nfs4_stateid *s2)
7024{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007025 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007026 return false;
7027
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007028 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007029 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007030 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007031 return true;
7032
7033 return false;
7034}
7035
Andy Adamson557134a2009-04-01 09:21:53 -04007036#endif /* CONFIG_NFS_V4_1 */
7037
Trond Myklebust36281ca2012-03-04 18:13:56 -05007038static bool nfs4_match_stateid(const nfs4_stateid *s1,
7039 const nfs4_stateid *s2)
7040{
Trond Myklebustf597c532012-03-04 18:13:56 -05007041 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007042}
7043
7044
Trond Myklebust17280172012-03-11 13:11:00 -04007045static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007046 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007047 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 .recover_open = nfs4_open_reclaim,
7049 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007050 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007051 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007052 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053};
7054
Andy Adamson591d71c2009-04-01 09:22:47 -04007055#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007056static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007057 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7058 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7059 .recover_open = nfs4_open_reclaim,
7060 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007061 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007062 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007063 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007064 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007065};
7066#endif /* CONFIG_NFS_V4_1 */
7067
Trond Myklebust17280172012-03-11 13:11:00 -04007068static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007069 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007070 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 .recover_open = nfs4_open_expired,
7072 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007073 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007074 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075};
7076
Andy Adamson591d71c2009-04-01 09:22:47 -04007077#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007078static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007079 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7080 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007081 .recover_open = nfs41_open_expired,
7082 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007083 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007084 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007085};
7086#endif /* CONFIG_NFS_V4_1 */
7087
Trond Myklebust17280172012-03-11 13:11:00 -04007088static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007089 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007090 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007091 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007092};
7093
7094#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007095static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007096 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007097 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007098 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007099};
7100#endif
7101
Trond Myklebust97dc1352010-06-16 09:52:26 -04007102static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7103 .minor_version = 0,
7104 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007105 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007106 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007107 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7108 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7109 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007110};
7111
7112#if defined(CONFIG_NFS_V4_1)
7113static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7114 .minor_version = 1,
7115 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007116 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007117 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007118 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7119 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7120 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007121};
7122#endif
7123
Trond Myklebust97dc1352010-06-16 09:52:26 -04007124const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7125 [0] = &nfs_v4_0_minor_ops,
7126#if defined(CONFIG_NFS_V4_1)
7127 [1] = &nfs_v4_1_minor_ops,
7128#endif
7129};
7130
Bryan Schumaker73a79702012-07-16 16:39:12 -04007131const struct inode_operations nfs4_dir_inode_operations = {
7132 .create = nfs_create,
7133 .lookup = nfs_lookup,
7134 .atomic_open = nfs_atomic_open,
7135 .link = nfs_link,
7136 .unlink = nfs_unlink,
7137 .symlink = nfs_symlink,
7138 .mkdir = nfs_mkdir,
7139 .rmdir = nfs_rmdir,
7140 .mknod = nfs_mknod,
7141 .rename = nfs_rename,
7142 .permission = nfs_permission,
7143 .getattr = nfs_getattr,
7144 .setattr = nfs_setattr,
7145 .getxattr = generic_getxattr,
7146 .setxattr = generic_setxattr,
7147 .listxattr = generic_listxattr,
7148 .removexattr = generic_removexattr,
7149};
7150
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007151static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007152 .permission = nfs_permission,
7153 .getattr = nfs_getattr,
7154 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007155 .getxattr = generic_getxattr,
7156 .setxattr = generic_setxattr,
7157 .listxattr = generic_listxattr,
7158 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007159};
7160
David Howells509de812006-08-22 20:06:11 -04007161const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 .version = 4, /* protocol version */
7163 .dentry_ops = &nfs4_dentry_operations,
7164 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007165 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007166 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007168 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007169 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170 .getattr = nfs4_proc_getattr,
7171 .setattr = nfs4_proc_setattr,
7172 .lookup = nfs4_proc_lookup,
7173 .access = nfs4_proc_access,
7174 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 .create = nfs4_proc_create,
7176 .remove = nfs4_proc_remove,
7177 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007178 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179 .unlink_done = nfs4_proc_unlink_done,
7180 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007181 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007182 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007183 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 .link = nfs4_proc_link,
7185 .symlink = nfs4_proc_symlink,
7186 .mkdir = nfs4_proc_mkdir,
7187 .rmdir = nfs4_proc_remove,
7188 .readdir = nfs4_proc_readdir,
7189 .mknod = nfs4_proc_mknod,
7190 .statfs = nfs4_proc_statfs,
7191 .fsinfo = nfs4_proc_fsinfo,
7192 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007193 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194 .decode_dirent = nfs4_decode_dirent,
7195 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007196 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007197 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007198 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007200 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007201 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007202 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007204 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007205 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007207 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007208 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007209 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007210 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007211 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007212 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007213 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007214 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007215 .create_server = nfs4_create_server,
7216 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217};
7218
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007219static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7220 .prefix = XATTR_NAME_NFSV4_ACL,
7221 .list = nfs4_xattr_list_nfs4_acl,
7222 .get = nfs4_xattr_get_nfs4_acl,
7223 .set = nfs4_xattr_set_nfs4_acl,
7224};
7225
7226const struct xattr_handler *nfs4_xattr_handlers[] = {
7227 &nfs4_xattr_nfs4_acl_handler,
7228 NULL
7229};
7230
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231/*
7232 * Local variables:
7233 * c-basic-offset: 8
7234 * End:
7235 */