blob: 14b39742b6e4ecf882bccfa8ea1157a17f99dc1b [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 Myklebust45d43c22012-02-06 19:38:51 -0500415nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400416{
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400417 /* clear used bit in bitmap */
418 __clear_bit(slotid, tbl->used_slots);
419
420 /* update highest_used_slotid when it is freed */
421 if (slotid == tbl->highest_used_slotid) {
422 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500423 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400424 tbl->highest_used_slotid = slotid;
425 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500426 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400427 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500428 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
429 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400430}
431
Trond Myklebust961a8282012-01-17 22:57:37 -0500432bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
433{
434 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
435 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400436}
437
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800438/*
Andy Adamson42acd022011-01-06 02:04:34 +0000439 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800440 */
Andy Adamson42acd022011-01-06 02:04:34 +0000441static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800442{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400443 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500444 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
445 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800446 return;
447 }
448
Trond Myklebust45d43c22012-02-06 19:38:51 -0500449 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800450 return;
451
Andy Adamson42acd022011-01-06 02:04:34 +0000452 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
453 complete(&ses->fc_slot_table.complete);
454}
455
456/*
457 * Signal state manager thread if session back channel is drained
458 */
459void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
460{
461 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500462 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000463 return;
464 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
465 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800466}
467
Trond Myklebustd185a332010-06-16 09:52:25 -0400468static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400469{
470 struct nfs4_slot_table *tbl;
471
Trond Myklebustd185a332010-06-16 09:52:25 -0400472 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400473 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400474 /* just wake up the next guy waiting since
475 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500476 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500477 return;
Andy Adamson13615872009-04-01 09:22:17 -0400478 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500479
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500480 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500481 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000482 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500483 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400484 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400485}
486
Trond Myklebust14516c32010-07-31 14:29:06 -0400487static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400488{
Trond Myklebust933602e2012-11-16 12:12:38 -0500489 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400490 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400492
493 /*
494 * sr_status remains 1 if an RPC level error occurred. The server
495 * may or may not have processed the sequence operation..
496 * Proceed as if the server received and processed the sequence
497 * operation.
498 */
499 if (res->sr_status == 1)
500 res->sr_status = NFS_OK;
501
Bryan Schumaker468f8612011-04-18 15:57:32 -0400502 /* don't increment the sequence number if the task wasn't sent */
503 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400504 goto out;
505
Trond Myklebust933602e2012-11-16 12:12:38 -0500506 slot = res->sr_slot;
507
Andy Adamson691daf32009-12-04 15:55:39 -0500508 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400509 switch (res->sr_status) {
510 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400511 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500512 ++slot->seq_nr;
513 timestamp = slot->renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400514 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400515 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500516 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500517 if (res->sr_status_flags != 0)
518 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400519 break;
520 case -NFS4ERR_DELAY:
521 /* The server detected a resend of the RPC call and
522 * returned NFS4ERR_DELAY as per Section 2.10.6.2
523 * of RFC5661.
524 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200525 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400526 __func__,
Trond Myklebust933602e2012-11-16 12:12:38 -0500527 slot - res->sr_session->fc_slot_table.slots,
528 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400529 goto out_retry;
530 default:
531 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500532 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400533 }
534out:
535 /* The session may be reset by one of the error handlers. */
536 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400537 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400538 return 1;
539out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400540 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400541 goto out;
542 rpc_delay(task, NFS4_POLL_RETRY_MAX);
543 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400544}
545
Trond Myklebust14516c32010-07-31 14:29:06 -0400546static int nfs4_sequence_done(struct rpc_task *task,
547 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400548{
Trond Myklebust14516c32010-07-31 14:29:06 -0400549 if (res->sr_session == NULL)
550 return 1;
551 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400552}
553
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400554/*
Benny Halevy510b8172009-04-01 09:22:14 -0400555 * nfs4_find_slot - efficiently look for a free slot
556 *
557 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
558 * If found, we mark the slot as used, update the highest_used_slotid,
559 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500560 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400561 *
562 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400563 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500564static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800565nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400566{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500567 u32 slotid;
568 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400569
Trond Myklebust45d43c22012-02-06 19:38:51 -0500570 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400571 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
572 tbl->max_slots);
573 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
574 if (slotid >= tbl->max_slots)
575 goto out;
576 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500577 if (slotid > tbl->highest_used_slotid ||
578 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400579 tbl->highest_used_slotid = slotid;
580 ret_id = slotid;
581out:
582 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
583 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400584 return ret_id;
585}
586
Trond Myklebust9d12b212012-01-17 22:04:25 -0500587static void nfs41_init_sequence(struct nfs4_sequence_args *args,
588 struct nfs4_sequence_res *res, int cache_reply)
589{
590 args->sa_session = NULL;
591 args->sa_cache_this = 0;
592 if (cache_reply)
593 args->sa_cache_this = 1;
594 res->sr_session = NULL;
595 res->sr_slot = NULL;
596}
597
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000598int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599 struct nfs4_sequence_args *args,
600 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601 struct rpc_task *task)
602{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400603 struct nfs4_slot *slot;
604 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500605 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400606
607 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400609 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610 return 0;
611
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400612 tbl = &session->fc_slot_table;
613
614 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400615 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800616 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500617 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500618 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400619 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500620 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500621 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400622 }
623
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800624 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
625 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
626 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
627 spin_unlock(&tbl->slot_tbl_lock);
628 dprintk("%s enforce FIFO order\n", __func__);
629 return -EAGAIN;
630 }
631
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800632 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500633 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400634 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
635 spin_unlock(&tbl->slot_tbl_lock);
636 dprintk("<-- %s: no free slots\n", __func__);
637 return -EAGAIN;
638 }
639 spin_unlock(&tbl->slot_tbl_lock);
640
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800641 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400642 slot = tbl->slots + slotid;
Trond Myklebust933602e2012-11-16 12:12:38 -0500643 slot->renewal_time = jiffies;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400644 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400645 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400646
647 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
648
Benny Halevy99fe60d2009-04-01 09:22:29 -0400649 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400650 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400651 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400652 /*
653 * sr_status is only set in decode_sequence, and so will remain
654 * set to 1 if an rpc level failure occurs.
655 */
656 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657 return 0;
658}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000659EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400660
Trond Myklebust035168a2010-06-16 09:52:26 -0400661int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662 struct nfs4_sequence_args *args,
663 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 struct rpc_task *task)
665{
Trond Myklebust035168a2010-06-16 09:52:26 -0400666 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 int ret = 0;
668
Trond Myklebust9d12b212012-01-17 22:04:25 -0500669 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400670 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400671
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200672 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400673 __func__, session->clp, session, res->sr_slot ?
674 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400675
Trond Myklebust9d12b212012-01-17 22:04:25 -0500676 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677out:
678 dprintk("<-- %s status=%d\n", __func__, ret);
679 return ret;
680}
681
682struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400683 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 struct nfs4_sequence_args *seq_args;
685 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686};
687
688static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
689{
690 struct nfs41_call_sync_data *data = calldata;
691
Trond Myklebust035168a2010-06-16 09:52:26 -0400692 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
693
694 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500695 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696 return;
697 rpc_call_start(task);
698}
699
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800700static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
701{
702 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
703 nfs41_call_sync_prepare(task, calldata);
704}
705
Andy Adamson69ab40c2009-04-01 09:22:19 -0400706static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
707{
708 struct nfs41_call_sync_data *data = calldata;
709
Trond Myklebust14516c32010-07-31 14:29:06 -0400710 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400711}
712
Trond Myklebust17280172012-03-11 13:11:00 -0400713static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400714 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400715 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400716};
717
Trond Myklebust17280172012-03-11 13:11:00 -0400718static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800719 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
720 .rpc_call_done = nfs41_call_sync_done,
721};
722
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
724 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400725 struct rpc_message *msg,
726 struct nfs4_sequence_args *args,
727 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800728 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400729{
730 int ret;
731 struct rpc_task *task;
732 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400733 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400734 .seq_args = args,
735 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400736 };
737 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000738 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400739 .rpc_message = msg,
740 .callback_ops = &nfs41_call_sync_ops,
741 .callback_data = &data
742 };
743
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800744 if (privileged)
745 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400746 task = rpc_run_task(&task_setup);
747 if (IS_ERR(task))
748 ret = PTR_ERR(task);
749 else {
750 ret = task->tk_status;
751 rpc_put_task(task);
752 }
753 return ret;
754}
755
Bryan Schumaker7c513052011-03-24 17:12:24 +0000756int _nfs4_call_sync_session(struct rpc_clnt *clnt,
757 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400758 struct rpc_message *msg,
759 struct nfs4_sequence_args *args,
760 struct nfs4_sequence_res *res,
761 int cache_reply)
762{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500763 nfs41_init_sequence(args, res, cache_reply);
764 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400765}
766
Trond Myklebustdf896452010-06-16 09:52:26 -0400767#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500768static inline
769void nfs41_init_sequence(struct nfs4_sequence_args *args,
770 struct nfs4_sequence_res *res, int cache_reply)
771{
772}
773
Trond Myklebust14516c32010-07-31 14:29:06 -0400774static int nfs4_sequence_done(struct rpc_task *task,
775 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400776{
Trond Myklebust14516c32010-07-31 14:29:06 -0400777 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400778}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400779#endif /* CONFIG_NFS_V4_1 */
780
Bryan Schumaker7c513052011-03-24 17:12:24 +0000781int _nfs4_call_sync(struct rpc_clnt *clnt,
782 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400783 struct rpc_message *msg,
784 struct nfs4_sequence_args *args,
785 struct nfs4_sequence_res *res,
786 int cache_reply)
787{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500788 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000789 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400790}
791
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000792static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000793int nfs4_call_sync(struct rpc_clnt *clnt,
794 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000795 struct rpc_message *msg,
796 struct nfs4_sequence_args *args,
797 struct nfs4_sequence_res *res,
798 int cache_reply)
799{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000800 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
801 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000802}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400803
Trond Myklebust38478b22006-05-25 01:40:57 -0400804static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Trond Myklebust38478b22006-05-25 01:40:57 -0400806 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Trond Myklebust38478b22006-05-25 01:40:57 -0400808 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400809 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700810 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400811 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700812 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400813 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400817 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 struct nfs_openargs o_arg;
819 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100820 struct nfs_open_confirmargs c_arg;
821 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500822 struct nfs4_string owner_name;
823 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400826 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400828 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100830 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400831 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100832 int rpc_status;
833 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834};
835
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400836
837static void nfs4_init_opendata_res(struct nfs4_opendata *p)
838{
839 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400840 p->o_res.seqid = p->o_arg.seqid;
841 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400842 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400843 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400844 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500845 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400846}
847
Al Viro82a2c1b2011-06-22 18:30:55 -0400848static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500849 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400850 const struct iattr *attrs,
851 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852{
Al Viro82a2c1b2011-06-22 18:30:55 -0400853 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 struct inode *dir = parent->d_inode;
855 struct nfs_server *server = NFS_SERVER(dir);
856 struct nfs4_opendata *p;
857
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400858 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 if (p == NULL)
860 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400861 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100862 if (p->o_arg.seqid == NULL)
863 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400864 nfs_sb_active(dentry->d_sb);
865 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100866 p->dir = parent;
867 p->owner = sp;
868 atomic_inc(&sp->so_count);
869 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870 p->o_arg.open_flags = flags;
871 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700872 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
873 * will return permission denied for all bits until close */
874 if (!(flags & O_EXCL)) {
875 /* ask server to check for all possible rights as results
876 * are cached */
877 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
878 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700879 }
David Howells7539bba2006-08-22 20:06:09 -0400880 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400881 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
882 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400883 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100884 p->o_arg.server = server;
885 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400886 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100887 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500888 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500889 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400890
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100891 p->o_arg.u.attrs = &p->attrs;
892 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500893
894 verf[0] = jiffies;
895 verf[1] = current->pid;
896 memcpy(p->o_arg.u.verifier.data, verf,
897 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100898 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100899 p->c_arg.fh = &p->o_res.fh;
900 p->c_arg.stateid = &p->o_res.stateid;
901 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400902 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400903 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100904 return p;
905err_free:
906 kfree(p);
907err:
908 dput(parent);
909 return NULL;
910}
911
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400912static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100913{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400914 struct nfs4_opendata *p = container_of(kref,
915 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400916 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400917
918 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400919 if (p->state != NULL)
920 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400921 nfs4_put_state_owner(p->owner);
922 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400923 dput(p->dentry);
924 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500925 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400926 kfree(p);
927}
928
929static void nfs4_opendata_put(struct nfs4_opendata *p)
930{
931 if (p != NULL)
932 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100933}
934
Trond Myklebust06f814a2006-01-03 09:55:07 +0100935static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
936{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100937 int ret;
938
Trond Myklebust06f814a2006-01-03 09:55:07 +0100939 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100940 return ret;
941}
942
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944{
945 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946
Trond Myklebust536e43d2012-01-17 22:04:26 -0500947 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948 goto out;
949 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400950 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500951 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
952 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400953 break;
954 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500955 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
956 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400957 break;
958 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500959 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
960 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400961 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400963 return ret;
964}
965
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400967{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500968 if (delegation == NULL)
969 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500972 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500974 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400975 return 1;
976}
977
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100979{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100981 case FMODE_WRITE:
982 state->n_wronly++;
983 break;
984 case FMODE_READ:
985 state->n_rdonly++;
986 break;
987 case FMODE_READ|FMODE_WRITE:
988 state->n_rdwr++;
989 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100991}
992
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400994{
995 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500996 nfs4_stateid_copy(&state->stateid, stateid);
997 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400999 case FMODE_READ:
1000 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1001 break;
1002 case FMODE_WRITE:
1003 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1004 break;
1005 case FMODE_READ|FMODE_WRITE:
1006 set_bit(NFS_O_RDWR_STATE, &state->flags);
1007 }
1008}
1009
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001011{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001012 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001014 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001015}
1016
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017static 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 -07001018{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001019 /*
1020 * Protect the call to nfs4_state_set_mode_locked and
1021 * serialise the stateid update
1022 */
1023 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001024 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001025 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001026 set_bit(NFS_DELEGATED_STATE, &state->flags);
1027 }
1028 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001030 write_sequnlock(&state->seqlock);
1031 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001033 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036static 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 -05001037{
1038 struct nfs_inode *nfsi = NFS_I(state->inode);
1039 struct nfs_delegation *deleg_cur;
1040 int ret = 0;
1041
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001043
1044 rcu_read_lock();
1045 deleg_cur = rcu_dereference(nfsi->delegation);
1046 if (deleg_cur == NULL)
1047 goto no_delegation;
1048
1049 spin_lock(&deleg_cur->lock);
1050 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001052 goto no_delegation_unlock;
1053
1054 if (delegation == NULL)
1055 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001056 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001057 goto no_delegation_unlock;
1058
Trond Myklebustb7391f42008-12-23 15:21:52 -05001059 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001060 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001061 ret = 1;
1062no_delegation_unlock:
1063 spin_unlock(&deleg_cur->lock);
1064no_delegation:
1065 rcu_read_unlock();
1066
1067 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001068 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001069 ret = 1;
1070 }
1071
1072 return ret;
1073}
1074
1075
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077{
1078 struct nfs_delegation *delegation;
1079
1080 rcu_read_lock();
1081 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001082 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001083 rcu_read_unlock();
1084 return;
1085 }
1086 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001087 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088}
1089
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001091{
1092 struct nfs4_state *state = opendata->state;
1093 struct nfs_inode *nfsi = NFS_I(state->inode);
1094 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001095 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001096 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001097 nfs4_stateid stateid;
1098 int ret = -EAGAIN;
1099
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001101 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001102 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001103 if (can_open_cached(state, fmode, open_mode)) {
1104 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001105 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001106 goto out_return_state;
1107 }
1108 spin_unlock(&state->owner->so_lock);
1109 }
Trond Myklebust34310432008-12-23 15:21:38 -05001110 rcu_read_lock();
1111 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001112 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001113 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001114 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001115 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001117 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001119 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001120 if (ret != 0)
1121 goto out;
1122 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001123
1124 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001126 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001128out:
1129 return ERR_PTR(ret);
1130out_return_state:
1131 atomic_inc(&state->count);
1132 return state;
1133}
1134
Andy Adamsone23008e2012-10-02 21:07:32 -04001135static void
1136nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1137{
1138 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1139 struct nfs_delegation *delegation;
1140 int delegation_flags = 0;
1141
1142 rcu_read_lock();
1143 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1144 if (delegation)
1145 delegation_flags = delegation->flags;
1146 rcu_read_unlock();
1147 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1148 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1149 "returning a delegation for "
1150 "OPEN(CLAIM_DELEGATE_CUR)\n",
1151 clp->cl_hostname);
1152 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1153 nfs_inode_set_delegation(state->inode,
1154 data->owner->so_cred,
1155 &data->o_res);
1156 else
1157 nfs_inode_reclaim_delegation(state->inode,
1158 data->owner->so_cred,
1159 &data->o_res);
1160}
1161
1162/*
1163 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1164 * and update the nfs4_state.
1165 */
1166static struct nfs4_state *
1167_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1168{
1169 struct inode *inode = data->state->inode;
1170 struct nfs4_state *state = data->state;
1171 int ret;
1172
1173 if (!data->rpc_done) {
1174 ret = data->rpc_status;
1175 goto err;
1176 }
1177
1178 ret = -ESTALE;
1179 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1180 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1181 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1182 goto err;
1183
1184 ret = -ENOMEM;
1185 state = nfs4_get_open_state(inode, data->owner);
1186 if (state == NULL)
1187 goto err;
1188
1189 ret = nfs_refresh_inode(inode, &data->f_attr);
1190 if (ret)
1191 goto err;
1192
1193 if (data->o_res.delegation_type != 0)
1194 nfs4_opendata_check_deleg(data, state);
1195 update_open_stateid(state, &data->o_res.stateid, NULL,
1196 data->o_arg.fmode);
1197
1198 return state;
1199err:
1200 return ERR_PTR(ret);
1201
1202}
1203
1204static struct nfs4_state *
1205_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001206{
1207 struct inode *inode;
1208 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001209 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001210
Trond Myklebustaac00a82007-07-05 19:02:21 -04001211 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001212 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001213 goto out;
1214 }
1215
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001216 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001217 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001218 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001219 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001220 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001221 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001222 goto err;
1223 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001224 state = nfs4_get_open_state(inode, data->owner);
1225 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001226 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001227 if (data->o_res.delegation_type != 0)
1228 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001229 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001230 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001231 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001232out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001233 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001234err_put_inode:
1235 iput(inode);
1236err:
1237 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001238}
1239
Andy Adamsone23008e2012-10-02 21:07:32 -04001240static struct nfs4_state *
1241nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1242{
1243 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1244 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1245 return _nfs4_opendata_to_nfs4_state(data);
1246}
1247
Trond Myklebust864472e2006-01-03 09:55:15 +01001248static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1249{
1250 struct nfs_inode *nfsi = NFS_I(state->inode);
1251 struct nfs_open_context *ctx;
1252
1253 spin_lock(&state->inode->i_lock);
1254 list_for_each_entry(ctx, &nfsi->open_files, list) {
1255 if (ctx->state != state)
1256 continue;
1257 get_nfs_open_context(ctx);
1258 spin_unlock(&state->inode->i_lock);
1259 return ctx;
1260 }
1261 spin_unlock(&state->inode->i_lock);
1262 return ERR_PTR(-ENOENT);
1263}
1264
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001265static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1266{
1267 struct nfs4_opendata *opendata;
1268
Al Viro3d4ff432011-06-22 18:40:12 -04001269 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001270 if (opendata == NULL)
1271 return ERR_PTR(-ENOMEM);
1272 opendata->state = state;
1273 atomic_inc(&state->count);
1274 return opendata;
1275}
1276
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001277static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001278{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001279 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 int ret;
1281
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001282 opendata->o_arg.open_flags = 0;
1283 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001284 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1285 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1286 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001287 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 if (ret != 0)
1289 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001290 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001291 if (IS_ERR(newstate))
1292 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001293 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001294 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001295 return 0;
1296}
1297
1298static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1299{
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001301 int ret;
1302
1303 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001304 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 smp_rmb();
1306 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001307 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001308 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001309 if (ret != 0)
1310 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001311 if (newstate != state)
1312 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001313 }
1314 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001315 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001316 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001317 if (ret != 0)
1318 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001319 if (newstate != state)
1320 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001321 }
1322 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001323 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001324 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001325 if (ret != 0)
1326 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001327 if (newstate != state)
1328 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001329 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001330 /*
1331 * We may have performed cached opens for all three recoveries.
1332 * Check if we need to update the current stateid.
1333 */
1334 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001335 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001336 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001337 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001338 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001339 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001340 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001341 return 0;
1342}
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344/*
1345 * OPEN_RECLAIM:
1346 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001348static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001350 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001351 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 int status;
1354
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001355 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1356 if (IS_ERR(opendata))
1357 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001358 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1359 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001360 rcu_read_lock();
1361 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001362 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001363 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001364 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001365 opendata->o_arg.u.delegation_type = delegation_type;
1366 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001367 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return status;
1369}
1370
Trond Myklebust539cd032007-06-05 11:46:42 -04001371static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 struct nfs_server *server = NFS_SERVER(state->inode);
1374 struct nfs4_exception exception = { };
1375 int err;
1376 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001377 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001378 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001379 break;
1380 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 } while (exception.retry);
1382 return err;
1383}
1384
Trond Myklebust864472e2006-01-03 09:55:15 +01001385static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1386{
1387 struct nfs_open_context *ctx;
1388 int ret;
1389
1390 ctx = nfs4_state_find_open_context(state);
1391 if (IS_ERR(ctx))
1392 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001393 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001394 put_nfs_open_context(ctx);
1395 return ret;
1396}
1397
Trond Myklebust13437e12007-07-06 15:10:43 -04001398static 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 -07001399{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001400 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001401 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001403 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1404 if (IS_ERR(opendata))
1405 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001406 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001407 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001408 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001409 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001410 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Trond Myklebust13437e12007-07-06 15:10:43 -04001413int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001416 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 int err;
1418 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001419 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 switch (err) {
1421 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001422 case -ENOENT:
1423 case -ESTALE:
1424 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001425 case -NFS4ERR_BADSESSION:
1426 case -NFS4ERR_BADSLOT:
1427 case -NFS4ERR_BAD_HIGH_SLOT:
1428 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1429 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001430 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001431 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 case -NFS4ERR_STALE_CLIENTID:
1433 case -NFS4ERR_STALE_STATEID:
1434 case -NFS4ERR_EXPIRED:
1435 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001436 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001437 goto out;
1438 case -ERESTARTSYS:
1439 /*
1440 * The show must go on: exit, but mark the
1441 * stateid as needing recovery.
1442 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001443 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001444 case -NFS4ERR_ADMIN_REVOKED:
1445 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001446 nfs_inode_find_state_and_recover(state->inode,
1447 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001448 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001449 case -EKEYEXPIRED:
1450 /*
1451 * User RPCSEC_GSS context has expired.
1452 * We cannot recover this stateid now, so
1453 * skip it and allow recovery thread to
1454 * proceed.
1455 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001456 case -ENOMEM:
1457 err = 0;
1458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460 err = nfs4_handle_exception(server, err, &exception);
1461 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001462out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return err;
1464}
1465
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001466static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1467{
1468 struct nfs4_opendata *data = calldata;
1469
1470 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001471 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001472 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001473 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001474 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001475 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001476 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001477}
1478
1479static void nfs4_open_confirm_release(void *calldata)
1480{
1481 struct nfs4_opendata *data = calldata;
1482 struct nfs4_state *state = NULL;
1483
1484 /* If this request hasn't been cancelled, do nothing */
1485 if (data->cancelled == 0)
1486 goto out_free;
1487 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001488 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001489 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001490 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001491 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001492 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001493out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001494 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001495}
1496
1497static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001498 .rpc_call_done = nfs4_open_confirm_done,
1499 .rpc_release = nfs4_open_confirm_release,
1500};
1501
1502/*
1503 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1504 */
1505static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1506{
1507 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1508 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001509 struct rpc_message msg = {
1510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1511 .rpc_argp = &data->c_arg,
1512 .rpc_resp = &data->c_res,
1513 .rpc_cred = data->owner->so_cred,
1514 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001515 struct rpc_task_setup task_setup_data = {
1516 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001517 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001518 .callback_ops = &nfs4_open_confirm_ops,
1519 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001520 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001521 .flags = RPC_TASK_ASYNC,
1522 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 int status;
1524
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001525 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001526 data->rpc_done = 0;
1527 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001528 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001529 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001530 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001531 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001532 status = nfs4_wait_for_completion_rpc_task(task);
1533 if (status != 0) {
1534 data->cancelled = 1;
1535 smp_wmb();
1536 } else
1537 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001538 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return status;
1540}
1541
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 struct nfs4_opendata *data = calldata;
1545 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001546
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001547 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1548 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001549 /*
1550 * Check if we still need to send an OPEN call, or if we can use
1551 * a delegation instead.
1552 */
1553 if (data->state != NULL) {
1554 struct nfs_delegation *delegation;
1555
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001556 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001557 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001558 rcu_read_lock();
1559 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001560 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1561 can_open_delegated(delegation, data->o_arg.fmode))
1562 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001563 rcu_read_unlock();
1564 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001565 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001566 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001567 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001568 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001569 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001570 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1571 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001572 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001573 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001574 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001575 &data->o_res.seq_res,
1576 task) != 0)
1577 nfs_release_seqid(data->o_arg.seqid);
1578 else
1579 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001580 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001581unlock_no_action:
1582 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001583out_no_action:
1584 task->tk_action = NULL;
1585
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001588static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1589{
1590 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1591 nfs4_open_prepare(task, calldata);
1592}
1593
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001594static void nfs4_open_done(struct rpc_task *task, void *calldata)
1595{
1596 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001598 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001599
Trond Myklebust14516c32010-07-31 14:29:06 -04001600 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1601 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001602
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001603 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001604 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1605 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001606 case S_IFREG:
1607 break;
1608 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001609 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001610 break;
1611 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001613 break;
1614 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001615 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001616 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001617 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001618 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001619 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1620 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001621 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001622 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001623}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001624
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001625static void nfs4_open_release(void *calldata)
1626{
1627 struct nfs4_opendata *data = calldata;
1628 struct nfs4_state *state = NULL;
1629
1630 /* If this request hasn't been cancelled, do nothing */
1631 if (data->cancelled == 0)
1632 goto out_free;
1633 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001634 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001635 goto out_free;
1636 /* In case we need an open_confirm, no cleanup! */
1637 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1638 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001640 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001641 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001642out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001643 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001644}
1645
1646static const struct rpc_call_ops nfs4_open_ops = {
1647 .rpc_call_prepare = nfs4_open_prepare,
1648 .rpc_call_done = nfs4_open_done,
1649 .rpc_release = nfs4_open_release,
1650};
1651
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001652static const struct rpc_call_ops nfs4_recover_open_ops = {
1653 .rpc_call_prepare = nfs4_recover_open_prepare,
1654 .rpc_call_done = nfs4_open_done,
1655 .rpc_release = nfs4_open_release,
1656};
1657
1658static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001659{
1660 struct inode *dir = data->dir->d_inode;
1661 struct nfs_server *server = NFS_SERVER(dir);
1662 struct nfs_openargs *o_arg = &data->o_arg;
1663 struct nfs_openres *o_res = &data->o_res;
1664 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001665 struct rpc_message msg = {
1666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1667 .rpc_argp = o_arg,
1668 .rpc_resp = o_res,
1669 .rpc_cred = data->owner->so_cred,
1670 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001671 struct rpc_task_setup task_setup_data = {
1672 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001673 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001674 .callback_ops = &nfs4_open_ops,
1675 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001676 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001677 .flags = RPC_TASK_ASYNC,
1678 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001679 int status;
1680
Trond Myklebust9d12b212012-01-17 22:04:25 -05001681 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001682 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001683 data->rpc_done = 0;
1684 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001685 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001686 if (isrecover)
1687 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001688 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001689 if (IS_ERR(task))
1690 return PTR_ERR(task);
1691 status = nfs4_wait_for_completion_rpc_task(task);
1692 if (status != 0) {
1693 data->cancelled = 1;
1694 smp_wmb();
1695 } else
1696 status = data->rpc_status;
1697 rpc_put_task(task);
1698
1699 return status;
1700}
1701
1702static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1703{
1704 struct inode *dir = data->dir->d_inode;
1705 struct nfs_openres *o_res = &data->o_res;
1706 int status;
1707
1708 status = nfs4_run_open_task(data, 1);
1709 if (status != 0 || !data->rpc_done)
1710 return status;
1711
Trond Myklebust6926afd2012-01-07 13:22:46 -05001712 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1713
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001714 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1715 status = _nfs4_proc_open_confirm(data);
1716 if (status != 0)
1717 return status;
1718 }
1719
1720 return status;
1721}
1722
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001723static int nfs4_opendata_access(struct rpc_cred *cred,
1724 struct nfs4_opendata *opendata,
1725 struct nfs4_state *state, fmode_t fmode)
1726{
1727 struct nfs_access_entry cache;
1728 u32 mask;
1729
1730 /* access call failed or for some reason the server doesn't
1731 * support any access modes -- defer access call until later */
1732 if (opendata->o_res.access_supported == 0)
1733 return 0;
1734
1735 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001736 /* don't check MAY_WRITE - a newly created file may not have
1737 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001738 if (fmode & FMODE_READ)
1739 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001740 if (fmode & FMODE_EXEC)
1741 mask |= MAY_EXEC;
1742
1743 cache.cred = cred;
1744 cache.jiffies = jiffies;
1745 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1746 nfs_access_add_cache(state->inode, &cache);
1747
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001748 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001749 return 0;
1750
1751 /* even though OPEN succeeded, access is denied. Close the file */
1752 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001753 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001754}
1755
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001756/*
1757 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1758 */
1759static int _nfs4_proc_open(struct nfs4_opendata *data)
1760{
1761 struct inode *dir = data->dir->d_inode;
1762 struct nfs_server *server = NFS_SERVER(dir);
1763 struct nfs_openargs *o_arg = &data->o_arg;
1764 struct nfs_openres *o_res = &data->o_res;
1765 int status;
1766
1767 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001768 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001769 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001770 if (status != 0) {
1771 if (status == -NFS4ERR_BADNAME &&
1772 !(o_arg->open_flags & O_CREAT))
1773 return -ENOENT;
1774 return status;
1775 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001776
Trond Myklebust6926afd2012-01-07 13:22:46 -05001777 nfs_fattr_map_and_free_names(server, &data->f_attr);
1778
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001779 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001780 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001781 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1782 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001784 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001786 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001789 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
Andy Adamsond83217c2011-03-01 01:34:17 +00001793static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001794{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001795 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001796 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001797
Trond Myklebusta78cb572009-08-09 15:06:19 -04001798 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001799 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001800 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001801 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001802 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1803 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001804 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001805 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001806 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001807 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001808 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001809}
1810
Andy Adamsond83217c2011-03-01 01:34:17 +00001811static int nfs4_recover_expired_lease(struct nfs_server *server)
1812{
1813 return nfs4_client_recover_expired_lease(server->nfs_client);
1814}
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816/*
1817 * OPEN_EXPIRED:
1818 * reclaim state on the server after a network partition.
1819 * Assumes caller holds the appropriate lock
1820 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001821static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001823 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001824 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001826 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1827 if (IS_ERR(opendata))
1828 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001829 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001830 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001831 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001832 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001833 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001836static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001837{
Trond Myklebust539cd032007-06-05 11:46:42 -04001838 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001839 struct nfs4_exception exception = { };
1840 int err;
1841
1842 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001843 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001844 switch (err) {
1845 default:
1846 goto out;
1847 case -NFS4ERR_GRACE:
1848 case -NFS4ERR_DELAY:
1849 nfs4_handle_exception(server, err, &exception);
1850 err = 0;
1851 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001852 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001853out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001854 return err;
1855}
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1858{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001860 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Trond Myklebust864472e2006-01-03 09:55:15 +01001862 ctx = nfs4_state_find_open_context(state);
1863 if (IS_ERR(ctx))
1864 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001865 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001866 put_nfs_open_context(ctx);
1867 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001870#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001871static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001872{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001873 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001874 nfs4_stateid *stateid = &state->stateid;
1875 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001876
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001877 /* If a state reset has been done, test_stateid is unneeded */
1878 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1879 return;
1880
1881 status = nfs41_test_stateid(server, stateid);
1882 if (status != NFS_OK) {
1883 /* Free the stateid unless the server explicitly
1884 * informs us the stateid is unrecognized. */
1885 if (status != -NFS4ERR_BAD_STATEID)
1886 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001887 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001888
Bryan Schumaker69388672012-09-26 15:25:52 -04001889 write_seqlock(&state->seqlock);
1890 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1891 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001892 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1893 }
1894}
1895
1896/**
1897 * nfs41_check_open_stateid - possibly free an open stateid
1898 *
1899 * @state: NFSv4 state for an inode
1900 *
1901 * Returns NFS_OK if recovery for this stateid is now finished.
1902 * Otherwise a negative NFS4ERR value is returned.
1903 */
1904static int nfs41_check_open_stateid(struct nfs4_state *state)
1905{
1906 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001907 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001908 int status;
1909
1910 /* If a state reset has been done, test_stateid is unneeded */
1911 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1912 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1913 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1914 return -NFS4ERR_BAD_STATEID;
1915
1916 status = nfs41_test_stateid(server, stateid);
1917 if (status != NFS_OK) {
1918 /* Free the stateid unless the server explicitly
1919 * informs us the stateid is unrecognized. */
1920 if (status != -NFS4ERR_BAD_STATEID)
1921 nfs41_free_stateid(server, stateid);
1922
1923 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1924 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1925 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001926 }
1927 return status;
1928}
1929
1930static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1931{
Chuck Levereb64cf92012-07-11 16:30:05 -04001932 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001933
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001934 nfs41_clear_delegation_stateid(state);
1935 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001936 if (status != NFS_OK)
1937 status = nfs4_open_expired(sp, state);
1938 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001939}
1940#endif
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001943 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1944 * fields corresponding to attributes that were used to store the verifier.
1945 * Make sure we clobber those fields in the later setattr call
1946 */
1947static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1948{
1949 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1950 !(sattr->ia_valid & ATTR_ATIME_SET))
1951 sattr->ia_valid |= ATTR_ATIME;
1952
1953 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1954 !(sattr->ia_valid & ATTR_MTIME_SET))
1955 sattr->ia_valid |= ATTR_MTIME;
1956}
1957
1958/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001959 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
Andy Adamson82be4172012-05-23 05:02:35 -04001961static int _nfs4_do_open(struct inode *dir,
1962 struct dentry *dentry,
1963 fmode_t fmode,
1964 int flags,
1965 struct iattr *sattr,
1966 struct rpc_cred *cred,
1967 struct nfs4_state **res,
1968 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 struct nfs4_state_owner *sp;
1971 struct nfs4_state *state = NULL;
1972 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001973 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001974 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001978 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1979 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1981 goto out_err;
1982 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001983 status = nfs4_recover_expired_lease(server);
1984 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001985 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001986 if (dentry->d_inode != NULL)
1987 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001988 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001989 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001990 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001991 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Andy Adamson82be4172012-05-23 05:02:35 -04001993 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1994 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1995 if (!opendata->f_attr.mdsthreshold)
1996 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001997 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001998 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001999 if (dentry->d_inode != NULL)
2000 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002001
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002002 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002004 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002006 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002007 status = PTR_ERR(state);
2008 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002009 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002010 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002011 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002012
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002013 status = nfs4_opendata_access(cred, opendata, state, fmode);
2014 if (status != 0)
2015 goto err_opendata_put;
2016
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002017 if (opendata->o_arg.open_flags & O_EXCL) {
2018 nfs4_exclusive_attrset(opendata, sattr);
2019
2020 nfs_fattr_init(opendata->o_res.f_attr);
2021 status = nfs4_do_setattr(state->inode, cred,
2022 opendata->o_res.f_attr, sattr,
2023 state);
2024 if (status == 0)
2025 nfs_setattr_update_inode(state->inode, sattr);
2026 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2027 }
Andy Adamson82be4172012-05-23 05:02:35 -04002028
2029 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2030 *ctx_th = opendata->f_attr.mdsthreshold;
2031 else
2032 kfree(opendata->f_attr.mdsthreshold);
2033 opendata->f_attr.mdsthreshold = NULL;
2034
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002035 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 *res = state;
2038 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002039err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002040 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002041 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002042err_put_state_owner:
2043 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 *res = NULL;
2046 return status;
2047}
2048
2049
Andy Adamson82be4172012-05-23 05:02:35 -04002050static struct nfs4_state *nfs4_do_open(struct inode *dir,
2051 struct dentry *dentry,
2052 fmode_t fmode,
2053 int flags,
2054 struct iattr *sattr,
2055 struct rpc_cred *cred,
2056 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 struct nfs4_exception exception = { };
2059 struct nfs4_state *res;
2060 int status;
2061
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002062 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002064 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2065 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (status == 0)
2067 break;
2068 /* NOTE: BAD_SEQID means the server and client disagree about the
2069 * book-keeping w.r.t. state-changing operations
2070 * (OPEN/CLOSE/LOCK/LOCKU...)
2071 * It is actually a sign of a bug on the client or on the server.
2072 *
2073 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002074 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 * have unhashed the old state_owner for us, and that we can
2076 * therefore safely retry using a new one. We should still warn
2077 * the user though...
2078 */
2079 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002080 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002081 " returned a bad sequence-id error!\n",
2082 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 exception.retry = 1;
2084 continue;
2085 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002086 /*
2087 * BAD_STATEID on OPEN means that the server cancelled our
2088 * state before it received the OPEN_CONFIRM.
2089 * Recover by retrying the request as per the discussion
2090 * on Page 181 of RFC3530.
2091 */
2092 if (status == -NFS4ERR_BAD_STATEID) {
2093 exception.retry = 1;
2094 continue;
2095 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002096 if (status == -EAGAIN) {
2097 /* We must have found a delegation */
2098 exception.retry = 1;
2099 continue;
2100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2102 status, &exception));
2103 } while (exception.retry);
2104 return res;
2105}
2106
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002107static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2108 struct nfs_fattr *fattr, struct iattr *sattr,
2109 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002111 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002113 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 .iap = sattr,
2115 .server = server,
2116 .bitmask = server->attr_bitmask,
2117 };
2118 struct nfs_setattrres res = {
2119 .fattr = fattr,
2120 .server = server,
2121 };
2122 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2124 .rpc_argp = &arg,
2125 .rpc_resp = &res,
2126 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002128 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002129 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Trond Myklebust0e574af2005-10-27 22:12:38 -04002131 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Trond Myklebust4fc87962012-03-08 17:42:01 -05002133 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002134 struct nfs_lockowner lockowner = {
2135 .l_owner = current->files,
2136 .l_pid = current->tgid,
2137 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002138 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002139 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002140 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2141 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002142 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002143 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002144 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Bryan Schumaker7c513052011-03-24 17:12:24 +00002146 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002147 if (status == 0 && state != NULL)
2148 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002149 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002152static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2153 struct nfs_fattr *fattr, struct iattr *sattr,
2154 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002156 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002157 struct nfs4_exception exception = {
2158 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002159 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002160 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 int err;
2162 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002163 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2164 switch (err) {
2165 case -NFS4ERR_OPENMODE:
2166 if (state && !(state->state & FMODE_WRITE)) {
2167 err = -EBADF;
2168 if (sattr->ia_valid & ATTR_OPEN)
2169 err = -EACCES;
2170 goto out;
2171 }
2172 }
2173 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002175out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return err;
2177}
2178
2179struct nfs4_closedata {
2180 struct inode *inode;
2181 struct nfs4_state *state;
2182 struct nfs_closeargs arg;
2183 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002184 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002185 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002186 bool roc;
2187 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188};
2189
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002190static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002191{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002192 struct nfs4_closedata *calldata = data;
2193 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002194 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002195
Fred Isamanf7e89172011-01-06 11:36:32 +00002196 if (calldata->roc)
2197 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002198 nfs4_put_open_state(calldata->state);
2199 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002200 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002201 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002202 kfree(calldata);
2203}
2204
Trond Myklebust88069f72009-12-08 08:33:16 -05002205static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2206 fmode_t fmode)
2207{
2208 spin_lock(&state->owner->so_lock);
2209 if (!(fmode & FMODE_READ))
2210 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2211 if (!(fmode & FMODE_WRITE))
2212 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2213 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2214 spin_unlock(&state->owner->so_lock);
2215}
2216
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002217static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002219 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct nfs_server *server = NFS_SERVER(calldata->inode);
2222
Chuck Levera3ca5652012-03-01 17:00:40 -05002223 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002224 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2225 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /* hmm. we are done with the inode, and in the process of freeing
2227 * the state_owner. we keep this around to process errors
2228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 switch (task->tk_status) {
2230 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002231 if (calldata->roc)
2232 pnfs_roc_set_barrier(state->inode,
2233 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002234 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002235 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002236 nfs4_close_clear_stateid_flags(state,
2237 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 break;
2239 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002240 case -NFS4ERR_OLD_STATEID:
2241 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002243 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002244 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002246 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2247 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002249 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002250 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002251 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002254static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002256 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002257 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002258 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002259 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002260
Chuck Levera3ca5652012-03-01 17:00:40 -05002261 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002262 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002263 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002264
Trond Myklebust88069f72009-12-08 08:33:16 -05002265 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2266 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002267 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002268 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002269 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002270 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002271 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2272 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2273 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002274 }
2275 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002276 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2277 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2278 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002279 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002280 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002281 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002282
2283 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002284 /* Note: exit _without_ calling nfs4_close_done */
2285 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002286 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002287 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002288
Fred Isamanf7e89172011-01-06 11:36:32 +00002289 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002290 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002291 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002292 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002293 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002294 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002295
Trond Myklebust516a6af2005-10-27 22:12:41 -04002296 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002297 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002298 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002299 &calldata->arg.seq_args,
2300 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002301 task) != 0)
2302 nfs_release_seqid(calldata->arg.seqid);
2303 else
2304 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002305out:
2306 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002309static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002310 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002311 .rpc_call_done = nfs4_close_done,
2312 .rpc_release = nfs4_free_closedata,
2313};
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315/*
2316 * It is possible for data to be read/written from a mem-mapped file
2317 * after the sys_close call (which hits the vfs layer as a flush).
2318 * This means that we can't safely call nfsv4 close on a file until
2319 * the inode is cleared. This in turn means that we are not good
2320 * NFSv4 citizens - we do not indicate to the server to update the file's
2321 * share state even when we are done with one of the three share
2322 * stateid's in the inode.
2323 *
2324 * NOTE: Caller must be holding the sp->so_owner semaphore!
2325 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002326int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002328 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002330 struct nfs4_state_owner *sp = state->owner;
2331 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002332 struct rpc_message msg = {
2333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2334 .rpc_cred = state->owner->so_cred,
2335 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002336 struct rpc_task_setup task_setup_data = {
2337 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002338 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002339 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002340 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002341 .flags = RPC_TASK_ASYNC,
2342 };
Trond Myklebust95121352005-10-18 14:20:12 -07002343 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002345 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002347 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002348 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002349 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002351 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002352 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002354 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002355 if (calldata->arg.seqid == NULL)
2356 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002357 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002358 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002359 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002360 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002361 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002362 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002363 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002364
Trond Myklebust1174dd12010-12-21 10:52:24 -05002365 msg.rpc_argp = &calldata->arg;
2366 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002367 task_setup_data.callback_data = calldata;
2368 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002369 if (IS_ERR(task))
2370 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002371 status = 0;
2372 if (wait)
2373 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002374 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002375 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002376out_free_calldata:
2377 kfree(calldata);
2378out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002379 nfs4_put_open_state(state);
2380 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002381 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Trond Myklebust2b484292010-09-17 10:56:51 -04002384static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002385nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 struct nfs4_state *state;
2388
Trond Myklebust565277f2007-10-15 18:17:53 -04002389 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002390 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2391 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002392 if (IS_ERR(state))
2393 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002394 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002395 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396}
2397
Trond Myklebust1185a552009-12-03 15:54:02 -05002398static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002399{
2400 if (ctx->state == NULL)
2401 return;
2402 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002403 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002404 else
Al Viro643168c2011-06-22 18:20:23 -04002405 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002406}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2409{
Benny Halevy43652ad2009-04-01 09:21:54 -04002410 struct nfs4_server_caps_arg args = {
2411 .fhandle = fhandle,
2412 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 struct nfs4_server_caps_res res = {};
2414 struct rpc_message msg = {
2415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002416 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 .rpc_resp = &res,
2418 };
2419 int status;
2420
Bryan Schumaker7c513052011-03-24 17:12:24 +00002421 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (status == 0) {
2423 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002424 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2425 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2426 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2427 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2428 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2430 server->caps |= NFS_CAP_ACLS;
2431 if (res.has_links != 0)
2432 server->caps |= NFS_CAP_HARDLINKS;
2433 if (res.has_symlinks != 0)
2434 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002435 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2436 server->caps |= NFS_CAP_FILEID;
2437 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2438 server->caps |= NFS_CAP_MODE;
2439 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2440 server->caps |= NFS_CAP_NLINK;
2441 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2442 server->caps |= NFS_CAP_OWNER;
2443 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2444 server->caps |= NFS_CAP_OWNER_GROUP;
2445 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2446 server->caps |= NFS_CAP_ATIME;
2447 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2448 server->caps |= NFS_CAP_CTIME;
2449 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2450 server->caps |= NFS_CAP_MTIME;
2451
Trond Myklebusta65318b2009-03-11 14:10:28 -04002452 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2453 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2454 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002456 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return status;
2460}
2461
Trond Myklebust55a97592006-06-09 09:34:19 -04002462int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 struct nfs4_exception exception = { };
2465 int err;
2466 do {
2467 err = nfs4_handle_exception(server,
2468 _nfs4_server_capabilities(server, fhandle),
2469 &exception);
2470 } while (exception.retry);
2471 return err;
2472}
2473
2474static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2475 struct nfs_fsinfo *info)
2476{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 struct nfs4_lookup_root_arg args = {
2478 .bitmask = nfs4_fattr_bitmap,
2479 };
2480 struct nfs4_lookup_res res = {
2481 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002482 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 .fh = fhandle,
2484 };
2485 struct rpc_message msg = {
2486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2487 .rpc_argp = &args,
2488 .rpc_resp = &res,
2489 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002490
Trond Myklebust0e574af2005-10-27 22:12:38 -04002491 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002492 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}
2494
2495static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2496 struct nfs_fsinfo *info)
2497{
2498 struct nfs4_exception exception = { };
2499 int err;
2500 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002501 err = _nfs4_lookup_root(server, fhandle, info);
2502 switch (err) {
2503 case 0:
2504 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002505 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002506 default:
2507 err = nfs4_handle_exception(server, err, &exception);
2508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002510out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 return err;
2512}
2513
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002514static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2515 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2516{
2517 struct rpc_auth *auth;
2518 int ret;
2519
2520 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002521 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002522 ret = -EIO;
2523 goto out;
2524 }
2525 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002526out:
2527 return ret;
2528}
2529
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002530static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002531 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002533 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002534 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002536 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002537 if (len < 0)
2538 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002539
2540 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002541 /* AUTH_UNIX is the default flavor if none was specified,
2542 * thus has already been tried. */
2543 if (flav_array[i] == RPC_AUTH_UNIX)
2544 continue;
2545
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002546 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002547 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002548 continue;
2549 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002550 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002551 /*
2552 * -EACCESS could mean that the user doesn't have correct permissions
2553 * to access the mount. It could also mean that we tried to mount
2554 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2555 * existing mount programs don't handle -EACCES very well so it should
2556 * be mapped to -EPERM instead.
2557 */
2558 if (status == -EACCES)
2559 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002560 return status;
2561}
2562
2563/*
2564 * get the file handle for the "/" directory on the server
2565 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002566int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2567 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002568{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002569 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002570 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002571 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2572 /*
2573 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2574 * by nfs4_map_errors() as this function exits.
2575 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002576 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (status == 0)
2578 status = nfs4_server_capabilities(server, fhandle);
2579 if (status == 0)
2580 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002581 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
Bryan Schumakerbae36242012-05-10 15:07:31 -04002584static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2585 struct nfs_fsinfo *info)
2586{
2587 int error;
2588 struct nfs_fattr *fattr = info->fattr;
2589
2590 error = nfs4_server_capabilities(server, mntfh);
2591 if (error < 0) {
2592 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2593 return error;
2594 }
2595
2596 error = nfs4_proc_getattr(server, mntfh, fattr);
2597 if (error < 0) {
2598 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2599 return error;
2600 }
2601
2602 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2603 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2604 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2605
2606 return error;
2607}
2608
Manoj Naik6b97fd32006-06-09 09:34:29 -04002609/*
2610 * Get locations and (maybe) other attributes of a referral.
2611 * Note that we'll actually follow the referral later when
2612 * we detect fsid mismatch in inode revalidation
2613 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002614static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2615 const struct qstr *name, struct nfs_fattr *fattr,
2616 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002617{
2618 int status = -ENOMEM;
2619 struct page *page = NULL;
2620 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002621
2622 page = alloc_page(GFP_KERNEL);
2623 if (page == NULL)
2624 goto out;
2625 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2626 if (locations == NULL)
2627 goto out;
2628
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002629 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002630 if (status != 0)
2631 goto out;
2632 /* Make sure server returned a different fsid for the referral */
2633 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002634 dprintk("%s: server did not return a different fsid for"
2635 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002636 status = -EIO;
2637 goto out;
2638 }
Andy Adamson533eb462011-06-13 18:25:56 -04002639 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2640 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002641
Andy Adamson533eb462011-06-13 18:25:56 -04002642 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002643 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002644 memset(fhandle, 0, sizeof(struct nfs_fh));
2645out:
2646 if (page)
2647 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002648 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002649 return status;
2650}
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2653{
2654 struct nfs4_getattr_arg args = {
2655 .fh = fhandle,
2656 .bitmask = server->attr_bitmask,
2657 };
2658 struct nfs4_getattr_res res = {
2659 .fattr = fattr,
2660 .server = server,
2661 };
2662 struct rpc_message msg = {
2663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2664 .rpc_argp = &args,
2665 .rpc_resp = &res,
2666 };
2667
Trond Myklebust0e574af2005-10-27 22:12:38 -04002668 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002669 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670}
2671
2672static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2673{
2674 struct nfs4_exception exception = { };
2675 int err;
2676 do {
2677 err = nfs4_handle_exception(server,
2678 _nfs4_proc_getattr(server, fhandle, fattr),
2679 &exception);
2680 } while (exception.retry);
2681 return err;
2682}
2683
2684/*
2685 * The file is not closed if it is opened due to the a request to change
2686 * the size of the file. The open call will not be needed once the
2687 * VFS layer lookup-intents are implemented.
2688 *
2689 * Close is called when the inode is destroyed.
2690 * If we haven't opened the file for O_WRONLY, we
2691 * need to in the size_change case to obtain a stateid.
2692 *
2693 * Got race?
2694 * Because OPEN is always done by name in nfsv4, it is
2695 * possible that we opened a different file by the same
2696 * name. We can recognize this race condition, but we
2697 * can't do anything about it besides returning an error.
2698 *
2699 * This will be fixed with VFS changes (lookup-intent).
2700 */
2701static int
2702nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2703 struct iattr *sattr)
2704{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002705 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002706 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002707 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 int status;
2709
Benny Halevy8a1636c2010-07-14 15:43:57 -04002710 if (pnfs_ld_layoutret_on_setattr(inode))
2711 pnfs_return_layout(inode);
2712
Trond Myklebust0e574af2005-10-27 22:12:38 -04002713 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Andy Adamson26699402012-05-30 16:12:24 -04002715 /* Deal with open(O_TRUNC) */
2716 if (sattr->ia_valid & ATTR_OPEN)
2717 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2718
2719 /* Optimization: if the end result is no change, don't RPC */
2720 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2721 return 0;
2722
Trond Myklebustd5308382005-11-04 15:33:38 -05002723 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002724 if (sattr->ia_valid & ATTR_FILE) {
2725 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002726
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002727 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002728 if (ctx) {
2729 cred = ctx->cred;
2730 state = ctx->state;
2731 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002732 }
2733
2734 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002735 if (status == 0)
2736 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return status;
2738}
2739
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002740static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2741 const struct qstr *name, struct nfs_fh *fhandle,
2742 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002743{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002744 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002745 int status;
2746 struct nfs4_lookup_arg args = {
2747 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002748 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002749 .name = name,
2750 };
2751 struct nfs4_lookup_res res = {
2752 .server = server,
2753 .fattr = fattr,
2754 .fh = fhandle,
2755 };
2756 struct rpc_message msg = {
2757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2758 .rpc_argp = &args,
2759 .rpc_resp = &res,
2760 };
2761
2762 nfs_fattr_init(fattr);
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002765 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 dprintk("NFS reply lookup: %d\n", status);
2767 return status;
2768}
2769
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002770static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002771{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002772 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002773 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002774 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2775 fattr->nlink = 2;
2776}
2777
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002778static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2779 struct qstr *name, struct nfs_fh *fhandle,
2780 struct nfs_fattr *fattr)
2781{
2782 struct nfs4_exception exception = { };
2783 struct rpc_clnt *client = *clnt;
2784 int err;
2785 do {
2786 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2787 switch (err) {
2788 case -NFS4ERR_BADNAME:
2789 err = -ENOENT;
2790 goto out;
2791 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002792 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002793 goto out;
2794 case -NFS4ERR_WRONGSEC:
2795 err = -EPERM;
2796 if (client != *clnt)
2797 goto out;
2798
2799 client = nfs4_create_sec_client(client, dir, name);
2800 if (IS_ERR(client))
2801 return PTR_ERR(client);
2802
2803 exception.retry = 1;
2804 break;
2805 default:
2806 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2807 }
2808 } while (exception.retry);
2809
2810out:
2811 if (err == 0)
2812 *clnt = client;
2813 else if (client != *clnt)
2814 rpc_shutdown_client(client);
2815
2816 return err;
2817}
2818
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002819static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002820 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002822 int status;
2823 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002824
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002825 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2826 if (client != NFS_CLIENT(dir)) {
2827 rpc_shutdown_client(client);
2828 nfs_fixup_secinfo_attributes(fattr);
2829 }
2830 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
2832
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002833struct rpc_clnt *
2834nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2835 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2836{
2837 int status;
2838 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2839
2840 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2841 if (status < 0) {
2842 rpc_shutdown_client(client);
2843 return ERR_PTR(status);
2844 }
2845 return client;
2846}
2847
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2849{
Trond Myklebust76b32992007-08-10 17:45:11 -04002850 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 struct nfs4_accessargs args = {
2852 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002853 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002855 struct nfs4_accessres res = {
2856 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002857 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 struct rpc_message msg = {
2859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2860 .rpc_argp = &args,
2861 .rpc_resp = &res,
2862 .rpc_cred = entry->cred,
2863 };
2864 int mode = entry->mask;
2865 int status;
2866
2867 /*
2868 * Determine which access bits we want to ask for...
2869 */
2870 if (mode & MAY_READ)
2871 args.access |= NFS4_ACCESS_READ;
2872 if (S_ISDIR(inode->i_mode)) {
2873 if (mode & MAY_WRITE)
2874 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2875 if (mode & MAY_EXEC)
2876 args.access |= NFS4_ACCESS_LOOKUP;
2877 } else {
2878 if (mode & MAY_WRITE)
2879 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2880 if (mode & MAY_EXEC)
2881 args.access |= NFS4_ACCESS_EXECUTE;
2882 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002883
2884 res.fattr = nfs_alloc_fattr();
2885 if (res.fattr == NULL)
2886 return -ENOMEM;
2887
Bryan Schumaker7c513052011-03-24 17:12:24 +00002888 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002890 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002891 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002893 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return status;
2895}
2896
2897static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2898{
2899 struct nfs4_exception exception = { };
2900 int err;
2901 do {
2902 err = nfs4_handle_exception(NFS_SERVER(inode),
2903 _nfs4_proc_access(inode, entry),
2904 &exception);
2905 } while (exception.retry);
2906 return err;
2907}
2908
2909/*
2910 * TODO: For the time being, we don't try to get any attributes
2911 * along with any of the zero-copy operations READ, READDIR,
2912 * READLINK, WRITE.
2913 *
2914 * In the case of the first three, we want to put the GETATTR
2915 * after the read-type operation -- this is because it is hard
2916 * to predict the length of a GETATTR response in v4, and thus
2917 * align the READ data correctly. This means that the GETATTR
2918 * may end up partially falling into the page cache, and we should
2919 * shift it into the 'tail' of the xdr_buf before processing.
2920 * To do this efficiently, we need to know the total length
2921 * of data received, which doesn't seem to be available outside
2922 * of the RPC layer.
2923 *
2924 * In the case of WRITE, we also want to put the GETATTR after
2925 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002926 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 *
2928 * Both of these changes to the XDR layer would in fact be quite
2929 * minor, but I decided to leave them for a subsequent patch.
2930 */
2931static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2932 unsigned int pgbase, unsigned int pglen)
2933{
2934 struct nfs4_readlink args = {
2935 .fh = NFS_FH(inode),
2936 .pgbase = pgbase,
2937 .pglen = pglen,
2938 .pages = &page,
2939 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002940 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 struct rpc_message msg = {
2942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2943 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002944 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 };
2946
Bryan Schumaker7c513052011-03-24 17:12:24 +00002947 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 -07002948}
2949
2950static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2951 unsigned int pgbase, unsigned int pglen)
2952{
2953 struct nfs4_exception exception = { };
2954 int err;
2955 do {
2956 err = nfs4_handle_exception(NFS_SERVER(inode),
2957 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2958 &exception);
2959 } while (exception.retry);
2960 return err;
2961}
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002964 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966static int
2967nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002968 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002970 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 int status = 0;
2973
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002974 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2975 if (IS_ERR(ctx))
2976 return PTR_ERR(ctx);
2977
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002978 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002979 state = nfs4_do_open(dir, dentry, ctx->mode,
2980 flags, sattr, ctx->cred,
2981 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002982 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 if (IS_ERR(state)) {
2984 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002985 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002987 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002988 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002989 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002991 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 return status;
2993}
2994
2995static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2996{
Trond Myklebust16e42952005-10-27 22:12:44 -04002997 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002998 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003000 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003002 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003003 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003004 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3007 .rpc_argp = &args,
3008 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003010 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003011
Bryan Schumaker7c513052011-03-24 17:12:24 +00003012 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003013 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003014 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return status;
3016}
3017
3018static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3019{
3020 struct nfs4_exception exception = { };
3021 int err;
3022 do {
3023 err = nfs4_handle_exception(NFS_SERVER(dir),
3024 _nfs4_proc_remove(dir, name),
3025 &exception);
3026 } while (exception.retry);
3027 return err;
3028}
3029
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003030static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003032 struct nfs_server *server = NFS_SERVER(dir);
3033 struct nfs_removeargs *args = msg->rpc_argp;
3034 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003036 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003038 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003041static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3042{
3043 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3044 &data->args.seq_args,
3045 &data->res.seq_res,
3046 task))
3047 return;
3048 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003051static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003053 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3054
Trond Myklebust14516c32010-07-31 14:29:06 -04003055 if (!nfs4_sequence_done(task, &res->seq_res))
3056 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003057 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003058 return 0;
3059 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003060 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
Jeff Laytond3d41522010-09-17 17:31:57 -04003063static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3064{
3065 struct nfs_server *server = NFS_SERVER(dir);
3066 struct nfs_renameargs *arg = msg->rpc_argp;
3067 struct nfs_renameres *res = msg->rpc_resp;
3068
3069 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003070 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003071 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003072}
3073
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003074static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3075{
3076 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3077 &data->args.seq_args,
3078 &data->res.seq_res,
3079 task))
3080 return;
3081 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003082}
3083
3084static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3085 struct inode *new_dir)
3086{
3087 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3088
3089 if (!nfs4_sequence_done(task, &res->seq_res))
3090 return 0;
3091 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3092 return 0;
3093
3094 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003095 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003096 return 1;
3097}
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3100 struct inode *new_dir, struct qstr *new_name)
3101{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003102 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003103 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 .old_dir = NFS_FH(old_dir),
3105 .new_dir = NFS_FH(new_dir),
3106 .old_name = old_name,
3107 .new_name = new_name,
3108 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003109 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003110 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003111 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 struct rpc_message msg = {
3113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3114 .rpc_argp = &arg,
3115 .rpc_resp = &res,
3116 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003117 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Bryan Schumaker7c513052011-03-24 17:12:24 +00003119 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (!status) {
3121 update_changeattr(old_dir, &res.old_cinfo);
3122 update_changeattr(new_dir, &res.new_cinfo);
3123 }
3124 return status;
3125}
3126
3127static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3128 struct inode *new_dir, struct qstr *new_name)
3129{
3130 struct nfs4_exception exception = { };
3131 int err;
3132 do {
3133 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3134 _nfs4_proc_rename(old_dir, old_name,
3135 new_dir, new_name),
3136 &exception);
3137 } while (exception.retry);
3138 return err;
3139}
3140
3141static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3142{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003143 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 struct nfs4_link_arg arg = {
3145 .fh = NFS_FH(inode),
3146 .dir_fh = NFS_FH(dir),
3147 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003148 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003150 struct nfs4_link_res res = {
3151 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 struct rpc_message msg = {
3154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3155 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003156 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003158 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Trond Myklebust136f2622010-04-16 16:22:49 -04003160 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003161 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003162 goto out;
3163
Bryan Schumaker7c513052011-03-24 17:12:24 +00003164 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003165 if (!status) {
3166 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003167 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003168 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003169out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003170 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return status;
3172}
3173
3174static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3175{
3176 struct nfs4_exception exception = { };
3177 int err;
3178 do {
3179 err = nfs4_handle_exception(NFS_SERVER(inode),
3180 _nfs4_proc_link(inode, dir, name),
3181 &exception);
3182 } while (exception.retry);
3183 return err;
3184}
3185
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003186struct nfs4_createdata {
3187 struct rpc_message msg;
3188 struct nfs4_create_arg arg;
3189 struct nfs4_create_res res;
3190 struct nfs_fh fh;
3191 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003192};
3193
3194static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3195 struct qstr *name, struct iattr *sattr, u32 ftype)
3196{
3197 struct nfs4_createdata *data;
3198
3199 data = kzalloc(sizeof(*data), GFP_KERNEL);
3200 if (data != NULL) {
3201 struct nfs_server *server = NFS_SERVER(dir);
3202
3203 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3204 data->msg.rpc_argp = &data->arg;
3205 data->msg.rpc_resp = &data->res;
3206 data->arg.dir_fh = NFS_FH(dir);
3207 data->arg.server = server;
3208 data->arg.name = name;
3209 data->arg.attrs = sattr;
3210 data->arg.ftype = ftype;
3211 data->arg.bitmask = server->attr_bitmask;
3212 data->res.server = server;
3213 data->res.fh = &data->fh;
3214 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003215 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003216 }
3217 return data;
3218}
3219
3220static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3221{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003222 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003223 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003224 if (status == 0) {
3225 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003226 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3227 }
3228 return status;
3229}
3230
3231static void nfs4_free_createdata(struct nfs4_createdata *data)
3232{
3233 kfree(data);
3234}
3235
Chuck Lever4f390c12006-08-22 20:06:22 -04003236static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003237 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003239 struct nfs4_createdata *data;
3240 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Chuck Lever94a6d752006-08-22 20:06:23 -04003242 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003243 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003244
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003245 status = -ENOMEM;
3246 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3247 if (data == NULL)
3248 goto out;
3249
3250 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3251 data->arg.u.symlink.pages = &page;
3252 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003254 status = nfs4_do_create(dir, dentry, data);
3255
3256 nfs4_free_createdata(data);
3257out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return status;
3259}
3260
Chuck Lever4f390c12006-08-22 20:06:22 -04003261static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003262 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
3264 struct nfs4_exception exception = { };
3265 int err;
3266 do {
3267 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003268 _nfs4_proc_symlink(dir, dentry, page,
3269 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 &exception);
3271 } while (exception.retry);
3272 return err;
3273}
3274
3275static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3276 struct iattr *sattr)
3277{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003278 struct nfs4_createdata *data;
3279 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003281 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3282 if (data == NULL)
3283 goto out;
3284
3285 status = nfs4_do_create(dir, dentry, data);
3286
3287 nfs4_free_createdata(data);
3288out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 return status;
3290}
3291
3292static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3293 struct iattr *sattr)
3294{
3295 struct nfs4_exception exception = { };
3296 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003297
3298 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 do {
3300 err = nfs4_handle_exception(NFS_SERVER(dir),
3301 _nfs4_proc_mkdir(dir, dentry, sattr),
3302 &exception);
3303 } while (exception.retry);
3304 return err;
3305}
3306
3307static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003308 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
3310 struct inode *dir = dentry->d_inode;
3311 struct nfs4_readdir_arg args = {
3312 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003313 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 .pgbase = 0,
3315 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003316 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003317 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 };
3319 struct nfs4_readdir_res res;
3320 struct rpc_message msg = {
3321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3322 .rpc_argp = &args,
3323 .rpc_resp = &res,
3324 .rpc_cred = cred,
3325 };
3326 int status;
3327
Harvey Harrison3110ff82008-05-02 13:42:44 -07003328 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003329 dentry->d_parent->d_name.name,
3330 dentry->d_name.name,
3331 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003332 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003334 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 -05003335 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003336 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003337 status += args.pgbase;
3338 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003339
3340 nfs_invalidate_atime(dir);
3341
Harvey Harrison3110ff82008-05-02 13:42:44 -07003342 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 return status;
3344}
3345
3346static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003347 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
3349 struct nfs4_exception exception = { };
3350 int err;
3351 do {
3352 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3353 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003354 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 &exception);
3356 } while (exception.retry);
3357 return err;
3358}
3359
3360static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3361 struct iattr *sattr, dev_t rdev)
3362{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003363 struct nfs4_createdata *data;
3364 int mode = sattr->ia_mode;
3365 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003367 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3368 if (data == NULL)
3369 goto out;
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003372 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003374 data->arg.ftype = NF4BLK;
3375 data->arg.u.device.specdata1 = MAJOR(rdev);
3376 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 }
3378 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003379 data->arg.ftype = NF4CHR;
3380 data->arg.u.device.specdata1 = MAJOR(rdev);
3381 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003382 } else if (!S_ISSOCK(mode)) {
3383 status = -EINVAL;
3384 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003387 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003388out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003389 nfs4_free_createdata(data);
3390out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 return status;
3392}
3393
3394static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3395 struct iattr *sattr, dev_t rdev)
3396{
3397 struct nfs4_exception exception = { };
3398 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003399
3400 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 do {
3402 err = nfs4_handle_exception(NFS_SERVER(dir),
3403 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3404 &exception);
3405 } while (exception.retry);
3406 return err;
3407}
3408
3409static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3410 struct nfs_fsstat *fsstat)
3411{
3412 struct nfs4_statfs_arg args = {
3413 .fh = fhandle,
3414 .bitmask = server->attr_bitmask,
3415 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003416 struct nfs4_statfs_res res = {
3417 .fsstat = fsstat,
3418 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 struct rpc_message msg = {
3420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3421 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003422 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 };
3424
Trond Myklebust0e574af2005-10-27 22:12:38 -04003425 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003426 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
3428
3429static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3430{
3431 struct nfs4_exception exception = { };
3432 int err;
3433 do {
3434 err = nfs4_handle_exception(server,
3435 _nfs4_proc_statfs(server, fhandle, fsstat),
3436 &exception);
3437 } while (exception.retry);
3438 return err;
3439}
3440
3441static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3442 struct nfs_fsinfo *fsinfo)
3443{
3444 struct nfs4_fsinfo_arg args = {
3445 .fh = fhandle,
3446 .bitmask = server->attr_bitmask,
3447 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003448 struct nfs4_fsinfo_res res = {
3449 .fsinfo = fsinfo,
3450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 struct rpc_message msg = {
3452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3453 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003454 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 };
3456
Bryan Schumaker7c513052011-03-24 17:12:24 +00003457 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
3460static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3461{
3462 struct nfs4_exception exception = { };
3463 int err;
3464
3465 do {
3466 err = nfs4_handle_exception(server,
3467 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3468 &exception);
3469 } while (exception.retry);
3470 return err;
3471}
3472
3473static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3474{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003475 int error;
3476
Trond Myklebust0e574af2005-10-27 22:12:38 -04003477 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003478 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003479 if (error == 0) {
3480 /* block layout checks this! */
3481 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003482 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003483 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003484
3485 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
3488static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3489 struct nfs_pathconf *pathconf)
3490{
3491 struct nfs4_pathconf_arg args = {
3492 .fh = fhandle,
3493 .bitmask = server->attr_bitmask,
3494 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003495 struct nfs4_pathconf_res res = {
3496 .pathconf = pathconf,
3497 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 struct rpc_message msg = {
3499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3500 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003501 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 };
3503
3504 /* None of the pathconf attributes are mandatory to implement */
3505 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3506 memset(pathconf, 0, sizeof(*pathconf));
3507 return 0;
3508 }
3509
Trond Myklebust0e574af2005-10-27 22:12:38 -04003510 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003511 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
3514static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3515 struct nfs_pathconf *pathconf)
3516{
3517 struct nfs4_exception exception = { };
3518 int err;
3519
3520 do {
3521 err = nfs4_handle_exception(server,
3522 _nfs4_proc_pathconf(server, fhandle, pathconf),
3523 &exception);
3524 } while (exception.retry);
3525 return err;
3526}
3527
Benny Halevyd20581a2011-05-22 19:52:03 +03003528void __nfs4_read_done_cb(struct nfs_read_data *data)
3529{
Fred Isamancd841602012-04-20 14:47:44 -04003530 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003531}
3532
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003533static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
Fred Isamancd841602012-04-20 14:47:44 -04003535 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003537 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003538 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003539 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003541
Benny Halevyd20581a2011-05-22 19:52:03 +03003542 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003544 renew_lease(server, data->timestamp);
3545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003548static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3549{
3550
3551 dprintk("--> %s\n", __func__);
3552
3553 if (!nfs4_sequence_done(task, &data->res.seq_res))
3554 return -EAGAIN;
3555
Benny Halevyd20581a2011-05-22 19:52:03 +03003556 return data->read_done_cb ? data->read_done_cb(task, data) :
3557 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003558}
3559
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003560static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003563 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003564 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003565 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003568static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3569{
Fred Isamancd841602012-04-20 14:47:44 -04003570 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003571 &data->args.seq_args,
3572 &data->res.seq_res,
3573 task))
3574 return;
3575 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Fred Isamanb029bc92011-03-03 15:13:42 +00003578static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Fred Isamancd841602012-04-20 14:47:44 -04003580 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003582 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003583 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003584 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003586 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003588 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003589 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
Fred Isamanb029bc92011-03-03 15:13:42 +00003593static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3594{
3595 if (!nfs4_sequence_done(task, &data->res.seq_res))
3596 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003597 return data->write_done_cb ? data->write_done_cb(task, data) :
3598 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003599}
3600
Trond Myklebust5a37f852012-04-28 14:55:16 -04003601static
3602bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003603{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003604 const struct nfs_pgio_header *hdr = data->header;
3605
3606 /* Don't request attributes for pNFS or O_DIRECT writes */
3607 if (data->ds_clp != NULL || hdr->dreq != NULL)
3608 return false;
3609 /* Otherwise, request attributes if and only if we don't hold
3610 * a delegation
3611 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003612 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003613}
Fred Isamana69aef12011-03-03 15:13:47 +00003614
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003615static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
Fred Isamancd841602012-04-20 14:47:44 -04003617 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003618
Trond Myklebust5a37f852012-04-28 14:55:16 -04003619 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003620 data->args.bitmask = NULL;
3621 data->res.fattr = NULL;
3622 } else
3623 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003624
Fred Isamanb029bc92011-03-03 15:13:42 +00003625 if (!data->write_done_cb)
3626 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003627 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 data->timestamp = jiffies;
3629
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003630 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003631 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
3633
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003634static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3635{
Fred Isamancd841602012-04-20 14:47:44 -04003636 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003637 &data->args.seq_args,
3638 &data->res.seq_res,
3639 task))
3640 return;
3641 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642}
3643
Fred Isaman0b7c0152012-04-20 14:47:39 -04003644static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3645{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3647 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003648 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return;
3651 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003652}
3653
Fred Isaman0b7c0152012-04-20 14:47:39 -04003654static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
3656 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003657
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003658 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003659 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003660 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
Fred Isaman0b7c0152012-04-20 14:47:39 -04003665static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003666{
3667 if (!nfs4_sequence_done(task, &data->res.seq_res))
3668 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003669 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003670}
3671
Fred Isaman0b7c0152012-04-20 14:47:39 -04003672static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003674 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003675
Fred Isaman0b7c0152012-04-20 14:47:39 -04003676 if (data->commit_done_cb == NULL)
3677 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003678 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003679 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003680 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003683struct nfs4_renewdata {
3684 struct nfs_client *client;
3685 unsigned long timestamp;
3686};
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688/*
3689 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3690 * standalone procedure for queueing an asynchronous RENEW.
3691 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003692static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003693{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003694 struct nfs4_renewdata *data = calldata;
3695 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003696
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003697 if (atomic_read(&clp->cl_count) > 1)
3698 nfs4_schedule_state_renewal(clp);
3699 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003700 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003701}
3702
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003703static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003705 struct nfs4_renewdata *data = calldata;
3706 struct nfs_client *clp = data->client;
3707 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003710 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003711 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3712 return;
3713 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003714 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003715 return;
3716 }
3717 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003719 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003722static const struct rpc_call_ops nfs4_renew_ops = {
3723 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003724 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003725};
3726
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003727static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
3729 struct rpc_message msg = {
3730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3731 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003732 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003734 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003736 if (renew_flags == 0)
3737 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003738 if (!atomic_inc_not_zero(&clp->cl_count))
3739 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003740 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003741 if (data == NULL)
3742 return -ENOMEM;
3743 data->client = clp;
3744 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003746 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}
3748
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003749static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
3751 struct rpc_message msg = {
3752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3753 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003754 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 };
3756 unsigned long now = jiffies;
3757 int status;
3758
3759 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3760 if (status < 0)
3761 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003762 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 return 0;
3764}
3765
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003766static inline int nfs4_server_supports_acls(struct nfs_server *server)
3767{
3768 return (server->caps & NFS_CAP_ACLS)
3769 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3770 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3771}
3772
Trond Myklebust21f498c2012-08-24 10:59:25 -04003773/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3774 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003775 * the stack.
3776 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003777#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003778
Neil Hormane9e3d722011-03-04 19:26:03 -05003779static int buf_to_pages_noslab(const void *buf, size_t buflen,
3780 struct page **pages, unsigned int *pgbase)
3781{
3782 struct page *newpage, **spages;
3783 int rc = 0;
3784 size_t len;
3785 spages = pages;
3786
3787 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003788 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003789 newpage = alloc_page(GFP_KERNEL);
3790
3791 if (newpage == NULL)
3792 goto unwind;
3793 memcpy(page_address(newpage), buf, len);
3794 buf += len;
3795 buflen -= len;
3796 *pages++ = newpage;
3797 rc++;
3798 } while (buflen != 0);
3799
3800 return rc;
3801
3802unwind:
3803 for(; rc > 0; rc--)
3804 __free_page(spages[rc-1]);
3805 return -ENOMEM;
3806}
3807
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003808struct nfs4_cached_acl {
3809 int cached;
3810 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003811 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003812};
3813
3814static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003815{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003816 struct nfs_inode *nfsi = NFS_I(inode);
3817
3818 spin_lock(&inode->i_lock);
3819 kfree(nfsi->nfs4_acl);
3820 nfsi->nfs4_acl = acl;
3821 spin_unlock(&inode->i_lock);
3822}
3823
3824static void nfs4_zap_acl_attr(struct inode *inode)
3825{
3826 nfs4_set_cached_acl(inode, NULL);
3827}
3828
3829static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3830{
3831 struct nfs_inode *nfsi = NFS_I(inode);
3832 struct nfs4_cached_acl *acl;
3833 int ret = -ENOENT;
3834
3835 spin_lock(&inode->i_lock);
3836 acl = nfsi->nfs4_acl;
3837 if (acl == NULL)
3838 goto out;
3839 if (buf == NULL) /* user is just asking for length */
3840 goto out_len;
3841 if (acl->cached == 0)
3842 goto out;
3843 ret = -ERANGE; /* see getxattr(2) man page */
3844 if (acl->len > buflen)
3845 goto out;
3846 memcpy(buf, acl->data, acl->len);
3847out_len:
3848 ret = acl->len;
3849out:
3850 spin_unlock(&inode->i_lock);
3851 return ret;
3852}
3853
Sachin Prabhu5794d212012-04-17 14:36:40 +01003854static 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 +00003855{
3856 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003857 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003858
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003859 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003860 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003861 if (acl == NULL)
3862 goto out;
3863 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003864 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003865 } else {
3866 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3867 if (acl == NULL)
3868 goto out;
3869 acl->cached = 0;
3870 }
3871 acl->len = acl_len;
3872out:
3873 nfs4_set_cached_acl(inode, acl);
3874}
3875
Andy Adamsonbf118a32011-12-07 11:55:27 -05003876/*
3877 * The getxattr API returns the required buffer length when called with a
3878 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3879 * the required buf. On a NULL buf, we send a page of data to the server
3880 * guessing that the ACL request can be serviced by a page. If so, we cache
3881 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3882 * the cache. If not so, we throw away the page, and cache the required
3883 * length. The next getxattr call will then produce another round trip to
3884 * the server, this time with the input buf of the required size.
3885 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003886static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003887{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003888 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003889 struct nfs_getaclargs args = {
3890 .fh = NFS_FH(inode),
3891 .acl_pages = pages,
3892 .acl_len = buflen,
3893 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003894 struct nfs_getaclres res = {
3895 .acl_len = buflen,
3896 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003897 struct rpc_message msg = {
3898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3899 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003900 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003901 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003902 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3903 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003904
Andy Adamsonbf118a32011-12-07 11:55:27 -05003905 /* As long as we're doing a round trip to the server anyway,
3906 * let's be prepared for a page of acl data. */
3907 if (npages == 0)
3908 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003909 if (npages > ARRAY_SIZE(pages))
3910 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003911
Andy Adamsonbf118a32011-12-07 11:55:27 -05003912 for (i = 0; i < npages; i++) {
3913 pages[i] = alloc_page(GFP_KERNEL);
3914 if (!pages[i])
3915 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003916 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003917
3918 /* for decoding across pages */
3919 res.acl_scratch = alloc_page(GFP_KERNEL);
3920 if (!res.acl_scratch)
3921 goto out_free;
3922
Andy Adamsonbf118a32011-12-07 11:55:27 -05003923 args.acl_len = npages * PAGE_SIZE;
3924 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003925
Peng Taode040be2012-01-10 22:42:47 +08003926 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003927 __func__, buf, buflen, npages, args.acl_len);
3928 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3929 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003930 if (ret)
3931 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003932
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003933 /* Handle the case where the passed-in buffer is too short */
3934 if (res.acl_flags & NFS4_ACL_TRUNC) {
3935 /* Did the user only issue a request for the acl length? */
3936 if (buf == NULL)
3937 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003938 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003939 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003940 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003941 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3942 if (buf)
3943 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3944out_ok:
3945 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003946out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003947 for (i = 0; i < npages; i++)
3948 if (pages[i])
3949 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003950 if (res.acl_scratch)
3951 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003952 return ret;
3953}
3954
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003955static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3956{
3957 struct nfs4_exception exception = { };
3958 ssize_t ret;
3959 do {
3960 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3961 if (ret >= 0)
3962 break;
3963 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3964 } while (exception.retry);
3965 return ret;
3966}
3967
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003968static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3969{
3970 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003971 int ret;
3972
3973 if (!nfs4_server_supports_acls(server))
3974 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003975 ret = nfs_revalidate_inode(server, inode);
3976 if (ret < 0)
3977 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003978 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3979 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003980 ret = nfs4_read_cached_acl(inode, buf, buflen);
3981 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003982 /* -ENOENT is returned if there is no ACL or if there is an ACL
3983 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003984 return ret;
3985 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003986}
3987
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003988static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003989{
3990 struct nfs_server *server = NFS_SERVER(inode);
3991 struct page *pages[NFS4ACL_MAXPAGES];
3992 struct nfs_setaclargs arg = {
3993 .fh = NFS_FH(inode),
3994 .acl_pages = pages,
3995 .acl_len = buflen,
3996 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003997 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003998 struct rpc_message msg = {
3999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4000 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004001 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004002 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004003 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004004 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004005
4006 if (!nfs4_server_supports_acls(server))
4007 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004008 if (npages > ARRAY_SIZE(pages))
4009 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004010 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4011 if (i < 0)
4012 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004013 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004014 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004015
4016 /*
4017 * Free each page after tx, so the only ref left is
4018 * held by the network stack
4019 */
4020 for (; i > 0; i--)
4021 put_page(pages[i-1]);
4022
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004023 /*
4024 * Acl update can result in inode attribute update.
4025 * so mark the attribute cache invalid.
4026 */
4027 spin_lock(&inode->i_lock);
4028 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4029 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004030 nfs_access_zap_cache(inode);
4031 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004032 return ret;
4033}
4034
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004035static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4036{
4037 struct nfs4_exception exception = { };
4038 int err;
4039 do {
4040 err = nfs4_handle_exception(NFS_SERVER(inode),
4041 __nfs4_proc_set_acl(inode, buf, buflen),
4042 &exception);
4043 } while (exception.retry);
4044 return err;
4045}
4046
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004048nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004050 struct nfs_client *clp = server->nfs_client;
4051
4052 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 return 0;
4054 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004055 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004056 case -NFS4ERR_ADMIN_REVOKED:
4057 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004058 if (state == NULL)
4059 break;
4060 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004061 case -NFS4ERR_OPENMODE:
4062 if (state == NULL)
4063 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004064 nfs4_schedule_stateid_recovery(server, state);
4065 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004066 case -NFS4ERR_EXPIRED:
4067 if (state != NULL)
4068 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004070 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004071 nfs4_schedule_lease_recovery(clp);
4072 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004073#if defined(CONFIG_NFS_V4_1)
4074 case -NFS4ERR_BADSESSION:
4075 case -NFS4ERR_BADSLOT:
4076 case -NFS4ERR_BAD_HIGH_SLOT:
4077 case -NFS4ERR_DEADSESSION:
4078 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4079 case -NFS4ERR_SEQ_FALSE_RETRY:
4080 case -NFS4ERR_SEQ_MISORDERED:
4081 dprintk("%s ERROR %d, Reset session\n", __func__,
4082 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004083 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004084 task->tk_status = 0;
4085 return -EAGAIN;
4086#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004088 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004089 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004090 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4092 task->tk_status = 0;
4093 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004094 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 case -NFS4ERR_OLD_STATEID:
4096 task->tk_status = 0;
4097 return -EAGAIN;
4098 }
4099 task->tk_status = nfs4_map_errors(task->tk_status);
4100 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004101wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004102 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004103 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4104 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4105 task->tk_status = 0;
4106 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107}
4108
Chuck Leverf0920752012-05-21 22:45:41 -04004109static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4110 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004111{
4112 __be32 verf[2];
4113
Chuck Lever2c820d92012-05-21 22:45:33 -04004114 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4115 /* An impossible timestamp guarantees this value
4116 * will never match a generated boot time. */
4117 verf[0] = 0;
4118 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4119 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004120 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4121 verf[0] = (__be32)nn->boot_time.tv_sec;
4122 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004123 }
Chuck Levercd937102012-03-02 17:14:31 -05004124 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4125}
4126
Chuck Levere984a552012-09-14 17:24:21 -04004127static unsigned int
4128nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4129 char *buf, size_t len)
4130{
4131 unsigned int result;
4132
4133 rcu_read_lock();
4134 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4135 clp->cl_ipaddr,
4136 rpc_peeraddr2str(clp->cl_rpcclient,
4137 RPC_DISPLAY_ADDR),
4138 rpc_peeraddr2str(clp->cl_rpcclient,
4139 RPC_DISPLAY_PROTO));
4140 rcu_read_unlock();
4141 return result;
4142}
4143
4144static unsigned int
4145nfs4_init_uniform_client_string(const struct nfs_client *clp,
4146 char *buf, size_t len)
4147{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004148 char *nodename = clp->cl_rpcclient->cl_nodename;
4149
4150 if (nfs4_client_id_uniquifier[0] != '\0')
4151 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004152 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4153 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004154 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004155}
4156
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004157/**
4158 * nfs4_proc_setclientid - Negotiate client ID
4159 * @clp: state data structure
4160 * @program: RPC program for NFSv4 callback service
4161 * @port: IP port number for NFS4 callback service
4162 * @cred: RPC credential to use for this call
4163 * @res: where to place the result
4164 *
4165 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4166 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004167int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4168 unsigned short port, struct rpc_cred *cred,
4169 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170{
4171 nfs4_verifier sc_verifier;
4172 struct nfs4_setclientid setclientid = {
4173 .sc_verifier = &sc_verifier,
4174 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004175 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 };
4177 struct rpc_message msg = {
4178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4179 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004180 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004181 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004183 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Chuck Leverde734832012-07-11 16:30:50 -04004185 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004186 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004187 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4188 setclientid.sc_name_len =
4189 nfs4_init_uniform_client_string(clp,
4190 setclientid.sc_name,
4191 sizeof(setclientid.sc_name));
4192 else
4193 setclientid.sc_name_len =
4194 nfs4_init_nonuniform_client_string(clp,
4195 setclientid.sc_name,
4196 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004197 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004198 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004199 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004200 sizeof(setclientid.sc_netid),
4201 rpc_peeraddr2str(clp->cl_rpcclient,
4202 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004203 rcu_read_unlock();
4204 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004205 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 clp->cl_ipaddr, port >> 8, port & 255);
4207
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004208 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4209 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4210 setclientid.sc_name_len, setclientid.sc_name);
4211 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4212 dprintk("NFS reply setclientid: %d\n", status);
4213 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214}
4215
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004216/**
4217 * nfs4_proc_setclientid_confirm - Confirm client ID
4218 * @clp: state data structure
4219 * @res: result of a previous SETCLIENTID
4220 * @cred: RPC credential to use for this call
4221 *
4222 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4223 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004224int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004225 struct nfs4_setclientid_res *arg,
4226 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227{
4228 struct nfs_fsinfo fsinfo;
4229 struct rpc_message msg = {
4230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004231 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004233 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 };
4235 unsigned long now;
4236 int status;
4237
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004238 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4239 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4240 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004242 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 if (status == 0) {
4244 spin_lock(&clp->cl_lock);
4245 clp->cl_lease_time = fsinfo.lease_time * HZ;
4246 clp->cl_last_renewal = now;
4247 spin_unlock(&clp->cl_lock);
4248 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004249 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return status;
4251}
4252
Trond Myklebustfe650402006-01-03 09:55:18 +01004253struct nfs4_delegreturndata {
4254 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004255 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004256 struct nfs_fh fh;
4257 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004258 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004259 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004260 int rpc_status;
4261};
4262
Trond Myklebustfe650402006-01-03 09:55:18 +01004263static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4264{
4265 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004266
Trond Myklebust14516c32010-07-31 14:29:06 -04004267 if (!nfs4_sequence_done(task, &data->res.seq_res))
4268 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004269
Ricardo Labiaga79708862009-12-07 09:23:21 -05004270 switch (task->tk_status) {
4271 case -NFS4ERR_STALE_STATEID:
4272 case -NFS4ERR_EXPIRED:
4273 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004274 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004275 break;
4276 default:
4277 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4278 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004279 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004280 return;
4281 }
4282 }
4283 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004284}
4285
4286static void nfs4_delegreturn_release(void *calldata)
4287{
Trond Myklebustfe650402006-01-03 09:55:18 +01004288 kfree(calldata);
4289}
4290
Andy Adamson938e1012009-04-01 09:22:28 -04004291#if defined(CONFIG_NFS_V4_1)
4292static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4293{
4294 struct nfs4_delegreturndata *d_data;
4295
4296 d_data = (struct nfs4_delegreturndata *)data;
4297
Trond Myklebust035168a2010-06-16 09:52:26 -04004298 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004299 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004300 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004301 return;
4302 rpc_call_start(task);
4303}
4304#endif /* CONFIG_NFS_V4_1 */
4305
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004306static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004307#if defined(CONFIG_NFS_V4_1)
4308 .rpc_call_prepare = nfs4_delegreturn_prepare,
4309#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004310 .rpc_call_done = nfs4_delegreturn_done,
4311 .rpc_release = nfs4_delegreturn_release,
4312};
4313
Trond Myklebuste6f81072008-01-24 18:14:34 -05004314static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004315{
4316 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004317 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004318 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004319 struct rpc_message msg = {
4320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4321 .rpc_cred = cred,
4322 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004323 struct rpc_task_setup task_setup_data = {
4324 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004325 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004326 .callback_ops = &nfs4_delegreturn_ops,
4327 .flags = RPC_TASK_ASYNC,
4328 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004329 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004330
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004331 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004332 if (data == NULL)
4333 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004334 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004335 data->args.fhandle = &data->fh;
4336 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004337 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004338 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004339 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004340 data->res.fattr = &data->fattr;
4341 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004342 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004343 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004344 data->rpc_status = 0;
4345
Trond Myklebustc970aa82007-07-14 15:39:59 -04004346 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004347 msg.rpc_argp = &data->args;
4348 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004349 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004350 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004351 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004352 if (!issync)
4353 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004354 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004355 if (status != 0)
4356 goto out;
4357 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004358 if (status == 0)
4359 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4360 else
4361 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004362out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004363 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004364 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365}
4366
Trond Myklebuste6f81072008-01-24 18:14:34 -05004367int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
4369 struct nfs_server *server = NFS_SERVER(inode);
4370 struct nfs4_exception exception = { };
4371 int err;
4372 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004373 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 switch (err) {
4375 case -NFS4ERR_STALE_STATEID:
4376 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 case 0:
4378 return 0;
4379 }
4380 err = nfs4_handle_exception(server, err, &exception);
4381 } while (exception.retry);
4382 return err;
4383}
4384
4385#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4386#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4387
4388/*
4389 * sleep, with exponential backoff, and retry the LOCK operation.
4390 */
4391static unsigned long
4392nfs4_set_lock_task_retry(unsigned long timeout)
4393{
Jeff Laytond3103102011-12-01 22:44:39 +01004394 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 timeout <<= 1;
4396 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4397 return NFS4_LOCK_MAXTIMEOUT;
4398 return timeout;
4399}
4400
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4402{
4403 struct inode *inode = state->inode;
4404 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004405 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004406 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004408 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004410 struct nfs_lockt_res res = {
4411 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 };
4413 struct rpc_message msg = {
4414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4415 .rpc_argp = &arg,
4416 .rpc_resp = &res,
4417 .rpc_cred = state->owner->so_cred,
4418 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 struct nfs4_lock_state *lsp;
4420 int status;
4421
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004422 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004423 status = nfs4_set_lock_state(state, request);
4424 if (status != 0)
4425 goto out;
4426 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004427 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004428 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004429 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004430 switch (status) {
4431 case 0:
4432 request->fl_type = F_UNLCK;
4433 break;
4434 case -NFS4ERR_DENIED:
4435 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004437 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004438out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 return status;
4440}
4441
4442static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4443{
4444 struct nfs4_exception exception = { };
4445 int err;
4446
4447 do {
4448 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4449 _nfs4_proc_getlk(state, cmd, request),
4450 &exception);
4451 } while (exception.retry);
4452 return err;
4453}
4454
4455static int do_vfs_lock(struct file *file, struct file_lock *fl)
4456{
4457 int res = 0;
4458 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4459 case FL_POSIX:
4460 res = posix_lock_file_wait(file, fl);
4461 break;
4462 case FL_FLOCK:
4463 res = flock_lock_file_wait(file, fl);
4464 break;
4465 default:
4466 BUG();
4467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 return res;
4469}
4470
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004471struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004472 struct nfs_locku_args arg;
4473 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004474 struct nfs4_lock_state *lsp;
4475 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004476 struct file_lock fl;
4477 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004478 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004479};
4480
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004481static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4482 struct nfs_open_context *ctx,
4483 struct nfs4_lock_state *lsp,
4484 struct nfs_seqid *seqid)
4485{
4486 struct nfs4_unlockdata *p;
4487 struct inode *inode = lsp->ls_state->inode;
4488
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004489 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004490 if (p == NULL)
4491 return NULL;
4492 p->arg.fh = NFS_FH(inode);
4493 p->arg.fl = &p->fl;
4494 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004495 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004496 p->arg.stateid = &lsp->ls_stateid;
4497 p->lsp = lsp;
4498 atomic_inc(&lsp->ls_count);
4499 /* Ensure we don't close file until we're done freeing locks! */
4500 p->ctx = get_nfs_open_context(ctx);
4501 memcpy(&p->fl, fl, sizeof(p->fl));
4502 p->server = NFS_SERVER(inode);
4503 return p;
4504}
4505
Trond Myklebust06f814a2006-01-03 09:55:07 +01004506static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004507{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004508 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004509 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004510 nfs4_put_lock_state(calldata->lsp);
4511 put_nfs_open_context(calldata->ctx);
4512 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004513}
4514
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004515static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004516{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004517 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004518
Trond Myklebust14516c32010-07-31 14:29:06 -04004519 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4520 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004521 switch (task->tk_status) {
4522 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004523 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4524 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004525 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004526 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004527 case -NFS4ERR_BAD_STATEID:
4528 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004529 case -NFS4ERR_STALE_STATEID:
4530 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004531 break;
4532 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004533 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004534 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004535 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004536 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004537}
4538
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004539static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004540{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004541 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004543 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004544 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004545 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004546 /* Note: exit _without_ running nfs4_locku_done */
4547 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004548 return;
4549 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004550 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004551 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004552 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004553 &calldata->res.seq_res,
4554 task) != 0)
4555 nfs_release_seqid(calldata->arg.seqid);
4556 else
4557 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558}
4559
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004560static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004561 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004562 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004563 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004564};
4565
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004566static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4567 struct nfs_open_context *ctx,
4568 struct nfs4_lock_state *lsp,
4569 struct nfs_seqid *seqid)
4570{
4571 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004572 struct rpc_message msg = {
4573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4574 .rpc_cred = ctx->cred,
4575 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004576 struct rpc_task_setup task_setup_data = {
4577 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004578 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004579 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004580 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004581 .flags = RPC_TASK_ASYNC,
4582 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004583
Frank Filz137d6ac2007-07-09 15:32:29 -07004584 /* Ensure this is an unlock - when canceling a lock, the
4585 * canceled lock is passed in, and it won't be an unlock.
4586 */
4587 fl->fl_type = F_UNLCK;
4588
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004589 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4590 if (data == NULL) {
4591 nfs_free_seqid(seqid);
4592 return ERR_PTR(-ENOMEM);
4593 }
4594
Trond Myklebust9d12b212012-01-17 22:04:25 -05004595 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004596 msg.rpc_argp = &data->arg;
4597 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004598 task_setup_data.callback_data = data;
4599 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004600}
4601
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4603{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004604 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004605 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004606 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004607 struct rpc_task *task;
4608 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004609 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
Trond Myklebust9b073572006-06-29 16:38:34 -04004611 status = nfs4_set_lock_state(state, request);
4612 /* Unlock _before_ we do the RPC call */
4613 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004614 down_read(&nfsi->rwsem);
4615 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4616 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004617 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004618 }
4619 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004620 if (status != 0)
4621 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004622 /* Is this a delegated lock? */
4623 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004624 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004625 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004626 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004627 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004628 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004629 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004630 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004631 status = PTR_ERR(task);
4632 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004633 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004634 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004635 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004636out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004637 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004638 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639}
4640
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004641struct nfs4_lockdata {
4642 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004643 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004644 struct nfs4_lock_state *lsp;
4645 struct nfs_open_context *ctx;
4646 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004647 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004648 int rpc_status;
4649 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004650 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004651};
4652
4653static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004654 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4655 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004656{
4657 struct nfs4_lockdata *p;
4658 struct inode *inode = lsp->ls_state->inode;
4659 struct nfs_server *server = NFS_SERVER(inode);
4660
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004661 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004662 if (p == NULL)
4663 return NULL;
4664
4665 p->arg.fh = NFS_FH(inode);
4666 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004667 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004668 if (p->arg.open_seqid == NULL)
4669 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004670 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004671 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004672 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004673 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004674 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004675 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004676 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004677 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004678 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004679 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004680 atomic_inc(&lsp->ls_count);
4681 p->ctx = get_nfs_open_context(ctx);
4682 memcpy(&p->fl, fl, sizeof(p->fl));
4683 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004684out_free_seqid:
4685 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004686out_free:
4687 kfree(p);
4688 return NULL;
4689}
4690
4691static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4692{
4693 struct nfs4_lockdata *data = calldata;
4694 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Harvey Harrison3110ff82008-05-02 13:42:44 -07004696 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004697 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4698 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004699 /* Do we need to do an open_to_lock_owner? */
4700 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004701 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004702 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004703 data->arg.open_stateid = &state->stateid;
4704 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004705 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004706 } else
4707 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004708 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004709 if (nfs4_setup_sequence(data->server,
4710 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004711 &data->res.seq_res,
4712 task) == 0) {
4713 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004714 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004715 }
4716 nfs_release_seqid(data->arg.open_seqid);
4717out_release_lock_seqid:
4718 nfs_release_seqid(data->arg.lock_seqid);
4719 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004720}
4721
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004722static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4723{
4724 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4725 nfs4_lock_prepare(task, calldata);
4726}
4727
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004728static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4729{
4730 struct nfs4_lockdata *data = calldata;
4731
Harvey Harrison3110ff82008-05-02 13:42:44 -07004732 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004733
Trond Myklebust14516c32010-07-31 14:29:06 -04004734 if (!nfs4_sequence_done(task, &data->res.seq_res))
4735 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004736
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004737 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004738 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004739 if (data->rpc_status == 0)
4740 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4741 else
4742 goto out;
4743 }
4744 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004745 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004746 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004747 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004748 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004749out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004750 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004751}
4752
4753static void nfs4_lock_release(void *calldata)
4754{
4755 struct nfs4_lockdata *data = calldata;
4756
Harvey Harrison3110ff82008-05-02 13:42:44 -07004757 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004758 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004759 if (data->cancelled != 0) {
4760 struct rpc_task *task;
4761 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4762 data->arg.lock_seqid);
4763 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004764 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004765 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004766 } else
4767 nfs_free_seqid(data->arg.lock_seqid);
4768 nfs4_put_lock_state(data->lsp);
4769 put_nfs_open_context(data->ctx);
4770 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004771 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004772}
4773
4774static const struct rpc_call_ops nfs4_lock_ops = {
4775 .rpc_call_prepare = nfs4_lock_prepare,
4776 .rpc_call_done = nfs4_lock_done,
4777 .rpc_release = nfs4_lock_release,
4778};
4779
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004780static const struct rpc_call_ops nfs4_recover_lock_ops = {
4781 .rpc_call_prepare = nfs4_recover_lock_prepare,
4782 .rpc_call_done = nfs4_lock_done,
4783 .rpc_release = nfs4_lock_release,
4784};
4785
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004786static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4787{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004788 switch (error) {
4789 case -NFS4ERR_ADMIN_REVOKED:
4790 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004791 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004792 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004793 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004794 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004795 break;
4796 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004797 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004798 case -NFS4ERR_EXPIRED:
4799 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004800 };
4801}
4802
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004803static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004804{
4805 struct nfs4_lockdata *data;
4806 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004807 struct rpc_message msg = {
4808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4809 .rpc_cred = state->owner->so_cred,
4810 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004811 struct rpc_task_setup task_setup_data = {
4812 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004813 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004814 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004815 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004816 .flags = RPC_TASK_ASYNC,
4817 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004818 int ret;
4819
Harvey Harrison3110ff82008-05-02 13:42:44 -07004820 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004821 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004822 fl->fl_u.nfs4_fl.owner,
4823 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004824 if (data == NULL)
4825 return -ENOMEM;
4826 if (IS_SETLKW(cmd))
4827 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004828 if (recovery_type > NFS_LOCK_NEW) {
4829 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004830 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004831 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4832 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004833 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004834 msg.rpc_argp = &data->arg;
4835 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004836 task_setup_data.callback_data = data;
4837 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004838 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004839 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004840 ret = nfs4_wait_for_completion_rpc_task(task);
4841 if (ret == 0) {
4842 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004843 if (ret)
4844 nfs4_handle_setlk_error(data->server, data->lsp,
4845 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004846 } else
4847 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004848 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004849 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004850 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851}
4852
4853static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4854{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004855 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004856 struct nfs4_exception exception = {
4857 .inode = state->inode,
4858 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004859 int err;
4860
4861 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004862 /* Cache the lock if possible... */
4863 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4864 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004865 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004866 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004867 break;
4868 nfs4_handle_exception(server, err, &exception);
4869 } while (exception.retry);
4870 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871}
4872
4873static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4874{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004875 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004876 struct nfs4_exception exception = {
4877 .inode = state->inode,
4878 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004879 int err;
4880
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004881 err = nfs4_set_lock_state(state, request);
4882 if (err != 0)
4883 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004884 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004885 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4886 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004887 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004888 switch (err) {
4889 default:
4890 goto out;
4891 case -NFS4ERR_GRACE:
4892 case -NFS4ERR_DELAY:
4893 nfs4_handle_exception(server, err, &exception);
4894 err = 0;
4895 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004896 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004897out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004898 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899}
4900
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004901#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004902/**
4903 * nfs41_check_expired_locks - possibly free a lock stateid
4904 *
4905 * @state: NFSv4 state for an inode
4906 *
4907 * Returns NFS_OK if recovery for this stateid is now finished.
4908 * Otherwise a negative NFS4ERR value is returned.
4909 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004910static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004911{
Chuck Levereb64cf92012-07-11 16:30:05 -04004912 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004913 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004914 struct nfs_server *server = NFS_SERVER(state->inode);
4915
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004916 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004917 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004918 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4919 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004920 /* Free the stateid unless the server
4921 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004922 if (status != -NFS4ERR_BAD_STATEID)
4923 nfs41_free_stateid(server,
4924 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004925 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004926 ret = status;
4927 }
4928 }
4929 };
4930
4931 return ret;
4932}
4933
4934static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4935{
4936 int status = NFS_OK;
4937
4938 if (test_bit(LK_STATE_IN_USE, &state->flags))
4939 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004940 if (status != NFS_OK)
4941 status = nfs4_lock_expired(state, request);
4942 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004943}
4944#endif
4945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4947{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004948 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004949 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004950 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004952 if ((fl_flags & FL_POSIX) &&
4953 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4954 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004955 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004956 status = nfs4_set_lock_state(state, request);
4957 if (status != 0)
4958 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004959 request->fl_flags |= FL_ACCESS;
4960 status = do_vfs_lock(request->fl_file, request);
4961 if (status < 0)
4962 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004963 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004964 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004965 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004966 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004967 request->fl_flags = fl_flags & ~FL_SLEEP;
4968 status = do_vfs_lock(request->fl_file, request);
4969 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004970 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004971 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004972 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004973 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004974 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004975 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004976 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004977 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4978 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004979out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004980 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004981out:
4982 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 return status;
4984}
4985
4986static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4987{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004988 struct nfs4_exception exception = {
4989 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004990 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004991 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 int err;
4993
4994 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004995 err = _nfs4_proc_setlk(state, cmd, request);
4996 if (err == -NFS4ERR_DENIED)
4997 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004999 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 } while (exception.retry);
5001 return err;
5002}
5003
5004static int
5005nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5006{
5007 struct nfs_open_context *ctx;
5008 struct nfs4_state *state;
5009 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5010 int status;
5011
5012 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005013 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 state = ctx->state;
5015
5016 if (request->fl_start < 0 || request->fl_end < 0)
5017 return -EINVAL;
5018
Trond Myklebustd9531262009-07-21 19:22:38 -04005019 if (IS_GETLK(cmd)) {
5020 if (state != NULL)
5021 return nfs4_proc_getlk(state, F_GETLK, request);
5022 return 0;
5023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
5025 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5026 return -EINVAL;
5027
Trond Myklebustd9531262009-07-21 19:22:38 -04005028 if (request->fl_type == F_UNLCK) {
5029 if (state != NULL)
5030 return nfs4_proc_unlck(state, cmd, request);
5031 return 0;
5032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
Trond Myklebustd9531262009-07-21 19:22:38 -04005034 if (state == NULL)
5035 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005036 /*
5037 * Don't rely on the VFS having checked the file open mode,
5038 * since it won't do this for flock() locks.
5039 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005040 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005041 case F_RDLCK:
5042 if (!(filp->f_mode & FMODE_READ))
5043 return -EBADF;
5044 break;
5045 case F_WRLCK:
5046 if (!(filp->f_mode & FMODE_WRITE))
5047 return -EBADF;
5048 }
5049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 do {
5051 status = nfs4_proc_setlk(state, cmd, request);
5052 if ((status != -EAGAIN) || IS_SETLK(cmd))
5053 break;
5054 timeout = nfs4_set_lock_task_retry(timeout);
5055 status = -ERESTARTSYS;
5056 if (signalled())
5057 break;
5058 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 return status;
5060}
5061
Trond Myklebust888e6942005-11-04 15:38:11 -05005062int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5063{
5064 struct nfs_server *server = NFS_SERVER(state->inode);
5065 struct nfs4_exception exception = { };
5066 int err;
5067
5068 err = nfs4_set_lock_state(state, fl);
5069 if (err != 0)
5070 goto out;
5071 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005072 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005073 switch (err) {
5074 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005075 printk(KERN_ERR "NFS: %s: unhandled error "
5076 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005077 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005078 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005079 goto out;
5080 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005081 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005082 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005083 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005084 nfs4_schedule_lease_recovery(server->nfs_client);
5085 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005086 case -NFS4ERR_BADSESSION:
5087 case -NFS4ERR_BADSLOT:
5088 case -NFS4ERR_BAD_HIGH_SLOT:
5089 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5090 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005091 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005092 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005093 case -ERESTARTSYS:
5094 /*
5095 * The show must go on: exit, but mark the
5096 * stateid as needing recovery.
5097 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005098 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005099 case -NFS4ERR_ADMIN_REVOKED:
5100 case -NFS4ERR_BAD_STATEID:
5101 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005102 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005103 err = 0;
5104 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005105 case -EKEYEXPIRED:
5106 /*
5107 * User RPCSEC_GSS context has expired.
5108 * We cannot recover this stateid now, so
5109 * skip it and allow recovery thread to
5110 * proceed.
5111 */
5112 err = 0;
5113 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005114 case -ENOMEM:
5115 case -NFS4ERR_DENIED:
5116 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5117 err = 0;
5118 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005119 case -NFS4ERR_DELAY:
5120 break;
5121 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005122 err = nfs4_handle_exception(server, err, &exception);
5123 } while (exception.retry);
5124out:
5125 return err;
5126}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005127
Trond Myklebustcf470c32012-03-07 13:49:12 -05005128struct nfs_release_lockowner_data {
5129 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005130 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005131 struct nfs_release_lockowner_args args;
5132};
5133
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005134static void nfs4_release_lockowner_release(void *calldata)
5135{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005136 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005137 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005138 kfree(calldata);
5139}
5140
Trond Myklebust17280172012-03-11 13:11:00 -04005141static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005142 .rpc_release = nfs4_release_lockowner_release,
5143};
5144
Trond Myklebustcf470c32012-03-07 13:49:12 -05005145int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005146{
5147 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005148 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005149 struct rpc_message msg = {
5150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5151 };
5152
5153 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005154 return -EINVAL;
5155 data = kmalloc(sizeof(*data), GFP_NOFS);
5156 if (!data)
5157 return -ENOMEM;
5158 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005159 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005160 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5161 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5162 data->args.lock_owner.s_dev = server->s_dev;
5163 msg.rpc_argp = &data->args;
5164 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5165 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005166}
5167
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005168#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5169
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005170static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5171 const void *buf, size_t buflen,
5172 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005173{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005174 if (strcmp(key, "") != 0)
5175 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005176
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005177 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005178}
5179
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005180static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5181 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005182{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005183 if (strcmp(key, "") != 0)
5184 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005185
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005186 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005187}
5188
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005189static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5190 size_t list_len, const char *name,
5191 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005192{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005193 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005194
J. Bruce Fields096455a2006-03-20 23:23:42 -05005195 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5196 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005197
5198 if (list && len <= list_len)
5199 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005200 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005201}
5202
Andy Adamson533eb462011-06-13 18:25:56 -04005203/*
5204 * nfs_fhget will use either the mounted_on_fileid or the fileid
5205 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005206static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5207{
Andy Adamson533eb462011-06-13 18:25:56 -04005208 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5209 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5210 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005211 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005212 return;
5213
5214 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005215 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005216 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5217 fattr->nlink = 2;
5218}
5219
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005220static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5221 const struct qstr *name,
5222 struct nfs4_fs_locations *fs_locations,
5223 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005224{
5225 struct nfs_server *server = NFS_SERVER(dir);
5226 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005227 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005228 };
5229 struct nfs4_fs_locations_arg args = {
5230 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005231 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005232 .page = page,
5233 .bitmask = bitmask,
5234 };
Benny Halevy22958462009-04-01 09:22:02 -04005235 struct nfs4_fs_locations_res res = {
5236 .fs_locations = fs_locations,
5237 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005238 struct rpc_message msg = {
5239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5240 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005241 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005242 };
5243 int status;
5244
Harvey Harrison3110ff82008-05-02 13:42:44 -07005245 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005246
5247 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5248 * is not supported */
5249 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5250 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5251 else
5252 bitmask[0] |= FATTR4_WORD0_FILEID;
5253
Trond Myklebustc228fd32007-01-13 02:28:11 -05005254 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005255 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005256 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005257 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005258 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005259 return status;
5260}
5261
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005262int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5263 const struct qstr *name,
5264 struct nfs4_fs_locations *fs_locations,
5265 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005266{
5267 struct nfs4_exception exception = { };
5268 int err;
5269 do {
5270 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005271 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005272 &exception);
5273 } while (exception.retry);
5274 return err;
5275}
5276
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005277static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5278{
5279 int status;
5280 struct nfs4_secinfo_arg args = {
5281 .dir_fh = NFS_FH(dir),
5282 .name = name,
5283 };
5284 struct nfs4_secinfo_res res = {
5285 .flavors = flavors,
5286 };
5287 struct rpc_message msg = {
5288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5289 .rpc_argp = &args,
5290 .rpc_resp = &res,
5291 };
5292
5293 dprintk("NFS call secinfo %s\n", name->name);
5294 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5295 dprintk("NFS reply secinfo: %d\n", status);
5296 return status;
5297}
5298
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005299int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5300 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005301{
5302 struct nfs4_exception exception = { };
5303 int err;
5304 do {
5305 err = nfs4_handle_exception(NFS_SERVER(dir),
5306 _nfs4_proc_secinfo(dir, name, flavors),
5307 &exception);
5308 } while (exception.retry);
5309 return err;
5310}
5311
Andy Adamson557134a2009-04-01 09:21:53 -04005312#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005313/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005314 * Check the exchange flags returned by the server for invalid flags, having
5315 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5316 * DS flags set.
5317 */
5318static int nfs4_check_cl_exchange_flags(u32 flags)
5319{
5320 if (flags & ~EXCHGID4_FLAG_MASK_R)
5321 goto out_inval;
5322 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5323 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5324 goto out_inval;
5325 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5326 goto out_inval;
5327 return NFS_OK;
5328out_inval:
5329 return -NFS4ERR_INVAL;
5330}
5331
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005332static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005333nfs41_same_server_scope(struct nfs41_server_scope *a,
5334 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005335{
5336 if (a->server_scope_sz == b->server_scope_sz &&
5337 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5338 return true;
5339
5340 return false;
5341}
5342
Andy Adamson357f54d2010-12-14 10:11:57 -05005343/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005344 * nfs4_proc_bind_conn_to_session()
5345 *
5346 * The 4.1 client currently uses the same TCP connection for the
5347 * fore and backchannel.
5348 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005349int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005350{
5351 int status;
5352 struct nfs41_bind_conn_to_session_res res;
5353 struct rpc_message msg = {
5354 .rpc_proc =
5355 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5356 .rpc_argp = clp,
5357 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005358 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005359 };
5360
5361 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005362
5363 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5364 if (unlikely(res.session == NULL)) {
5365 status = -ENOMEM;
5366 goto out;
5367 }
5368
5369 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5370 if (status == 0) {
5371 if (memcmp(res.session->sess_id.data,
5372 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5373 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5374 status = -EIO;
5375 goto out_session;
5376 }
5377 if (res.dir != NFS4_CDFS4_BOTH) {
5378 dprintk("NFS: %s: Unexpected direction from server\n",
5379 __func__);
5380 status = -EIO;
5381 goto out_session;
5382 }
5383 if (res.use_conn_in_rdma_mode) {
5384 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5385 __func__);
5386 status = -EIO;
5387 goto out_session;
5388 }
5389 }
5390out_session:
5391 kfree(res.session);
5392out:
5393 dprintk("<-- %s status= %d\n", __func__, status);
5394 return status;
5395}
5396
5397/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005398 * nfs4_proc_exchange_id()
5399 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005400 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5401 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005402 * Since the clientid has expired, all compounds using sessions
5403 * associated with the stale clientid will be returning
5404 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5405 * be in some phase of session reset.
5406 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005407int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005408{
5409 nfs4_verifier verifier;
5410 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005411 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005412 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005413 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005414 };
5415 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005416 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005417 };
5418 int status;
5419 struct rpc_message msg = {
5420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5421 .rpc_argp = &args,
5422 .rpc_resp = &res,
5423 .rpc_cred = cred,
5424 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005425
Chuck Leverf0920752012-05-21 22:45:41 -04005426 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005427 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5428 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005429 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5430 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5431 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005432
Chuck Leveracdeb692012-05-21 22:46:16 -04005433 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005434 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005435 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005436 status = -ENOMEM;
5437 goto out;
5438 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005439
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005440 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005441 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005442 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005443 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005444 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005445 }
5446
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005447 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005448 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005449 status = -ENOMEM;
5450 goto out_server_scope;
5451 }
5452
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005453 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005454 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005455 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005456
Chuck Lever177313f2012-05-21 22:44:58 -04005457 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005458 clp->cl_clientid = res.clientid;
5459 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5460 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5461 clp->cl_seqid = res.seqid;
5462
Chuck Leveracdeb692012-05-21 22:46:16 -04005463 kfree(clp->cl_serverowner);
5464 clp->cl_serverowner = res.server_owner;
5465 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005466
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005467 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005468 kfree(clp->cl_implid);
5469 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005470
Chuck Lever177313f2012-05-21 22:44:58 -04005471 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005472 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005473 res.server_scope)) {
5474 dprintk("%s: server_scope mismatch detected\n",
5475 __func__);
5476 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005477 kfree(clp->cl_serverscope);
5478 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005479 }
5480
Chuck Lever177313f2012-05-21 22:44:58 -04005481 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005482 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005483 goto out;
5484 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005485 } else
5486 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005487
Chuck Leveracdeb692012-05-21 22:46:16 -04005488out_server_owner:
5489 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005490out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005491 kfree(res.server_scope);
5492out:
Chuck Lever177313f2012-05-21 22:44:58 -04005493 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005494 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005495 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005496 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005497 clp->cl_implid->date.seconds,
5498 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005499 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005500 return status;
5501}
5502
Trond Myklebust66245532012-05-25 17:18:09 -04005503static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5504 struct rpc_cred *cred)
5505{
5506 struct rpc_message msg = {
5507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5508 .rpc_argp = clp,
5509 .rpc_cred = cred,
5510 };
5511 int status;
5512
5513 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5514 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005515 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005516 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5517 return status;
5518}
5519
5520static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5521 struct rpc_cred *cred)
5522{
5523 unsigned int loop;
5524 int ret;
5525
5526 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5527 ret = _nfs4_proc_destroy_clientid(clp, cred);
5528 switch (ret) {
5529 case -NFS4ERR_DELAY:
5530 case -NFS4ERR_CLIENTID_BUSY:
5531 ssleep(1);
5532 break;
5533 default:
5534 return ret;
5535 }
5536 }
5537 return 0;
5538}
5539
5540int nfs4_destroy_clientid(struct nfs_client *clp)
5541{
5542 struct rpc_cred *cred;
5543 int ret = 0;
5544
5545 if (clp->cl_mvops->minor_version < 1)
5546 goto out;
5547 if (clp->cl_exchange_flags == 0)
5548 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005549 if (clp->cl_preserve_clid)
5550 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005551 cred = nfs4_get_exchange_id_cred(clp);
5552 ret = nfs4_proc_destroy_clientid(clp, cred);
5553 if (cred)
5554 put_rpccred(cred);
5555 switch (ret) {
5556 case 0:
5557 case -NFS4ERR_STALE_CLIENTID:
5558 clp->cl_exchange_flags = 0;
5559 }
5560out:
5561 return ret;
5562}
5563
Andy Adamson2050f0c2009-04-01 09:22:30 -04005564struct nfs4_get_lease_time_data {
5565 struct nfs4_get_lease_time_args *args;
5566 struct nfs4_get_lease_time_res *res;
5567 struct nfs_client *clp;
5568};
5569
5570static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5571 void *calldata)
5572{
5573 int ret;
5574 struct nfs4_get_lease_time_data *data =
5575 (struct nfs4_get_lease_time_data *)calldata;
5576
5577 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005578 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005579 /* just setup sequence, do not trigger session recovery
5580 since we're invoked within one */
5581 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005582 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005583 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005584
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005585 if (ret != -EAGAIN)
5586 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005587 dprintk("<-- %s\n", __func__);
5588}
5589
5590/*
5591 * Called from nfs4_state_manager thread for session setup, so don't recover
5592 * from sequence operation or clientid errors.
5593 */
5594static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5595{
5596 struct nfs4_get_lease_time_data *data =
5597 (struct nfs4_get_lease_time_data *)calldata;
5598
5599 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005600 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5601 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005602 switch (task->tk_status) {
5603 case -NFS4ERR_DELAY:
5604 case -NFS4ERR_GRACE:
5605 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5606 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5607 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005608 /* fall through */
5609 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005610 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005611 return;
5612 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005613 dprintk("<-- %s\n", __func__);
5614}
5615
Trond Myklebust17280172012-03-11 13:11:00 -04005616static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005617 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5618 .rpc_call_done = nfs4_get_lease_time_done,
5619};
5620
5621int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5622{
5623 struct rpc_task *task;
5624 struct nfs4_get_lease_time_args args;
5625 struct nfs4_get_lease_time_res res = {
5626 .lr_fsinfo = fsinfo,
5627 };
5628 struct nfs4_get_lease_time_data data = {
5629 .args = &args,
5630 .res = &res,
5631 .clp = clp,
5632 };
5633 struct rpc_message msg = {
5634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5635 .rpc_argp = &args,
5636 .rpc_resp = &res,
5637 };
5638 struct rpc_task_setup task_setup = {
5639 .rpc_client = clp->cl_rpcclient,
5640 .rpc_message = &msg,
5641 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005642 .callback_data = &data,
5643 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005644 };
5645 int status;
5646
Trond Myklebust9d12b212012-01-17 22:04:25 -05005647 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005648 dprintk("--> %s\n", __func__);
5649 task = rpc_run_task(&task_setup);
5650
5651 if (IS_ERR(task))
5652 status = PTR_ERR(task);
5653 else {
5654 status = task->tk_status;
5655 rpc_put_task(task);
5656 }
5657 dprintk("<-- %s return %d\n", __func__, status);
5658
5659 return status;
5660}
5661
Trond Myklebust92161062012-11-19 19:50:45 -05005662struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005663{
Trond Myklebust2d473d32012-11-19 18:03:22 -05005664 return kmalloc_array(max_slots, sizeof(struct nfs4_slot), gfp_flags);
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005665}
5666
5667static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5668 struct nfs4_slot *new,
5669 u32 max_slots,
5670 u32 ivalue)
5671{
5672 struct nfs4_slot *old = NULL;
5673 u32 i;
5674
5675 spin_lock(&tbl->slot_tbl_lock);
5676 if (new) {
5677 old = tbl->slots;
5678 tbl->slots = new;
5679 tbl->max_slots = max_slots;
5680 }
Trond Myklebustf9b1ef52012-10-29 16:48:40 -04005681 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005682 for (i = 0; i < tbl->max_slots; i++)
5683 tbl->slots[i].seq_nr = ivalue;
5684 spin_unlock(&tbl->slot_tbl_lock);
5685 kfree(old);
5686}
5687
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005688/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005689 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005690 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005691static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5692 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005693{
Andy Adamson104aeba2010-01-14 17:45:10 -05005694 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005695 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005696
Andy Adamson104aeba2010-01-14 17:45:10 -05005697 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5698 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005699
Andy Adamson104aeba2010-01-14 17:45:10 -05005700 /* Does the newly negotiated max_reqs match the existing slot table? */
5701 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005702 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005703 if (!new)
5704 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005705 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005706 ret = 0;
5707
5708 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005709 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5710 tbl, tbl->slots, tbl->max_slots);
5711out:
5712 dprintk("<-- %s: return %d\n", __func__, ret);
5713 return ret;
5714}
5715
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005716/* Destroy the slot table */
5717static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5718{
5719 if (session->fc_slot_table.slots != NULL) {
5720 kfree(session->fc_slot_table.slots);
5721 session->fc_slot_table.slots = NULL;
5722 }
5723 if (session->bc_slot_table.slots != NULL) {
5724 kfree(session->bc_slot_table.slots);
5725 session->bc_slot_table.slots = NULL;
5726 }
5727 return;
5728}
5729
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005730/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005731 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005732 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005733static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005734{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005735 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005736 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005737
Andy Adamsonaacd5532011-11-09 13:58:21 -05005738 dprintk("--> %s\n", __func__);
5739 /* Fore channel */
5740 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005741 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5742 if (status) /* -ENOMEM */
5743 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005744 /* Back channel */
5745 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005746 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5747 if (status && tbl->slots == NULL)
5748 /* Fore and back channel share a connection so get
5749 * both slot tables or neither */
5750 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005751 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005752}
5753
5754struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5755{
5756 struct nfs4_session *session;
5757 struct nfs4_slot_table *tbl;
5758
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005759 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005760 if (!session)
5761 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005762
Andy Adamson557134a2009-04-01 09:21:53 -04005763 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005764 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005765 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005766 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005767 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005768
5769 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005770 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005771 spin_lock_init(&tbl->slot_tbl_lock);
5772 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005773 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005774
Trond Myklebust1055d762010-06-16 09:52:27 -04005775 session->session_state = 1<<NFS4_SESSION_INITING;
5776
Andy Adamson557134a2009-04-01 09:21:53 -04005777 session->clp = clp;
5778 return session;
5779}
5780
5781void nfs4_destroy_session(struct nfs4_session *session)
5782{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005783 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005784 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005785
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005786 cred = nfs4_get_exchange_id_cred(session->clp);
5787 nfs4_proc_destroy_session(session, cred);
5788 if (cred)
5789 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005790
5791 rcu_read_lock();
5792 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5793 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005794 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005795 __func__, xprt);
5796 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005797 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005798 kfree(session);
5799}
5800
Andy Adamsonfc931582009-04-01 09:22:31 -04005801/*
5802 * Initialize the values to be used by the client in CREATE_SESSION
5803 * If nfs4_init_session set the fore channel request and response sizes,
5804 * use them.
5805 *
5806 * Set the back channel max_resp_sz_cached to zero to force the client to
5807 * always set csa_cachethis to FALSE because the current implementation
5808 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5809 */
5810static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5811{
5812 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005813 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5814 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005815
5816 if (mxrqst_sz == 0)
5817 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5818 if (mxresp_sz == 0)
5819 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5820 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005821 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5822 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005823 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005824 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005825
5826 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005827 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005828 __func__,
5829 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005830 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005831
5832 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005833 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5834 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5835 args->bc_attrs.max_resp_sz_cached = 0;
5836 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5837 args->bc_attrs.max_reqs = 1;
5838
5839 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5840 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5841 __func__,
5842 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5843 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5844 args->bc_attrs.max_reqs);
5845}
5846
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005847static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005848{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005849 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5850 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5851
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005852 if (rcvd->max_resp_sz > sent->max_resp_sz)
5853 return -EINVAL;
5854 /*
5855 * Our requested max_ops is the minimum we need; we're not
5856 * prepared to break up compounds into smaller pieces than that.
5857 * So, no point even trying to continue if the server won't
5858 * cooperate:
5859 */
5860 if (rcvd->max_ops < sent->max_ops)
5861 return -EINVAL;
5862 if (rcvd->max_reqs == 0)
5863 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005864 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5865 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005866 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005867}
5868
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005869static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5870{
5871 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5872 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005873
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005874 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5875 return -EINVAL;
5876 if (rcvd->max_resp_sz < sent->max_resp_sz)
5877 return -EINVAL;
5878 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5879 return -EINVAL;
5880 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005881 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005882 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005883 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005884 return -EINVAL;
5885 return 0;
5886}
Andy Adamson8d353012009-04-01 09:22:32 -04005887
Andy Adamson8d353012009-04-01 09:22:32 -04005888static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5889 struct nfs4_session *session)
5890{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005891 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005892
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005893 ret = nfs4_verify_fore_channel_attrs(args, session);
5894 if (ret)
5895 return ret;
5896 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005897}
5898
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005899static int _nfs4_proc_create_session(struct nfs_client *clp,
5900 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005901{
5902 struct nfs4_session *session = clp->cl_session;
5903 struct nfs41_create_session_args args = {
5904 .client = clp,
5905 .cb_program = NFS4_CALLBACK,
5906 };
5907 struct nfs41_create_session_res res = {
5908 .client = clp,
5909 };
5910 struct rpc_message msg = {
5911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5912 .rpc_argp = &args,
5913 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005914 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005915 };
5916 int status;
5917
5918 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005919 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005920
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005921 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005922
Trond Myklebust43095d32012-11-20 11:13:12 -05005923 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005924 /* Verify the session's negotiated channel_attrs values */
5925 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005926 /* Increment the clientid slot sequence id */
5927 clp->cl_seqid++;
5928 }
5929
5930 return status;
5931}
5932
5933/*
5934 * Issues a CREATE_SESSION operation to the server.
5935 * It is the responsibility of the caller to verify the session is
5936 * expired before calling this routine.
5937 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005938int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005939{
5940 int status;
5941 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005942 struct nfs4_session *session = clp->cl_session;
5943
5944 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5945
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005946 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005947 if (status)
5948 goto out;
5949
Andy Adamsonaacd5532011-11-09 13:58:21 -05005950 /* Init or reset the session slot tables */
5951 status = nfs4_setup_session_slot_tables(session);
5952 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005953 if (status)
5954 goto out;
5955
5956 ptr = (unsigned *)&session->sess_id.data[0];
5957 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5958 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005959out:
5960 dprintk("<-- %s\n", __func__);
5961 return status;
5962}
5963
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005964/*
5965 * Issue the over-the-wire RPC DESTROY_SESSION.
5966 * The caller must serialize access to this routine.
5967 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005968int nfs4_proc_destroy_session(struct nfs4_session *session,
5969 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005970{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005971 struct rpc_message msg = {
5972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5973 .rpc_argp = session,
5974 .rpc_cred = cred,
5975 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005976 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005977
5978 dprintk("--> nfs4_proc_destroy_session\n");
5979
5980 /* session is still being setup */
5981 if (session->clp->cl_cons_state != NFS_CS_READY)
5982 return status;
5983
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005984 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005985
5986 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005987 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005988 "Session has been destroyed regardless...\n", status);
5989
5990 dprintk("<-- nfs4_proc_destroy_session\n");
5991 return status;
5992}
5993
Trond Myklebust7b38c362012-05-23 13:23:31 -04005994/*
5995 * With sessions, the client is not marked ready until after a
5996 * successful EXCHANGE_ID and CREATE_SESSION.
5997 *
5998 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5999 * other versions of NFS can be tried.
6000 */
6001static int nfs41_check_session_ready(struct nfs_client *clp)
6002{
6003 int ret;
6004
6005 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6006 ret = nfs4_client_recover_expired_lease(clp);
6007 if (ret)
6008 return ret;
6009 }
6010 if (clp->cl_cons_state < NFS_CS_READY)
6011 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006012 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006013 return 0;
6014}
6015
Trond Myklebustfccba802009-07-21 16:48:07 -04006016int nfs4_init_session(struct nfs_server *server)
6017{
6018 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006019 struct nfs4_session *session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006020 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6021 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
Trond Myklebustfccba802009-07-21 16:48:07 -04006022
6023 if (!nfs4_has_session(clp))
6024 return 0;
6025
Trond Myklebustae72ae672012-11-20 11:02:55 -05006026 if (server->rsize != 0)
6027 target_max_resp_sz = server->rsize;
6028 target_max_resp_sz += nfs41_maxread_overhead;
6029
6030 if (server->wsize != 0)
6031 target_max_rqst_sz = server->wsize;
6032 target_max_rqst_sz += nfs41_maxwrite_overhead;
6033
Trond Myklebust1055d762010-06-16 09:52:27 -04006034 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006035 spin_lock(&clp->cl_lock);
6036 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebustae72ae672012-11-20 11:02:55 -05006037 /* Initialise targets and channel attributes */
6038 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6039 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6040 session->fc_target_max_resp_sz = target_max_resp_sz;
6041 session->fc_attrs.max_resp_sz = target_max_resp_sz;
Trond Myklebust688a9022012-11-20 10:53:39 -05006042 } else {
6043 /* Just adjust the targets */
6044 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6045 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6046 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6047 }
6048 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6049 session->fc_target_max_resp_sz = target_max_resp_sz;
6050 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6051 }
Trond Myklebust7b38c362012-05-23 13:23:31 -04006052 }
6053 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006054
Trond Myklebust688a9022012-11-20 10:53:39 -05006055 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6056 nfs4_schedule_lease_recovery(clp);
6057
Trond Myklebust7b38c362012-05-23 13:23:31 -04006058 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006059}
6060
Trond Myklebust7b38c362012-05-23 13:23:31 -04006061int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006062{
6063 struct nfs4_session *session = clp->cl_session;
6064 int ret;
6065
Trond Myklebust7b38c362012-05-23 13:23:31 -04006066 spin_lock(&clp->cl_lock);
6067 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6068 /*
6069 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6070 * DS lease to be equal to the MDS lease.
6071 */
6072 clp->cl_lease_time = lease_time;
6073 clp->cl_last_renewal = jiffies;
6074 }
6075 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006076
Trond Myklebust7b38c362012-05-23 13:23:31 -04006077 ret = nfs41_check_session_ready(clp);
6078 if (ret)
6079 return ret;
6080 /* Test for the DS role */
6081 if (!is_ds_client(clp))
6082 return -ENODEV;
6083 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006084}
6085EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6086
6087
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006088/*
6089 * Renew the cl_session lease.
6090 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006091struct nfs4_sequence_data {
6092 struct nfs_client *clp;
6093 struct nfs4_sequence_args args;
6094 struct nfs4_sequence_res res;
6095};
6096
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006097static void nfs41_sequence_release(void *data)
6098{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006099 struct nfs4_sequence_data *calldata = data;
6100 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006101
Alexandros Batsakis71358402010-02-05 03:45:05 -08006102 if (atomic_read(&clp->cl_count) > 1)
6103 nfs4_schedule_state_renewal(clp);
6104 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006105 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006106}
6107
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006108static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6109{
6110 switch(task->tk_status) {
6111 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006112 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6113 return -EAGAIN;
6114 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006115 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006116 }
6117 return 0;
6118}
6119
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006120static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006121{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006122 struct nfs4_sequence_data *calldata = data;
6123 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006124
Trond Myklebust14516c32010-07-31 14:29:06 -04006125 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6126 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006127
6128 if (task->tk_status < 0) {
6129 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006130 if (atomic_read(&clp->cl_count) == 1)
6131 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006132
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006133 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6134 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006135 return;
6136 }
6137 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006138 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006139out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006140 dprintk("<-- %s\n", __func__);
6141}
6142
6143static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6144{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006145 struct nfs4_sequence_data *calldata = data;
6146 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006147 struct nfs4_sequence_args *args;
6148 struct nfs4_sequence_res *res;
6149
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006150 args = task->tk_msg.rpc_argp;
6151 res = task->tk_msg.rpc_resp;
6152
Trond Myklebust9d12b212012-01-17 22:04:25 -05006153 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006154 return;
6155 rpc_call_start(task);
6156}
6157
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006158static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6159{
6160 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6161 nfs41_sequence_prepare(task, data);
6162}
6163
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006164static const struct rpc_call_ops nfs41_sequence_ops = {
6165 .rpc_call_done = nfs41_sequence_call_done,
6166 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006167 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006168};
6169
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006170static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6171 .rpc_call_done = nfs41_sequence_call_done,
6172 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6173 .rpc_release = nfs41_sequence_release,
6174};
6175
6176static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6177 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006178{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006179 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006180 struct rpc_message msg = {
6181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6182 .rpc_cred = cred,
6183 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006184 struct rpc_task_setup task_setup_data = {
6185 .rpc_client = clp->cl_rpcclient,
6186 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006187 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006188 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6189 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006190
Alexandros Batsakis71358402010-02-05 03:45:05 -08006191 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006192 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006193 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006194 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006195 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006196 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006197 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006198 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006199 msg.rpc_argp = &calldata->args;
6200 msg.rpc_resp = &calldata->res;
6201 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006202 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006203
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006204 return rpc_run_task(&task_setup_data);
6205}
6206
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006207static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006208{
6209 struct rpc_task *task;
6210 int ret = 0;
6211
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006212 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6213 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006214 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006215 if (IS_ERR(task))
6216 ret = PTR_ERR(task);
6217 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006218 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006219 dprintk("<-- %s status=%d\n", __func__, ret);
6220 return ret;
6221}
6222
6223static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6224{
6225 struct rpc_task *task;
6226 int ret;
6227
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006228 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006229 if (IS_ERR(task)) {
6230 ret = PTR_ERR(task);
6231 goto out;
6232 }
6233 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006234 if (!ret) {
6235 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6236
6237 if (task->tk_status == 0)
6238 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006239 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006240 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006241 rpc_put_task(task);
6242out:
6243 dprintk("<-- %s status=%d\n", __func__, ret);
6244 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006245}
6246
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006247struct nfs4_reclaim_complete_data {
6248 struct nfs_client *clp;
6249 struct nfs41_reclaim_complete_args arg;
6250 struct nfs41_reclaim_complete_res res;
6251};
6252
6253static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6254{
6255 struct nfs4_reclaim_complete_data *calldata = data;
6256
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006257 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006258 if (nfs41_setup_sequence(calldata->clp->cl_session,
6259 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006260 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006261 return;
6262
6263 rpc_call_start(task);
6264}
6265
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006266static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6267{
6268 switch(task->tk_status) {
6269 case 0:
6270 case -NFS4ERR_COMPLETE_ALREADY:
6271 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6272 break;
6273 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006274 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006275 /* fall through */
6276 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006277 return -EAGAIN;
6278 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006279 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006280 }
6281 return 0;
6282}
6283
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006284static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6285{
6286 struct nfs4_reclaim_complete_data *calldata = data;
6287 struct nfs_client *clp = calldata->clp;
6288 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6289
6290 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006291 if (!nfs41_sequence_done(task, res))
6292 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006293
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006294 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6295 rpc_restart_call_prepare(task);
6296 return;
6297 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006298 dprintk("<-- %s\n", __func__);
6299}
6300
6301static void nfs4_free_reclaim_complete_data(void *data)
6302{
6303 struct nfs4_reclaim_complete_data *calldata = data;
6304
6305 kfree(calldata);
6306}
6307
6308static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6309 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6310 .rpc_call_done = nfs4_reclaim_complete_done,
6311 .rpc_release = nfs4_free_reclaim_complete_data,
6312};
6313
6314/*
6315 * Issue a global reclaim complete.
6316 */
6317static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6318{
6319 struct nfs4_reclaim_complete_data *calldata;
6320 struct rpc_task *task;
6321 struct rpc_message msg = {
6322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6323 };
6324 struct rpc_task_setup task_setup_data = {
6325 .rpc_client = clp->cl_rpcclient,
6326 .rpc_message = &msg,
6327 .callback_ops = &nfs4_reclaim_complete_call_ops,
6328 .flags = RPC_TASK_ASYNC,
6329 };
6330 int status = -ENOMEM;
6331
6332 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006333 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006334 if (calldata == NULL)
6335 goto out;
6336 calldata->clp = clp;
6337 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006338
Trond Myklebust9d12b212012-01-17 22:04:25 -05006339 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006340 msg.rpc_argp = &calldata->arg;
6341 msg.rpc_resp = &calldata->res;
6342 task_setup_data.callback_data = calldata;
6343 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006344 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006345 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006346 goto out;
6347 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006348 status = nfs4_wait_for_completion_rpc_task(task);
6349 if (status == 0)
6350 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006351 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006352 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006353out:
6354 dprintk("<-- %s status=%d\n", __func__, status);
6355 return status;
6356}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006357
6358static void
6359nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6360{
6361 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006362 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006363
6364 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006365 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6366 * right now covering the LAYOUTGET we are about to send.
6367 * However, that is not so catastrophic, and there seems
6368 * to be no way to prevent it completely.
6369 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006370 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006371 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006372 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006373 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6374 NFS_I(lgp->args.inode)->layout,
6375 lgp->args.ctx->state)) {
6376 rpc_exit(task, NFS4_OK);
6377 return;
6378 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006379 rpc_call_start(task);
6380}
6381
6382static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6383{
6384 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006385 struct inode *inode = lgp->args.inode;
6386 struct nfs_server *server = NFS_SERVER(inode);
6387 struct pnfs_layout_hdr *lo;
6388 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006389
6390 dprintk("--> %s\n", __func__);
6391
6392 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006393 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006394
6395 switch (task->tk_status) {
6396 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006397 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006398 case -NFS4ERR_LAYOUTTRYLATER:
6399 case -NFS4ERR_RECALLCONFLICT:
6400 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006401 break;
6402 case -NFS4ERR_EXPIRED:
6403 case -NFS4ERR_BAD_STATEID:
6404 spin_lock(&inode->i_lock);
6405 lo = NFS_I(inode)->layout;
6406 if (!lo || list_empty(&lo->plh_segs)) {
6407 spin_unlock(&inode->i_lock);
6408 /* If the open stateid was bad, then recover it. */
6409 state = lgp->args.ctx->state;
6410 } else {
6411 LIST_HEAD(head);
6412
6413 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6414 spin_unlock(&inode->i_lock);
6415 /* Mark the bad layout state as invalid, then
6416 * retry using the open stateid. */
6417 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006418 }
6419 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006420 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6421 rpc_restart_call_prepare(task);
6422out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006423 dprintk("<-- %s\n", __func__);
6424}
6425
Idan Kedar85541162012-08-02 11:47:10 +03006426static size_t max_response_pages(struct nfs_server *server)
6427{
6428 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6429 return nfs_page_array_len(0, max_resp_sz);
6430}
6431
6432static void nfs4_free_pages(struct page **pages, size_t size)
6433{
6434 int i;
6435
6436 if (!pages)
6437 return;
6438
6439 for (i = 0; i < size; i++) {
6440 if (!pages[i])
6441 break;
6442 __free_page(pages[i]);
6443 }
6444 kfree(pages);
6445}
6446
6447static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6448{
6449 struct page **pages;
6450 int i;
6451
6452 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6453 if (!pages) {
6454 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6455 return NULL;
6456 }
6457
6458 for (i = 0; i < size; i++) {
6459 pages[i] = alloc_page(gfp_flags);
6460 if (!pages[i]) {
6461 dprintk("%s: failed to allocate page\n", __func__);
6462 nfs4_free_pages(pages, size);
6463 return NULL;
6464 }
6465 }
6466
6467 return pages;
6468}
6469
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006470static void nfs4_layoutget_release(void *calldata)
6471{
6472 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006473 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6474 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006475
6476 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006477 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006478 put_nfs_open_context(lgp->args.ctx);
6479 kfree(calldata);
6480 dprintk("<-- %s\n", __func__);
6481}
6482
6483static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6484 .rpc_call_prepare = nfs4_layoutget_prepare,
6485 .rpc_call_done = nfs4_layoutget_done,
6486 .rpc_release = nfs4_layoutget_release,
6487};
6488
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006489struct pnfs_layout_segment *
6490nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006491{
6492 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006493 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006494 struct rpc_task *task;
6495 struct rpc_message msg = {
6496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6497 .rpc_argp = &lgp->args,
6498 .rpc_resp = &lgp->res,
6499 };
6500 struct rpc_task_setup task_setup_data = {
6501 .rpc_client = server->client,
6502 .rpc_message = &msg,
6503 .callback_ops = &nfs4_layoutget_call_ops,
6504 .callback_data = lgp,
6505 .flags = RPC_TASK_ASYNC,
6506 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006507 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006508 int status = 0;
6509
6510 dprintk("--> %s\n", __func__);
6511
Idan Kedar85541162012-08-02 11:47:10 +03006512 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6513 if (!lgp->args.layout.pages) {
6514 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006515 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006516 }
6517 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6518
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006519 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006520 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006521 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006522 task = rpc_run_task(&task_setup_data);
6523 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006524 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006525 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006526 if (status == 0)
6527 status = task->tk_status;
6528 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006529 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006530 rpc_put_task(task);
6531 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006532 if (status)
6533 return ERR_PTR(status);
6534 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006535}
6536
Benny Halevycbe82602011-05-22 19:52:37 +03006537static void
6538nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6539{
6540 struct nfs4_layoutreturn *lrp = calldata;
6541
6542 dprintk("--> %s\n", __func__);
6543 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006544 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006545 return;
6546 rpc_call_start(task);
6547}
6548
6549static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6550{
6551 struct nfs4_layoutreturn *lrp = calldata;
6552 struct nfs_server *server;
6553
6554 dprintk("--> %s\n", __func__);
6555
6556 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6557 return;
6558
6559 server = NFS_SERVER(lrp->args.inode);
6560 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006561 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006562 return;
6563 }
Benny Halevycbe82602011-05-22 19:52:37 +03006564 dprintk("<-- %s\n", __func__);
6565}
6566
6567static void nfs4_layoutreturn_release(void *calldata)
6568{
6569 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006570 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006571
6572 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006573 spin_lock(&lo->plh_inode->i_lock);
6574 if (lrp->res.lrs_present)
6575 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6576 lo->plh_block_lgets--;
6577 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006578 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006579 kfree(calldata);
6580 dprintk("<-- %s\n", __func__);
6581}
6582
6583static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6584 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6585 .rpc_call_done = nfs4_layoutreturn_done,
6586 .rpc_release = nfs4_layoutreturn_release,
6587};
6588
6589int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6590{
6591 struct rpc_task *task;
6592 struct rpc_message msg = {
6593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6594 .rpc_argp = &lrp->args,
6595 .rpc_resp = &lrp->res,
6596 };
6597 struct rpc_task_setup task_setup_data = {
6598 .rpc_client = lrp->clp->cl_rpcclient,
6599 .rpc_message = &msg,
6600 .callback_ops = &nfs4_layoutreturn_call_ops,
6601 .callback_data = lrp,
6602 };
6603 int status;
6604
6605 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006606 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006607 task = rpc_run_task(&task_setup_data);
6608 if (IS_ERR(task))
6609 return PTR_ERR(task);
6610 status = task->tk_status;
6611 dprintk("<-- %s status=%d\n", __func__, status);
6612 rpc_put_task(task);
6613 return status;
6614}
6615
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006616/*
6617 * Retrieve the list of Data Server devices from the MDS.
6618 */
6619static int _nfs4_getdevicelist(struct nfs_server *server,
6620 const struct nfs_fh *fh,
6621 struct pnfs_devicelist *devlist)
6622{
6623 struct nfs4_getdevicelist_args args = {
6624 .fh = fh,
6625 .layoutclass = server->pnfs_curr_ld->id,
6626 };
6627 struct nfs4_getdevicelist_res res = {
6628 .devlist = devlist,
6629 };
6630 struct rpc_message msg = {
6631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6632 .rpc_argp = &args,
6633 .rpc_resp = &res,
6634 };
6635 int status;
6636
6637 dprintk("--> %s\n", __func__);
6638 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6639 &res.seq_res, 0);
6640 dprintk("<-- %s status=%d\n", __func__, status);
6641 return status;
6642}
6643
6644int nfs4_proc_getdevicelist(struct nfs_server *server,
6645 const struct nfs_fh *fh,
6646 struct pnfs_devicelist *devlist)
6647{
6648 struct nfs4_exception exception = { };
6649 int err;
6650
6651 do {
6652 err = nfs4_handle_exception(server,
6653 _nfs4_getdevicelist(server, fh, devlist),
6654 &exception);
6655 } while (exception.retry);
6656
6657 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6658 err, devlist->num_devs);
6659
6660 return err;
6661}
6662EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6663
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006664static int
6665_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6666{
6667 struct nfs4_getdeviceinfo_args args = {
6668 .pdev = pdev,
6669 };
6670 struct nfs4_getdeviceinfo_res res = {
6671 .pdev = pdev,
6672 };
6673 struct rpc_message msg = {
6674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6675 .rpc_argp = &args,
6676 .rpc_resp = &res,
6677 };
6678 int status;
6679
6680 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006681 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006682 dprintk("<-- %s status=%d\n", __func__, status);
6683
6684 return status;
6685}
6686
6687int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6688{
6689 struct nfs4_exception exception = { };
6690 int err;
6691
6692 do {
6693 err = nfs4_handle_exception(server,
6694 _nfs4_proc_getdeviceinfo(server, pdev),
6695 &exception);
6696 } while (exception.retry);
6697 return err;
6698}
6699EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6700
Andy Adamson863a3c62011-03-23 13:27:54 +00006701static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6702{
6703 struct nfs4_layoutcommit_data *data = calldata;
6704 struct nfs_server *server = NFS_SERVER(data->args.inode);
6705
6706 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006707 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006708 return;
6709 rpc_call_start(task);
6710}
6711
6712static void
6713nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6714{
6715 struct nfs4_layoutcommit_data *data = calldata;
6716 struct nfs_server *server = NFS_SERVER(data->args.inode);
6717
6718 if (!nfs4_sequence_done(task, &data->res.seq_res))
6719 return;
6720
6721 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006722 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6723 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6724 case -NFS4ERR_BADLAYOUT: /* no layout */
6725 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006726 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006727 break;
6728 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006729 nfs_post_op_update_inode_force_wcc(data->args.inode,
6730 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006731 break;
6732 default:
6733 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6734 rpc_restart_call_prepare(task);
6735 return;
6736 }
6737 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006738}
6739
6740static void nfs4_layoutcommit_release(void *calldata)
6741{
6742 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006743 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006744 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006745
Andy Adamsondb29c082011-07-30 20:52:38 -04006746 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006747 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006748 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6749 list_del_init(&lseg->pls_lc_list);
6750 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6751 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006752 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006753 }
Peng Tao92407e72011-10-23 20:21:17 -07006754
6755 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6756 smp_mb__after_clear_bit();
6757 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6758
Andy Adamson863a3c62011-03-23 13:27:54 +00006759 put_rpccred(data->cred);
6760 kfree(data);
6761}
6762
6763static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6764 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6765 .rpc_call_done = nfs4_layoutcommit_done,
6766 .rpc_release = nfs4_layoutcommit_release,
6767};
6768
6769int
Andy Adamsonef311532011-03-12 02:58:10 -05006770nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006771{
6772 struct rpc_message msg = {
6773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6774 .rpc_argp = &data->args,
6775 .rpc_resp = &data->res,
6776 .rpc_cred = data->cred,
6777 };
6778 struct rpc_task_setup task_setup_data = {
6779 .task = &data->task,
6780 .rpc_client = NFS_CLIENT(data->args.inode),
6781 .rpc_message = &msg,
6782 .callback_ops = &nfs4_layoutcommit_ops,
6783 .callback_data = data,
6784 .flags = RPC_TASK_ASYNC,
6785 };
6786 struct rpc_task *task;
6787 int status = 0;
6788
6789 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6790 "lbw: %llu inode %lu\n",
6791 data->task.tk_pid, sync,
6792 data->args.lastbytewritten,
6793 data->args.inode->i_ino);
6794
Trond Myklebust9d12b212012-01-17 22:04:25 -05006795 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006796 task = rpc_run_task(&task_setup_data);
6797 if (IS_ERR(task))
6798 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006799 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006800 goto out;
6801 status = nfs4_wait_for_completion_rpc_task(task);
6802 if (status != 0)
6803 goto out;
6804 status = task->tk_status;
6805out:
6806 dprintk("%s: status %d\n", __func__, status);
6807 rpc_put_task(task);
6808 return status;
6809}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006810
6811static int
6812_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6813 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6814{
6815 struct nfs41_secinfo_no_name_args args = {
6816 .style = SECINFO_STYLE_CURRENT_FH,
6817 };
6818 struct nfs4_secinfo_res res = {
6819 .flavors = flavors,
6820 };
6821 struct rpc_message msg = {
6822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6823 .rpc_argp = &args,
6824 .rpc_resp = &res,
6825 };
6826 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6827}
6828
6829static int
6830nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6831 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6832{
6833 struct nfs4_exception exception = { };
6834 int err;
6835 do {
6836 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6837 switch (err) {
6838 case 0:
6839 case -NFS4ERR_WRONGSEC:
6840 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006841 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006842 default:
6843 err = nfs4_handle_exception(server, err, &exception);
6844 }
6845 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006846out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006847 return err;
6848}
6849
6850static int
6851nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6852 struct nfs_fsinfo *info)
6853{
6854 int err;
6855 struct page *page;
6856 rpc_authflavor_t flavor;
6857 struct nfs4_secinfo_flavors *flavors;
6858
6859 page = alloc_page(GFP_KERNEL);
6860 if (!page) {
6861 err = -ENOMEM;
6862 goto out;
6863 }
6864
6865 flavors = page_address(page);
6866 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6867
6868 /*
6869 * Fall back on "guess and check" method if
6870 * the server doesn't support SECINFO_NO_NAME
6871 */
6872 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6873 err = nfs4_find_root_sec(server, fhandle, info);
6874 goto out_freepage;
6875 }
6876 if (err)
6877 goto out_freepage;
6878
6879 flavor = nfs_find_best_sec(flavors);
6880 if (err == 0)
6881 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6882
6883out_freepage:
6884 put_page(page);
6885 if (err == -EACCES)
6886 return -EPERM;
6887out:
6888 return err;
6889}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006890
6891static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006892{
6893 int status;
6894 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006895 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006896 };
6897 struct nfs41_test_stateid_res res;
6898 struct rpc_message msg = {
6899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6900 .rpc_argp = &args,
6901 .rpc_resp = &res,
6902 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006903
Chuck Lever38527b12012-07-11 16:30:23 -04006904 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006905 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006906 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006907 if (status != NFS_OK) {
6908 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006909 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006910 }
6911 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006912 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006913}
6914
Chuck Lever38527b12012-07-11 16:30:23 -04006915/**
6916 * nfs41_test_stateid - perform a TEST_STATEID operation
6917 *
6918 * @server: server / transport on which to perform the operation
6919 * @stateid: state ID to test
6920 *
6921 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6922 * Otherwise a negative NFS4ERR value is returned if the operation
6923 * failed or the state ID is not currently valid.
6924 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006925static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006926{
6927 struct nfs4_exception exception = { };
6928 int err;
6929 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006930 err = _nfs41_test_stateid(server, stateid);
6931 if (err != -NFS4ERR_DELAY)
6932 break;
6933 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006934 } while (exception.retry);
6935 return err;
6936}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006937
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006938static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006939{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006940 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006941 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006942 };
6943 struct nfs41_free_stateid_res res;
6944 struct rpc_message msg = {
6945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6946 .rpc_argp = &args,
6947 .rpc_resp = &res,
6948 };
Chuck Lever38527b12012-07-11 16:30:23 -04006949 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006950
Chuck Lever38527b12012-07-11 16:30:23 -04006951 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006952 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006953 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006954 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006955 dprintk("NFS reply free_stateid: %d\n", status);
6956 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006957}
6958
Chuck Lever38527b12012-07-11 16:30:23 -04006959/**
6960 * nfs41_free_stateid - perform a FREE_STATEID operation
6961 *
6962 * @server: server / transport on which to perform the operation
6963 * @stateid: state ID to release
6964 *
6965 * Returns NFS_OK if the server freed "stateid". Otherwise a
6966 * negative NFS4ERR value is returned.
6967 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006968static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006969{
6970 struct nfs4_exception exception = { };
6971 int err;
6972 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006973 err = _nfs4_free_stateid(server, stateid);
6974 if (err != -NFS4ERR_DELAY)
6975 break;
6976 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006977 } while (exception.retry);
6978 return err;
6979}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006980
6981static bool nfs41_match_stateid(const nfs4_stateid *s1,
6982 const nfs4_stateid *s2)
6983{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006984 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006985 return false;
6986
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006987 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006988 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006989 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006990 return true;
6991
6992 return false;
6993}
6994
Andy Adamson557134a2009-04-01 09:21:53 -04006995#endif /* CONFIG_NFS_V4_1 */
6996
Trond Myklebust36281ca2012-03-04 18:13:56 -05006997static bool nfs4_match_stateid(const nfs4_stateid *s1,
6998 const nfs4_stateid *s2)
6999{
Trond Myklebustf597c532012-03-04 18:13:56 -05007000 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007001}
7002
7003
Trond Myklebust17280172012-03-11 13:11:00 -04007004static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007005 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007006 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 .recover_open = nfs4_open_reclaim,
7008 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007009 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007010 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007011 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012};
7013
Andy Adamson591d71c2009-04-01 09:22:47 -04007014#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007015static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007016 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7017 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7018 .recover_open = nfs4_open_reclaim,
7019 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007020 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007021 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007022 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007023 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007024};
7025#endif /* CONFIG_NFS_V4_1 */
7026
Trond Myklebust17280172012-03-11 13:11:00 -04007027static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007028 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007029 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 .recover_open = nfs4_open_expired,
7031 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007032 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007033 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034};
7035
Andy Adamson591d71c2009-04-01 09:22:47 -04007036#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007037static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007038 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7039 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007040 .recover_open = nfs41_open_expired,
7041 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007042 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007043 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007044};
7045#endif /* CONFIG_NFS_V4_1 */
7046
Trond Myklebust17280172012-03-11 13:11:00 -04007047static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007048 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007049 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007050 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007051};
7052
7053#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007054static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007055 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007056 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007057 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007058};
7059#endif
7060
Trond Myklebust97dc1352010-06-16 09:52:26 -04007061static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7062 .minor_version = 0,
7063 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007064 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007065 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007066 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7067 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7068 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007069};
7070
7071#if defined(CONFIG_NFS_V4_1)
7072static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7073 .minor_version = 1,
7074 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007075 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007076 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007077 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7078 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7079 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007080};
7081#endif
7082
Trond Myklebust97dc1352010-06-16 09:52:26 -04007083const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7084 [0] = &nfs_v4_0_minor_ops,
7085#if defined(CONFIG_NFS_V4_1)
7086 [1] = &nfs_v4_1_minor_ops,
7087#endif
7088};
7089
Bryan Schumaker73a79702012-07-16 16:39:12 -04007090const struct inode_operations nfs4_dir_inode_operations = {
7091 .create = nfs_create,
7092 .lookup = nfs_lookup,
7093 .atomic_open = nfs_atomic_open,
7094 .link = nfs_link,
7095 .unlink = nfs_unlink,
7096 .symlink = nfs_symlink,
7097 .mkdir = nfs_mkdir,
7098 .rmdir = nfs_rmdir,
7099 .mknod = nfs_mknod,
7100 .rename = nfs_rename,
7101 .permission = nfs_permission,
7102 .getattr = nfs_getattr,
7103 .setattr = nfs_setattr,
7104 .getxattr = generic_getxattr,
7105 .setxattr = generic_setxattr,
7106 .listxattr = generic_listxattr,
7107 .removexattr = generic_removexattr,
7108};
7109
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007110static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007111 .permission = nfs_permission,
7112 .getattr = nfs_getattr,
7113 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007114 .getxattr = generic_getxattr,
7115 .setxattr = generic_setxattr,
7116 .listxattr = generic_listxattr,
7117 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007118};
7119
David Howells509de812006-08-22 20:06:11 -04007120const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 .version = 4, /* protocol version */
7122 .dentry_ops = &nfs4_dentry_operations,
7123 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007124 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007125 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007127 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007128 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 .getattr = nfs4_proc_getattr,
7130 .setattr = nfs4_proc_setattr,
7131 .lookup = nfs4_proc_lookup,
7132 .access = nfs4_proc_access,
7133 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 .create = nfs4_proc_create,
7135 .remove = nfs4_proc_remove,
7136 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007137 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 .unlink_done = nfs4_proc_unlink_done,
7139 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007140 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007141 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007142 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 .link = nfs4_proc_link,
7144 .symlink = nfs4_proc_symlink,
7145 .mkdir = nfs4_proc_mkdir,
7146 .rmdir = nfs4_proc_remove,
7147 .readdir = nfs4_proc_readdir,
7148 .mknod = nfs4_proc_mknod,
7149 .statfs = nfs4_proc_statfs,
7150 .fsinfo = nfs4_proc_fsinfo,
7151 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007152 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 .decode_dirent = nfs4_decode_dirent,
7154 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007155 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007156 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007157 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007159 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007160 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007161 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007163 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007164 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007166 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007167 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007168 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007169 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007170 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007171 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007172 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007173 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007174 .create_server = nfs4_create_server,
7175 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176};
7177
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007178static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7179 .prefix = XATTR_NAME_NFSV4_ACL,
7180 .list = nfs4_xattr_list_nfs4_acl,
7181 .get = nfs4_xattr_get_nfs4_acl,
7182 .set = nfs4_xattr_set_nfs4_acl,
7183};
7184
7185const struct xattr_handler *nfs4_xattr_handlers[] = {
7186 &nfs4_xattr_nfs4_acl_handler,
7187 NULL
7188};
7189
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190/*
7191 * Local variables:
7192 * c-basic-offset: 8
7193 * End:
7194 */