blob: 1ac339b4f092d54163dc692b24e7155d9883c134 [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{
489 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400490 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400491
492 /*
493 * sr_status remains 1 if an RPC level error occurred. The server
494 * may or may not have processed the sequence operation..
495 * Proceed as if the server received and processed the sequence
496 * operation.
497 */
498 if (res->sr_status == 1)
499 res->sr_status = NFS_OK;
500
Bryan Schumaker468f8612011-04-18 15:57:32 -0400501 /* don't increment the sequence number if the task wasn't sent */
502 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400503 goto out;
504
Andy Adamson691daf32009-12-04 15:55:39 -0500505 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400506 switch (res->sr_status) {
507 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400508 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400509 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400510 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400511 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400512 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500513 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500514 if (res->sr_status_flags != 0)
515 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400516 break;
517 case -NFS4ERR_DELAY:
518 /* The server detected a resend of the RPC call and
519 * returned NFS4ERR_DELAY as per Section 2.10.6.2
520 * of RFC5661.
521 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200522 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400523 __func__,
524 res->sr_slot - res->sr_session->fc_slot_table.slots,
525 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400526 goto out_retry;
527 default:
528 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400529 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400530 }
531out:
532 /* The session may be reset by one of the error handlers. */
533 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400534 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400535 return 1;
536out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400537 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400538 goto out;
539 rpc_delay(task, NFS4_POLL_RETRY_MAX);
540 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400541}
542
Trond Myklebust14516c32010-07-31 14:29:06 -0400543static int nfs4_sequence_done(struct rpc_task *task,
544 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400545{
Trond Myklebust14516c32010-07-31 14:29:06 -0400546 if (res->sr_session == NULL)
547 return 1;
548 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400549}
550
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400551/*
Benny Halevy510b8172009-04-01 09:22:14 -0400552 * nfs4_find_slot - efficiently look for a free slot
553 *
554 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
555 * If found, we mark the slot as used, update the highest_used_slotid,
556 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500557 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558 *
559 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400560 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500561static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800562nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400563{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500564 u32 slotid;
565 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400566
Trond Myklebust45d43c22012-02-06 19:38:51 -0500567 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400568 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
569 tbl->max_slots);
570 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
571 if (slotid >= tbl->max_slots)
572 goto out;
573 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500574 if (slotid > tbl->highest_used_slotid ||
575 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400576 tbl->highest_used_slotid = slotid;
577 ret_id = slotid;
578out:
579 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
580 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400581 return ret_id;
582}
583
Trond Myklebust9d12b212012-01-17 22:04:25 -0500584static void nfs41_init_sequence(struct nfs4_sequence_args *args,
585 struct nfs4_sequence_res *res, int cache_reply)
586{
587 args->sa_session = NULL;
588 args->sa_cache_this = 0;
589 if (cache_reply)
590 args->sa_cache_this = 1;
591 res->sr_session = NULL;
592 res->sr_slot = NULL;
593}
594
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000595int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400596 struct nfs4_sequence_args *args,
597 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400598 struct rpc_task *task)
599{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600 struct nfs4_slot *slot;
601 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500602 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400603
604 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400606 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607 return 0;
608
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400609 tbl = &session->fc_slot_table;
610
611 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400612 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800613 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500614 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500615 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400616 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500617 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500618 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400619 }
620
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800621 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
622 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
623 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
624 spin_unlock(&tbl->slot_tbl_lock);
625 dprintk("%s enforce FIFO order\n", __func__);
626 return -EAGAIN;
627 }
628
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800629 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500630 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400631 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
632 spin_unlock(&tbl->slot_tbl_lock);
633 dprintk("<-- %s: no free slots\n", __func__);
634 return -EAGAIN;
635 }
636 spin_unlock(&tbl->slot_tbl_lock);
637
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800638 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400639 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400640 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400641 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400642
643 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
644
Benny Halevy99fe60d2009-04-01 09:22:29 -0400645 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400646 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400647 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400648 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400649 /*
650 * sr_status is only set in decode_sequence, and so will remain
651 * set to 1 if an rpc level failure occurs.
652 */
653 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 return 0;
655}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000656EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657
Trond Myklebust035168a2010-06-16 09:52:26 -0400658int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659 struct nfs4_sequence_args *args,
660 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 struct rpc_task *task)
662{
Trond Myklebust035168a2010-06-16 09:52:26 -0400663 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 int ret = 0;
665
Trond Myklebust9d12b212012-01-17 22:04:25 -0500666 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400668
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200669 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400670 __func__, session->clp, session, res->sr_slot ?
671 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400672
Trond Myklebust9d12b212012-01-17 22:04:25 -0500673 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400674out:
675 dprintk("<-- %s status=%d\n", __func__, ret);
676 return ret;
677}
678
679struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400680 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 struct nfs4_sequence_args *seq_args;
682 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683};
684
685static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
686{
687 struct nfs41_call_sync_data *data = calldata;
688
Trond Myklebust035168a2010-06-16 09:52:26 -0400689 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
690
691 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500692 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400693 return;
694 rpc_call_start(task);
695}
696
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800697static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
698{
699 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
700 nfs41_call_sync_prepare(task, calldata);
701}
702
Andy Adamson69ab40c2009-04-01 09:22:19 -0400703static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
704{
705 struct nfs41_call_sync_data *data = calldata;
706
Trond Myklebust14516c32010-07-31 14:29:06 -0400707 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400708}
709
Trond Myklebust17280172012-03-11 13:11:00 -0400710static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400711 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400712 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713};
714
Trond Myklebust17280172012-03-11 13:11:00 -0400715static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800716 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
717 .rpc_call_done = nfs41_call_sync_done,
718};
719
Bryan Schumaker7c513052011-03-24 17:12:24 +0000720static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
721 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400722 struct rpc_message *msg,
723 struct nfs4_sequence_args *args,
724 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800725 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400726{
727 int ret;
728 struct rpc_task *task;
729 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400730 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400731 .seq_args = args,
732 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400733 };
734 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000735 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400736 .rpc_message = msg,
737 .callback_ops = &nfs41_call_sync_ops,
738 .callback_data = &data
739 };
740
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800741 if (privileged)
742 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400743 task = rpc_run_task(&task_setup);
744 if (IS_ERR(task))
745 ret = PTR_ERR(task);
746 else {
747 ret = task->tk_status;
748 rpc_put_task(task);
749 }
750 return ret;
751}
752
Bryan Schumaker7c513052011-03-24 17:12:24 +0000753int _nfs4_call_sync_session(struct rpc_clnt *clnt,
754 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400755 struct rpc_message *msg,
756 struct nfs4_sequence_args *args,
757 struct nfs4_sequence_res *res,
758 int cache_reply)
759{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500760 nfs41_init_sequence(args, res, cache_reply);
761 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400762}
763
Trond Myklebustdf896452010-06-16 09:52:26 -0400764#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500765static inline
766void nfs41_init_sequence(struct nfs4_sequence_args *args,
767 struct nfs4_sequence_res *res, int cache_reply)
768{
769}
770
Trond Myklebust14516c32010-07-31 14:29:06 -0400771static int nfs4_sequence_done(struct rpc_task *task,
772 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400773{
Trond Myklebust14516c32010-07-31 14:29:06 -0400774 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400775}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400776#endif /* CONFIG_NFS_V4_1 */
777
Bryan Schumaker7c513052011-03-24 17:12:24 +0000778int _nfs4_call_sync(struct rpc_clnt *clnt,
779 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400780 struct rpc_message *msg,
781 struct nfs4_sequence_args *args,
782 struct nfs4_sequence_res *res,
783 int cache_reply)
784{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500785 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000786 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400787}
788
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000789static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000790int nfs4_call_sync(struct rpc_clnt *clnt,
791 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000792 struct rpc_message *msg,
793 struct nfs4_sequence_args *args,
794 struct nfs4_sequence_res *res,
795 int cache_reply)
796{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000797 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
798 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000799}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400800
Trond Myklebust38478b22006-05-25 01:40:57 -0400801static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Trond Myklebust38478b22006-05-25 01:40:57 -0400803 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Trond Myklebust38478b22006-05-25 01:40:57 -0400805 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400806 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700807 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400808 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700809 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400810 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100813struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400814 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815 struct nfs_openargs o_arg;
816 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100817 struct nfs_open_confirmargs c_arg;
818 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500819 struct nfs4_string owner_name;
820 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400823 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400825 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100827 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400828 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100829 int rpc_status;
830 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831};
832
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400833
834static void nfs4_init_opendata_res(struct nfs4_opendata *p)
835{
836 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400837 p->o_res.seqid = p->o_arg.seqid;
838 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400839 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400840 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400841 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500842 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400843}
844
Al Viro82a2c1b2011-06-22 18:30:55 -0400845static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500846 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400847 const struct iattr *attrs,
848 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100849{
Al Viro82a2c1b2011-06-22 18:30:55 -0400850 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100851 struct inode *dir = parent->d_inode;
852 struct nfs_server *server = NFS_SERVER(dir);
853 struct nfs4_opendata *p;
854
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400855 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100856 if (p == NULL)
857 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400858 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 if (p->o_arg.seqid == NULL)
860 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400861 nfs_sb_active(dentry->d_sb);
862 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100863 p->dir = parent;
864 p->owner = sp;
865 atomic_inc(&sp->so_count);
866 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867 p->o_arg.open_flags = flags;
868 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700869 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
870 * will return permission denied for all bits until close */
871 if (!(flags & O_EXCL)) {
872 /* ask server to check for all possible rights as results
873 * are cached */
874 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
875 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700876 }
David Howells7539bba2006-08-22 20:06:09 -0400877 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400878 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
879 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400880 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 p->o_arg.server = server;
882 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400883 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100884 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500885 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500886 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400887
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100888 p->o_arg.u.attrs = &p->attrs;
889 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500890
891 verf[0] = jiffies;
892 verf[1] = current->pid;
893 memcpy(p->o_arg.u.verifier.data, verf,
894 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100895 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100896 p->c_arg.fh = &p->o_res.fh;
897 p->c_arg.stateid = &p->o_res.stateid;
898 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400899 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400900 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100901 return p;
902err_free:
903 kfree(p);
904err:
905 dput(parent);
906 return NULL;
907}
908
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400909static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100910{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400911 struct nfs4_opendata *p = container_of(kref,
912 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400913 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400914
915 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400916 if (p->state != NULL)
917 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400918 nfs4_put_state_owner(p->owner);
919 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400920 dput(p->dentry);
921 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500922 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400923 kfree(p);
924}
925
926static void nfs4_opendata_put(struct nfs4_opendata *p)
927{
928 if (p != NULL)
929 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100930}
931
Trond Myklebust06f814a2006-01-03 09:55:07 +0100932static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
933{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100934 int ret;
935
Trond Myklebust06f814a2006-01-03 09:55:07 +0100936 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100937 return ret;
938}
939
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400941{
942 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943
Trond Myklebust536e43d2012-01-17 22:04:26 -0500944 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945 goto out;
946 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400947 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500948 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
949 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400950 break;
951 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500952 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
953 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400954 break;
955 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500956 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
957 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400958 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400960 return ret;
961}
962
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400964{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500965 if (delegation == NULL)
966 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400968 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500969 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400970 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500971 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400972 return 1;
973}
974
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100976{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100978 case FMODE_WRITE:
979 state->n_wronly++;
980 break;
981 case FMODE_READ:
982 state->n_rdonly++;
983 break;
984 case FMODE_READ|FMODE_WRITE:
985 state->n_rdwr++;
986 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100988}
989
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400991{
992 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500993 nfs4_stateid_copy(&state->stateid, stateid);
994 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400996 case FMODE_READ:
997 set_bit(NFS_O_RDONLY_STATE, &state->flags);
998 break;
999 case FMODE_WRITE:
1000 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1001 break;
1002 case FMODE_READ|FMODE_WRITE:
1003 set_bit(NFS_O_RDWR_STATE, &state->flags);
1004 }
1005}
1006
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001008{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001009 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001011 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001012}
1013
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014static 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 -07001015{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001016 /*
1017 * Protect the call to nfs4_state_set_mode_locked and
1018 * serialise the stateid update
1019 */
1020 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001021 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001022 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001023 set_bit(NFS_DELEGATED_STATE, &state->flags);
1024 }
1025 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001027 write_sequnlock(&state->seqlock);
1028 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001030 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033static 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 -05001034{
1035 struct nfs_inode *nfsi = NFS_I(state->inode);
1036 struct nfs_delegation *deleg_cur;
1037 int ret = 0;
1038
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001040
1041 rcu_read_lock();
1042 deleg_cur = rcu_dereference(nfsi->delegation);
1043 if (deleg_cur == NULL)
1044 goto no_delegation;
1045
1046 spin_lock(&deleg_cur->lock);
1047 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001049 goto no_delegation_unlock;
1050
1051 if (delegation == NULL)
1052 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001053 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001054 goto no_delegation_unlock;
1055
Trond Myklebustb7391f42008-12-23 15:21:52 -05001056 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001057 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001058 ret = 1;
1059no_delegation_unlock:
1060 spin_unlock(&deleg_cur->lock);
1061no_delegation:
1062 rcu_read_unlock();
1063
1064 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001065 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001066 ret = 1;
1067 }
1068
1069 return ret;
1070}
1071
1072
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074{
1075 struct nfs_delegation *delegation;
1076
1077 rcu_read_lock();
1078 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001079 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 rcu_read_unlock();
1081 return;
1082 }
1083 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001084 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085}
1086
Trond Myklebust6ee41262007-07-08 14:11:36 -04001087static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088{
1089 struct nfs4_state *state = opendata->state;
1090 struct nfs_inode *nfsi = NFS_I(state->inode);
1091 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001092 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001093 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001094 nfs4_stateid stateid;
1095 int ret = -EAGAIN;
1096
Trond Myklebustaac00a82007-07-05 19:02:21 -04001097 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001098 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001099 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001100 if (can_open_cached(state, fmode, open_mode)) {
1101 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001102 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 goto out_return_state;
1104 }
1105 spin_unlock(&state->owner->so_lock);
1106 }
Trond Myklebust34310432008-12-23 15:21:38 -05001107 rcu_read_lock();
1108 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001109 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001110 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001111 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001112 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001113 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001114 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001115 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001116 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117 if (ret != 0)
1118 goto out;
1119 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001120
1121 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001122 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001123 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001124 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001125out:
1126 return ERR_PTR(ret);
1127out_return_state:
1128 atomic_inc(&state->count);
1129 return state;
1130}
1131
Andy Adamsone23008e2012-10-02 21:07:32 -04001132static void
1133nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1134{
1135 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1136 struct nfs_delegation *delegation;
1137 int delegation_flags = 0;
1138
1139 rcu_read_lock();
1140 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1141 if (delegation)
1142 delegation_flags = delegation->flags;
1143 rcu_read_unlock();
1144 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1145 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1146 "returning a delegation for "
1147 "OPEN(CLAIM_DELEGATE_CUR)\n",
1148 clp->cl_hostname);
1149 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1150 nfs_inode_set_delegation(state->inode,
1151 data->owner->so_cred,
1152 &data->o_res);
1153 else
1154 nfs_inode_reclaim_delegation(state->inode,
1155 data->owner->so_cred,
1156 &data->o_res);
1157}
1158
1159/*
1160 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1161 * and update the nfs4_state.
1162 */
1163static struct nfs4_state *
1164_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1165{
1166 struct inode *inode = data->state->inode;
1167 struct nfs4_state *state = data->state;
1168 int ret;
1169
1170 if (!data->rpc_done) {
1171 ret = data->rpc_status;
1172 goto err;
1173 }
1174
1175 ret = -ESTALE;
1176 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1177 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1178 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1179 goto err;
1180
1181 ret = -ENOMEM;
1182 state = nfs4_get_open_state(inode, data->owner);
1183 if (state == NULL)
1184 goto err;
1185
1186 ret = nfs_refresh_inode(inode, &data->f_attr);
1187 if (ret)
1188 goto err;
1189
1190 if (data->o_res.delegation_type != 0)
1191 nfs4_opendata_check_deleg(data, state);
1192 update_open_stateid(state, &data->o_res.stateid, NULL,
1193 data->o_arg.fmode);
1194
1195 return state;
1196err:
1197 return ERR_PTR(ret);
1198
1199}
1200
1201static struct nfs4_state *
1202_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001203{
1204 struct inode *inode;
1205 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001206 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001207
Trond Myklebustaac00a82007-07-05 19:02:21 -04001208 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001209 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001210 goto out;
1211 }
1212
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001213 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001214 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001215 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001216 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001217 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001218 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001219 goto err;
1220 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001221 state = nfs4_get_open_state(inode, data->owner);
1222 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001223 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001224 if (data->o_res.delegation_type != 0)
1225 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001226 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001227 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001228 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001229out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001230 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001231err_put_inode:
1232 iput(inode);
1233err:
1234 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001235}
1236
Andy Adamsone23008e2012-10-02 21:07:32 -04001237static struct nfs4_state *
1238nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1239{
1240 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1241 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1242 return _nfs4_opendata_to_nfs4_state(data);
1243}
1244
Trond Myklebust864472e2006-01-03 09:55:15 +01001245static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1246{
1247 struct nfs_inode *nfsi = NFS_I(state->inode);
1248 struct nfs_open_context *ctx;
1249
1250 spin_lock(&state->inode->i_lock);
1251 list_for_each_entry(ctx, &nfsi->open_files, list) {
1252 if (ctx->state != state)
1253 continue;
1254 get_nfs_open_context(ctx);
1255 spin_unlock(&state->inode->i_lock);
1256 return ctx;
1257 }
1258 spin_unlock(&state->inode->i_lock);
1259 return ERR_PTR(-ENOENT);
1260}
1261
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001262static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1263{
1264 struct nfs4_opendata *opendata;
1265
Al Viro3d4ff432011-06-22 18:40:12 -04001266 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001267 if (opendata == NULL)
1268 return ERR_PTR(-ENOMEM);
1269 opendata->state = state;
1270 atomic_inc(&state->count);
1271 return opendata;
1272}
1273
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001274static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001275{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001276 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001277 int ret;
1278
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001279 opendata->o_arg.open_flags = 0;
1280 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001281 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1282 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1283 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001284 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001285 if (ret != 0)
1286 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001287 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001288 if (IS_ERR(newstate))
1289 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001290 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001291 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001292 return 0;
1293}
1294
1295static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1296{
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001298 int ret;
1299
1300 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001301 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001302 smp_rmb();
1303 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001304 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001305 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001306 if (ret != 0)
1307 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001308 if (newstate != state)
1309 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001310 }
1311 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001312 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001313 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001314 if (ret != 0)
1315 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001316 if (newstate != state)
1317 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 }
1319 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001320 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001321 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001322 if (ret != 0)
1323 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001324 if (newstate != state)
1325 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001326 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001327 /*
1328 * We may have performed cached opens for all three recoveries.
1329 * Check if we need to update the current stateid.
1330 */
1331 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001332 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001333 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001334 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001335 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001336 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001337 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001338 return 0;
1339}
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/*
1342 * OPEN_RECLAIM:
1343 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001345static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001347 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001348 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001349 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 int status;
1351
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001352 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1353 if (IS_ERR(opendata))
1354 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001355 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1356 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001357 rcu_read_lock();
1358 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001359 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001360 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001361 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001362 opendata->o_arg.u.delegation_type = delegation_type;
1363 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001364 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return status;
1366}
1367
Trond Myklebust539cd032007-06-05 11:46:42 -04001368static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct nfs_server *server = NFS_SERVER(state->inode);
1371 struct nfs4_exception exception = { };
1372 int err;
1373 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001374 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001375 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001376 break;
1377 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 } while (exception.retry);
1379 return err;
1380}
1381
Trond Myklebust864472e2006-01-03 09:55:15 +01001382static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1383{
1384 struct nfs_open_context *ctx;
1385 int ret;
1386
1387 ctx = nfs4_state_find_open_context(state);
1388 if (IS_ERR(ctx))
1389 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001390 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001391 put_nfs_open_context(ctx);
1392 return ret;
1393}
1394
Trond Myklebust13437e12007-07-06 15:10:43 -04001395static 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 -07001396{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001397 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001398 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001400 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1401 if (IS_ERR(opendata))
1402 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001403 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001404 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001405 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001406 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001407 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Trond Myklebust13437e12007-07-06 15:10:43 -04001410int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001413 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 int err;
1415 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001416 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 switch (err) {
1418 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001419 case -ENOENT:
1420 case -ESTALE:
1421 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001422 case -NFS4ERR_BADSESSION:
1423 case -NFS4ERR_BADSLOT:
1424 case -NFS4ERR_BAD_HIGH_SLOT:
1425 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1426 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001427 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001428 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 case -NFS4ERR_STALE_CLIENTID:
1430 case -NFS4ERR_STALE_STATEID:
1431 case -NFS4ERR_EXPIRED:
1432 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001433 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001434 goto out;
1435 case -ERESTARTSYS:
1436 /*
1437 * The show must go on: exit, but mark the
1438 * stateid as needing recovery.
1439 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001440 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001441 case -NFS4ERR_ADMIN_REVOKED:
1442 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001443 nfs_inode_find_state_and_recover(state->inode,
1444 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001445 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001446 case -EKEYEXPIRED:
1447 /*
1448 * User RPCSEC_GSS context has expired.
1449 * We cannot recover this stateid now, so
1450 * skip it and allow recovery thread to
1451 * proceed.
1452 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001453 case -ENOMEM:
1454 err = 0;
1455 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457 err = nfs4_handle_exception(server, err, &exception);
1458 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001459out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return err;
1461}
1462
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001463static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1464{
1465 struct nfs4_opendata *data = calldata;
1466
1467 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001468 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001469 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001470 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001471 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001472 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001473 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001474}
1475
1476static void nfs4_open_confirm_release(void *calldata)
1477{
1478 struct nfs4_opendata *data = calldata;
1479 struct nfs4_state *state = NULL;
1480
1481 /* If this request hasn't been cancelled, do nothing */
1482 if (data->cancelled == 0)
1483 goto out_free;
1484 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001485 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001486 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001487 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001488 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001489 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001490out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001491 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001492}
1493
1494static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001495 .rpc_call_done = nfs4_open_confirm_done,
1496 .rpc_release = nfs4_open_confirm_release,
1497};
1498
1499/*
1500 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1501 */
1502static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1503{
1504 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1505 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001506 struct rpc_message msg = {
1507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1508 .rpc_argp = &data->c_arg,
1509 .rpc_resp = &data->c_res,
1510 .rpc_cred = data->owner->so_cred,
1511 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001512 struct rpc_task_setup task_setup_data = {
1513 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001514 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001515 .callback_ops = &nfs4_open_confirm_ops,
1516 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001517 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001518 .flags = RPC_TASK_ASYNC,
1519 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int status;
1521
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001522 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001523 data->rpc_done = 0;
1524 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001525 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001526 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001527 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001528 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001529 status = nfs4_wait_for_completion_rpc_task(task);
1530 if (status != 0) {
1531 data->cancelled = 1;
1532 smp_wmb();
1533 } else
1534 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001535 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return status;
1537}
1538
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001541 struct nfs4_opendata *data = calldata;
1542 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001543
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1545 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001546 /*
1547 * Check if we still need to send an OPEN call, or if we can use
1548 * a delegation instead.
1549 */
1550 if (data->state != NULL) {
1551 struct nfs_delegation *delegation;
1552
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001553 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001554 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001555 rcu_read_lock();
1556 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001557 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1558 can_open_delegated(delegation, data->o_arg.fmode))
1559 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001560 rcu_read_unlock();
1561 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001562 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001563 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001564 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001565 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001566 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001567 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1568 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001569 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001570 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001571 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001572 &data->o_res.seq_res,
1573 task) != 0)
1574 nfs_release_seqid(data->o_arg.seqid);
1575 else
1576 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001577 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001578unlock_no_action:
1579 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001580out_no_action:
1581 task->tk_action = NULL;
1582
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001583}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001585static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1586{
1587 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1588 nfs4_open_prepare(task, calldata);
1589}
1590
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001591static void nfs4_open_done(struct rpc_task *task, void *calldata)
1592{
1593 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001595 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001596
Trond Myklebust14516c32010-07-31 14:29:06 -04001597 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1598 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001599
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001600 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001601 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1602 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001603 case S_IFREG:
1604 break;
1605 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001606 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001607 break;
1608 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001609 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001610 break;
1611 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001613 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001614 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001615 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001616 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1617 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001618 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001619 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001620}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001621
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622static void nfs4_open_release(void *calldata)
1623{
1624 struct nfs4_opendata *data = calldata;
1625 struct nfs4_state *state = NULL;
1626
1627 /* If this request hasn't been cancelled, do nothing */
1628 if (data->cancelled == 0)
1629 goto out_free;
1630 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001631 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001632 goto out_free;
1633 /* In case we need an open_confirm, no cleanup! */
1634 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1635 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001636 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001637 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001638 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001640 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641}
1642
1643static const struct rpc_call_ops nfs4_open_ops = {
1644 .rpc_call_prepare = nfs4_open_prepare,
1645 .rpc_call_done = nfs4_open_done,
1646 .rpc_release = nfs4_open_release,
1647};
1648
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001649static const struct rpc_call_ops nfs4_recover_open_ops = {
1650 .rpc_call_prepare = nfs4_recover_open_prepare,
1651 .rpc_call_done = nfs4_open_done,
1652 .rpc_release = nfs4_open_release,
1653};
1654
1655static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001656{
1657 struct inode *dir = data->dir->d_inode;
1658 struct nfs_server *server = NFS_SERVER(dir);
1659 struct nfs_openargs *o_arg = &data->o_arg;
1660 struct nfs_openres *o_res = &data->o_res;
1661 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001662 struct rpc_message msg = {
1663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1664 .rpc_argp = o_arg,
1665 .rpc_resp = o_res,
1666 .rpc_cred = data->owner->so_cred,
1667 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001668 struct rpc_task_setup task_setup_data = {
1669 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001670 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001671 .callback_ops = &nfs4_open_ops,
1672 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001673 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001674 .flags = RPC_TASK_ASYNC,
1675 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 int status;
1677
Trond Myklebust9d12b212012-01-17 22:04:25 -05001678 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001679 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001680 data->rpc_done = 0;
1681 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001682 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001683 if (isrecover)
1684 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001685 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001686 if (IS_ERR(task))
1687 return PTR_ERR(task);
1688 status = nfs4_wait_for_completion_rpc_task(task);
1689 if (status != 0) {
1690 data->cancelled = 1;
1691 smp_wmb();
1692 } else
1693 status = data->rpc_status;
1694 rpc_put_task(task);
1695
1696 return status;
1697}
1698
1699static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1700{
1701 struct inode *dir = data->dir->d_inode;
1702 struct nfs_openres *o_res = &data->o_res;
1703 int status;
1704
1705 status = nfs4_run_open_task(data, 1);
1706 if (status != 0 || !data->rpc_done)
1707 return status;
1708
Trond Myklebust6926afd2012-01-07 13:22:46 -05001709 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1710
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001711 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1712 status = _nfs4_proc_open_confirm(data);
1713 if (status != 0)
1714 return status;
1715 }
1716
1717 return status;
1718}
1719
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001720static int nfs4_opendata_access(struct rpc_cred *cred,
1721 struct nfs4_opendata *opendata,
1722 struct nfs4_state *state, fmode_t fmode)
1723{
1724 struct nfs_access_entry cache;
1725 u32 mask;
1726
1727 /* access call failed or for some reason the server doesn't
1728 * support any access modes -- defer access call until later */
1729 if (opendata->o_res.access_supported == 0)
1730 return 0;
1731
1732 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001733 /* don't check MAY_WRITE - a newly created file may not have
1734 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001735 if (fmode & FMODE_READ)
1736 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001737 if (fmode & FMODE_EXEC)
1738 mask |= MAY_EXEC;
1739
1740 cache.cred = cred;
1741 cache.jiffies = jiffies;
1742 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1743 nfs_access_add_cache(state->inode, &cache);
1744
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001745 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001746 return 0;
1747
1748 /* even though OPEN succeeded, access is denied. Close the file */
1749 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001750 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001751}
1752
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001753/*
1754 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1755 */
1756static int _nfs4_proc_open(struct nfs4_opendata *data)
1757{
1758 struct inode *dir = data->dir->d_inode;
1759 struct nfs_server *server = NFS_SERVER(dir);
1760 struct nfs_openargs *o_arg = &data->o_arg;
1761 struct nfs_openres *o_res = &data->o_res;
1762 int status;
1763
1764 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001765 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001766 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001767 if (status != 0) {
1768 if (status == -NFS4ERR_BADNAME &&
1769 !(o_arg->open_flags & O_CREAT))
1770 return -ENOENT;
1771 return status;
1772 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773
Trond Myklebust6926afd2012-01-07 13:22:46 -05001774 nfs_fattr_map_and_free_names(server, &data->f_attr);
1775
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001776 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001777 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001778 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1779 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001781 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001786 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Andy Adamsond83217c2011-03-01 01:34:17 +00001790static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001791{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001792 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001793 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001794
Trond Myklebusta78cb572009-08-09 15:06:19 -04001795 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001796 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001797 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001798 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001799 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1800 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001801 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001802 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001803 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001804 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001805 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001806}
1807
Andy Adamsond83217c2011-03-01 01:34:17 +00001808static int nfs4_recover_expired_lease(struct nfs_server *server)
1809{
1810 return nfs4_client_recover_expired_lease(server->nfs_client);
1811}
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813/*
1814 * OPEN_EXPIRED:
1815 * reclaim state on the server after a network partition.
1816 * Assumes caller holds the appropriate lock
1817 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001818static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001820 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001821 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001823 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1824 if (IS_ERR(opendata))
1825 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001826 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001827 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001828 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001829 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001830 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001833static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001834{
Trond Myklebust539cd032007-06-05 11:46:42 -04001835 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001836 struct nfs4_exception exception = { };
1837 int err;
1838
1839 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001840 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001841 switch (err) {
1842 default:
1843 goto out;
1844 case -NFS4ERR_GRACE:
1845 case -NFS4ERR_DELAY:
1846 nfs4_handle_exception(server, err, &exception);
1847 err = 0;
1848 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001849 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001850out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001851 return err;
1852}
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1855{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001857 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 ctx = nfs4_state_find_open_context(state);
1860 if (IS_ERR(ctx))
1861 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001862 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001863 put_nfs_open_context(ctx);
1864 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001867#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001868static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001869{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001870 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001871 nfs4_stateid *stateid = &state->stateid;
1872 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001873
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001874 /* If a state reset has been done, test_stateid is unneeded */
1875 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1876 return;
1877
1878 status = nfs41_test_stateid(server, stateid);
1879 if (status != NFS_OK) {
1880 /* Free the stateid unless the server explicitly
1881 * informs us the stateid is unrecognized. */
1882 if (status != -NFS4ERR_BAD_STATEID)
1883 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001884 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001885
Bryan Schumaker69388672012-09-26 15:25:52 -04001886 write_seqlock(&state->seqlock);
1887 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1888 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001889 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1890 }
1891}
1892
1893/**
1894 * nfs41_check_open_stateid - possibly free an open stateid
1895 *
1896 * @state: NFSv4 state for an inode
1897 *
1898 * Returns NFS_OK if recovery for this stateid is now finished.
1899 * Otherwise a negative NFS4ERR value is returned.
1900 */
1901static int nfs41_check_open_stateid(struct nfs4_state *state)
1902{
1903 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001904 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001905 int status;
1906
1907 /* If a state reset has been done, test_stateid is unneeded */
1908 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1909 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1910 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1911 return -NFS4ERR_BAD_STATEID;
1912
1913 status = nfs41_test_stateid(server, stateid);
1914 if (status != NFS_OK) {
1915 /* Free the stateid unless the server explicitly
1916 * informs us the stateid is unrecognized. */
1917 if (status != -NFS4ERR_BAD_STATEID)
1918 nfs41_free_stateid(server, stateid);
1919
1920 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1921 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1922 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001923 }
1924 return status;
1925}
1926
1927static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1928{
Chuck Levereb64cf92012-07-11 16:30:05 -04001929 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001930
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001931 nfs41_clear_delegation_stateid(state);
1932 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001933 if (status != NFS_OK)
1934 status = nfs4_open_expired(sp, state);
1935 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001936}
1937#endif
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001940 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1941 * fields corresponding to attributes that were used to store the verifier.
1942 * Make sure we clobber those fields in the later setattr call
1943 */
1944static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1945{
1946 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1947 !(sattr->ia_valid & ATTR_ATIME_SET))
1948 sattr->ia_valid |= ATTR_ATIME;
1949
1950 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1951 !(sattr->ia_valid & ATTR_MTIME_SET))
1952 sattr->ia_valid |= ATTR_MTIME;
1953}
1954
1955/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001956 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 */
Andy Adamson82be4172012-05-23 05:02:35 -04001958static int _nfs4_do_open(struct inode *dir,
1959 struct dentry *dentry,
1960 fmode_t fmode,
1961 int flags,
1962 struct iattr *sattr,
1963 struct rpc_cred *cred,
1964 struct nfs4_state **res,
1965 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 struct nfs4_state_owner *sp;
1968 struct nfs4_state *state = NULL;
1969 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001970 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001971 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001975 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1976 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1978 goto out_err;
1979 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001980 status = nfs4_recover_expired_lease(server);
1981 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001982 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001983 if (dentry->d_inode != NULL)
1984 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001985 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001986 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001987 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001988 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Andy Adamson82be4172012-05-23 05:02:35 -04001990 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1991 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1992 if (!opendata->f_attr.mdsthreshold)
1993 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001994 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001995 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001996 if (dentry->d_inode != NULL)
1997 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001998
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001999 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002001 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002003 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002004 status = PTR_ERR(state);
2005 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002006 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002007 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002008 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002009
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002010 status = nfs4_opendata_access(cred, opendata, state, fmode);
2011 if (status != 0)
2012 goto err_opendata_put;
2013
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002014 if (opendata->o_arg.open_flags & O_EXCL) {
2015 nfs4_exclusive_attrset(opendata, sattr);
2016
2017 nfs_fattr_init(opendata->o_res.f_attr);
2018 status = nfs4_do_setattr(state->inode, cred,
2019 opendata->o_res.f_attr, sattr,
2020 state);
2021 if (status == 0)
2022 nfs_setattr_update_inode(state->inode, sattr);
2023 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2024 }
Andy Adamson82be4172012-05-23 05:02:35 -04002025
2026 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2027 *ctx_th = opendata->f_attr.mdsthreshold;
2028 else
2029 kfree(opendata->f_attr.mdsthreshold);
2030 opendata->f_attr.mdsthreshold = NULL;
2031
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002032 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 *res = state;
2035 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002036err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002037 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002038 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002039err_put_state_owner:
2040 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 *res = NULL;
2043 return status;
2044}
2045
2046
Andy Adamson82be4172012-05-23 05:02:35 -04002047static struct nfs4_state *nfs4_do_open(struct inode *dir,
2048 struct dentry *dentry,
2049 fmode_t fmode,
2050 int flags,
2051 struct iattr *sattr,
2052 struct rpc_cred *cred,
2053 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 struct nfs4_exception exception = { };
2056 struct nfs4_state *res;
2057 int status;
2058
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002059 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002061 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2062 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (status == 0)
2064 break;
2065 /* NOTE: BAD_SEQID means the server and client disagree about the
2066 * book-keeping w.r.t. state-changing operations
2067 * (OPEN/CLOSE/LOCK/LOCKU...)
2068 * It is actually a sign of a bug on the client or on the server.
2069 *
2070 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002071 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 * have unhashed the old state_owner for us, and that we can
2073 * therefore safely retry using a new one. We should still warn
2074 * the user though...
2075 */
2076 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002077 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002078 " returned a bad sequence-id error!\n",
2079 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 exception.retry = 1;
2081 continue;
2082 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002083 /*
2084 * BAD_STATEID on OPEN means that the server cancelled our
2085 * state before it received the OPEN_CONFIRM.
2086 * Recover by retrying the request as per the discussion
2087 * on Page 181 of RFC3530.
2088 */
2089 if (status == -NFS4ERR_BAD_STATEID) {
2090 exception.retry = 1;
2091 continue;
2092 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002093 if (status == -EAGAIN) {
2094 /* We must have found a delegation */
2095 exception.retry = 1;
2096 continue;
2097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2099 status, &exception));
2100 } while (exception.retry);
2101 return res;
2102}
2103
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002104static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2105 struct nfs_fattr *fattr, struct iattr *sattr,
2106 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002108 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002110 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 .iap = sattr,
2112 .server = server,
2113 .bitmask = server->attr_bitmask,
2114 };
2115 struct nfs_setattrres res = {
2116 .fattr = fattr,
2117 .server = server,
2118 };
2119 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2121 .rpc_argp = &arg,
2122 .rpc_resp = &res,
2123 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002125 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002126 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Trond Myklebust0e574af2005-10-27 22:12:38 -04002128 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Trond Myklebust4fc87962012-03-08 17:42:01 -05002130 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002131 struct nfs_lockowner lockowner = {
2132 .l_owner = current->files,
2133 .l_pid = current->tgid,
2134 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002135 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002136 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002137 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2138 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002139 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002140 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002141 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Bryan Schumaker7c513052011-03-24 17:12:24 +00002143 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002144 if (status == 0 && state != NULL)
2145 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002146 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002149static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2150 struct nfs_fattr *fattr, struct iattr *sattr,
2151 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002153 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002154 struct nfs4_exception exception = {
2155 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002156 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002157 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 int err;
2159 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002160 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2161 switch (err) {
2162 case -NFS4ERR_OPENMODE:
2163 if (state && !(state->state & FMODE_WRITE)) {
2164 err = -EBADF;
2165 if (sattr->ia_valid & ATTR_OPEN)
2166 err = -EACCES;
2167 goto out;
2168 }
2169 }
2170 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002172out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return err;
2174}
2175
2176struct nfs4_closedata {
2177 struct inode *inode;
2178 struct nfs4_state *state;
2179 struct nfs_closeargs arg;
2180 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002181 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002182 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002183 bool roc;
2184 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185};
2186
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002187static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002188{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002189 struct nfs4_closedata *calldata = data;
2190 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002191 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002192
Fred Isamanf7e89172011-01-06 11:36:32 +00002193 if (calldata->roc)
2194 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002195 nfs4_put_open_state(calldata->state);
2196 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002197 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002198 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002199 kfree(calldata);
2200}
2201
Trond Myklebust88069f72009-12-08 08:33:16 -05002202static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2203 fmode_t fmode)
2204{
2205 spin_lock(&state->owner->so_lock);
2206 if (!(fmode & FMODE_READ))
2207 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2208 if (!(fmode & FMODE_WRITE))
2209 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2210 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2211 spin_unlock(&state->owner->so_lock);
2212}
2213
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002214static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002216 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct nfs_server *server = NFS_SERVER(calldata->inode);
2219
Chuck Levera3ca5652012-03-01 17:00:40 -05002220 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002221 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2222 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /* hmm. we are done with the inode, and in the process of freeing
2224 * the state_owner. we keep this around to process errors
2225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 switch (task->tk_status) {
2227 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002228 if (calldata->roc)
2229 pnfs_roc_set_barrier(state->inode,
2230 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002231 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002232 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002233 nfs4_close_clear_stateid_flags(state,
2234 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 break;
2236 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002237 case -NFS4ERR_OLD_STATEID:
2238 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002240 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002243 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2244 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002246 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002247 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002248 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002251static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002253 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002254 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002255 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002256 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002257
Chuck Levera3ca5652012-03-01 17:00:40 -05002258 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002259 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002260 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002261
Trond Myklebust88069f72009-12-08 08:33:16 -05002262 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2263 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002264 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002265 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002266 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002267 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002268 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2269 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2270 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002271 }
2272 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002273 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2274 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2275 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002276 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002277 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002278 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002279
2280 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002281 /* Note: exit _without_ calling nfs4_close_done */
2282 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002283 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002284 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002285
Fred Isamanf7e89172011-01-06 11:36:32 +00002286 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002287 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002288 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002289 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002290 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002291 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002292
Trond Myklebust516a6af2005-10-27 22:12:41 -04002293 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002294 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002295 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002296 &calldata->arg.seq_args,
2297 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002298 task) != 0)
2299 nfs_release_seqid(calldata->arg.seqid);
2300 else
2301 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002302out:
2303 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002306static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002307 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002308 .rpc_call_done = nfs4_close_done,
2309 .rpc_release = nfs4_free_closedata,
2310};
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312/*
2313 * It is possible for data to be read/written from a mem-mapped file
2314 * after the sys_close call (which hits the vfs layer as a flush).
2315 * This means that we can't safely call nfsv4 close on a file until
2316 * the inode is cleared. This in turn means that we are not good
2317 * NFSv4 citizens - we do not indicate to the server to update the file's
2318 * share state even when we are done with one of the three share
2319 * stateid's in the inode.
2320 *
2321 * NOTE: Caller must be holding the sp->so_owner semaphore!
2322 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002323int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002325 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002327 struct nfs4_state_owner *sp = state->owner;
2328 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002329 struct rpc_message msg = {
2330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2331 .rpc_cred = state->owner->so_cred,
2332 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002333 struct rpc_task_setup task_setup_data = {
2334 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002335 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002336 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002337 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002338 .flags = RPC_TASK_ASYNC,
2339 };
Trond Myklebust95121352005-10-18 14:20:12 -07002340 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002342 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002344 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002345 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002346 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002348 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002349 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002351 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002352 if (calldata->arg.seqid == NULL)
2353 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002354 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002355 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002356 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002357 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002358 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002359 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002360 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002361
Trond Myklebust1174dd12010-12-21 10:52:24 -05002362 msg.rpc_argp = &calldata->arg;
2363 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002364 task_setup_data.callback_data = calldata;
2365 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002366 if (IS_ERR(task))
2367 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002368 status = 0;
2369 if (wait)
2370 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002371 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002372 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002373out_free_calldata:
2374 kfree(calldata);
2375out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002376 nfs4_put_open_state(state);
2377 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002378 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
Trond Myklebust2b484292010-09-17 10:56:51 -04002381static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002382nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 struct nfs4_state *state;
2385
Trond Myklebust565277f2007-10-15 18:17:53 -04002386 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002387 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2388 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002389 if (IS_ERR(state))
2390 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002391 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002392 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
Trond Myklebust1185a552009-12-03 15:54:02 -05002395static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002396{
2397 if (ctx->state == NULL)
2398 return;
2399 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002400 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002401 else
Al Viro643168c2011-06-22 18:20:23 -04002402 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002403}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2406{
Benny Halevy43652ad2009-04-01 09:21:54 -04002407 struct nfs4_server_caps_arg args = {
2408 .fhandle = fhandle,
2409 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 struct nfs4_server_caps_res res = {};
2411 struct rpc_message msg = {
2412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002413 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 .rpc_resp = &res,
2415 };
2416 int status;
2417
Bryan Schumaker7c513052011-03-24 17:12:24 +00002418 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (status == 0) {
2420 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002421 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2422 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2423 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2424 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2425 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2427 server->caps |= NFS_CAP_ACLS;
2428 if (res.has_links != 0)
2429 server->caps |= NFS_CAP_HARDLINKS;
2430 if (res.has_symlinks != 0)
2431 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002432 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2433 server->caps |= NFS_CAP_FILEID;
2434 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2435 server->caps |= NFS_CAP_MODE;
2436 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2437 server->caps |= NFS_CAP_NLINK;
2438 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2439 server->caps |= NFS_CAP_OWNER;
2440 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2441 server->caps |= NFS_CAP_OWNER_GROUP;
2442 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2443 server->caps |= NFS_CAP_ATIME;
2444 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2445 server->caps |= NFS_CAP_CTIME;
2446 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2447 server->caps |= NFS_CAP_MTIME;
2448
Trond Myklebusta65318b2009-03-11 14:10:28 -04002449 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2450 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2451 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002453 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return status;
2457}
2458
Trond Myklebust55a97592006-06-09 09:34:19 -04002459int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
2461 struct nfs4_exception exception = { };
2462 int err;
2463 do {
2464 err = nfs4_handle_exception(server,
2465 _nfs4_server_capabilities(server, fhandle),
2466 &exception);
2467 } while (exception.retry);
2468 return err;
2469}
2470
2471static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2472 struct nfs_fsinfo *info)
2473{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 struct nfs4_lookup_root_arg args = {
2475 .bitmask = nfs4_fattr_bitmap,
2476 };
2477 struct nfs4_lookup_res res = {
2478 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002479 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 .fh = fhandle,
2481 };
2482 struct rpc_message msg = {
2483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2484 .rpc_argp = &args,
2485 .rpc_resp = &res,
2486 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002487
Trond Myklebust0e574af2005-10-27 22:12:38 -04002488 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002489 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
2492static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2493 struct nfs_fsinfo *info)
2494{
2495 struct nfs4_exception exception = { };
2496 int err;
2497 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002498 err = _nfs4_lookup_root(server, fhandle, info);
2499 switch (err) {
2500 case 0:
2501 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002502 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002503 default:
2504 err = nfs4_handle_exception(server, err, &exception);
2505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002507out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return err;
2509}
2510
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002511static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2512 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2513{
2514 struct rpc_auth *auth;
2515 int ret;
2516
2517 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002518 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002519 ret = -EIO;
2520 goto out;
2521 }
2522 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002523out:
2524 return ret;
2525}
2526
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002527static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002528 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002530 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002531 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002533 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002534 if (len < 0)
2535 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002536
2537 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002538 /* AUTH_UNIX is the default flavor if none was specified,
2539 * thus has already been tried. */
2540 if (flav_array[i] == RPC_AUTH_UNIX)
2541 continue;
2542
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002543 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002544 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002545 continue;
2546 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002547 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002548 /*
2549 * -EACCESS could mean that the user doesn't have correct permissions
2550 * to access the mount. It could also mean that we tried to mount
2551 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2552 * existing mount programs don't handle -EACCES very well so it should
2553 * be mapped to -EPERM instead.
2554 */
2555 if (status == -EACCES)
2556 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002557 return status;
2558}
2559
2560/*
2561 * get the file handle for the "/" directory on the server
2562 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002563int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2564 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002565{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002566 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002567 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002568 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2569 /*
2570 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2571 * by nfs4_map_errors() as this function exits.
2572 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002573 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (status == 0)
2575 status = nfs4_server_capabilities(server, fhandle);
2576 if (status == 0)
2577 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002578 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
Bryan Schumakerbae36242012-05-10 15:07:31 -04002581static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2582 struct nfs_fsinfo *info)
2583{
2584 int error;
2585 struct nfs_fattr *fattr = info->fattr;
2586
2587 error = nfs4_server_capabilities(server, mntfh);
2588 if (error < 0) {
2589 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2590 return error;
2591 }
2592
2593 error = nfs4_proc_getattr(server, mntfh, fattr);
2594 if (error < 0) {
2595 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2596 return error;
2597 }
2598
2599 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2600 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2601 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2602
2603 return error;
2604}
2605
Manoj Naik6b97fd32006-06-09 09:34:29 -04002606/*
2607 * Get locations and (maybe) other attributes of a referral.
2608 * Note that we'll actually follow the referral later when
2609 * we detect fsid mismatch in inode revalidation
2610 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002611static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2612 const struct qstr *name, struct nfs_fattr *fattr,
2613 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002614{
2615 int status = -ENOMEM;
2616 struct page *page = NULL;
2617 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002618
2619 page = alloc_page(GFP_KERNEL);
2620 if (page == NULL)
2621 goto out;
2622 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2623 if (locations == NULL)
2624 goto out;
2625
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002626 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002627 if (status != 0)
2628 goto out;
2629 /* Make sure server returned a different fsid for the referral */
2630 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002631 dprintk("%s: server did not return a different fsid for"
2632 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002633 status = -EIO;
2634 goto out;
2635 }
Andy Adamson533eb462011-06-13 18:25:56 -04002636 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2637 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002638
Andy Adamson533eb462011-06-13 18:25:56 -04002639 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002640 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002641 memset(fhandle, 0, sizeof(struct nfs_fh));
2642out:
2643 if (page)
2644 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002645 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002646 return status;
2647}
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2650{
2651 struct nfs4_getattr_arg args = {
2652 .fh = fhandle,
2653 .bitmask = server->attr_bitmask,
2654 };
2655 struct nfs4_getattr_res res = {
2656 .fattr = fattr,
2657 .server = server,
2658 };
2659 struct rpc_message msg = {
2660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2661 .rpc_argp = &args,
2662 .rpc_resp = &res,
2663 };
2664
Trond Myklebust0e574af2005-10-27 22:12:38 -04002665 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002666 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
2669static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2670{
2671 struct nfs4_exception exception = { };
2672 int err;
2673 do {
2674 err = nfs4_handle_exception(server,
2675 _nfs4_proc_getattr(server, fhandle, fattr),
2676 &exception);
2677 } while (exception.retry);
2678 return err;
2679}
2680
2681/*
2682 * The file is not closed if it is opened due to the a request to change
2683 * the size of the file. The open call will not be needed once the
2684 * VFS layer lookup-intents are implemented.
2685 *
2686 * Close is called when the inode is destroyed.
2687 * If we haven't opened the file for O_WRONLY, we
2688 * need to in the size_change case to obtain a stateid.
2689 *
2690 * Got race?
2691 * Because OPEN is always done by name in nfsv4, it is
2692 * possible that we opened a different file by the same
2693 * name. We can recognize this race condition, but we
2694 * can't do anything about it besides returning an error.
2695 *
2696 * This will be fixed with VFS changes (lookup-intent).
2697 */
2698static int
2699nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2700 struct iattr *sattr)
2701{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002702 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002703 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002704 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 int status;
2706
Benny Halevy8a1636c2010-07-14 15:43:57 -04002707 if (pnfs_ld_layoutret_on_setattr(inode))
2708 pnfs_return_layout(inode);
2709
Trond Myklebust0e574af2005-10-27 22:12:38 -04002710 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Andy Adamson26699402012-05-30 16:12:24 -04002712 /* Deal with open(O_TRUNC) */
2713 if (sattr->ia_valid & ATTR_OPEN)
2714 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2715
2716 /* Optimization: if the end result is no change, don't RPC */
2717 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2718 return 0;
2719
Trond Myklebustd5308382005-11-04 15:33:38 -05002720 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002721 if (sattr->ia_valid & ATTR_FILE) {
2722 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002723
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002724 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002725 if (ctx) {
2726 cred = ctx->cred;
2727 state = ctx->state;
2728 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002729 }
2730
2731 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002732 if (status == 0)
2733 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return status;
2735}
2736
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002737static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2738 const struct qstr *name, struct nfs_fh *fhandle,
2739 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002740{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002741 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002742 int status;
2743 struct nfs4_lookup_arg args = {
2744 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002745 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002746 .name = name,
2747 };
2748 struct nfs4_lookup_res res = {
2749 .server = server,
2750 .fattr = fattr,
2751 .fh = fhandle,
2752 };
2753 struct rpc_message msg = {
2754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2755 .rpc_argp = &args,
2756 .rpc_resp = &res,
2757 };
2758
2759 nfs_fattr_init(fattr);
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002762 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 dprintk("NFS reply lookup: %d\n", status);
2764 return status;
2765}
2766
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002767static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002768{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002769 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002770 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002771 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2772 fattr->nlink = 2;
2773}
2774
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002775static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2776 struct qstr *name, struct nfs_fh *fhandle,
2777 struct nfs_fattr *fattr)
2778{
2779 struct nfs4_exception exception = { };
2780 struct rpc_clnt *client = *clnt;
2781 int err;
2782 do {
2783 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2784 switch (err) {
2785 case -NFS4ERR_BADNAME:
2786 err = -ENOENT;
2787 goto out;
2788 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002789 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002790 goto out;
2791 case -NFS4ERR_WRONGSEC:
2792 err = -EPERM;
2793 if (client != *clnt)
2794 goto out;
2795
2796 client = nfs4_create_sec_client(client, dir, name);
2797 if (IS_ERR(client))
2798 return PTR_ERR(client);
2799
2800 exception.retry = 1;
2801 break;
2802 default:
2803 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2804 }
2805 } while (exception.retry);
2806
2807out:
2808 if (err == 0)
2809 *clnt = client;
2810 else if (client != *clnt)
2811 rpc_shutdown_client(client);
2812
2813 return err;
2814}
2815
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002816static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002817 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002819 int status;
2820 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002821
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002822 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2823 if (client != NFS_CLIENT(dir)) {
2824 rpc_shutdown_client(client);
2825 nfs_fixup_secinfo_attributes(fattr);
2826 }
2827 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
2829
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002830struct rpc_clnt *
2831nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2832 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2833{
2834 int status;
2835 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2836
2837 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2838 if (status < 0) {
2839 rpc_shutdown_client(client);
2840 return ERR_PTR(status);
2841 }
2842 return client;
2843}
2844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2846{
Trond Myklebust76b32992007-08-10 17:45:11 -04002847 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 struct nfs4_accessargs args = {
2849 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002850 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002852 struct nfs4_accessres res = {
2853 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002854 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 struct rpc_message msg = {
2856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2857 .rpc_argp = &args,
2858 .rpc_resp = &res,
2859 .rpc_cred = entry->cred,
2860 };
2861 int mode = entry->mask;
2862 int status;
2863
2864 /*
2865 * Determine which access bits we want to ask for...
2866 */
2867 if (mode & MAY_READ)
2868 args.access |= NFS4_ACCESS_READ;
2869 if (S_ISDIR(inode->i_mode)) {
2870 if (mode & MAY_WRITE)
2871 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2872 if (mode & MAY_EXEC)
2873 args.access |= NFS4_ACCESS_LOOKUP;
2874 } else {
2875 if (mode & MAY_WRITE)
2876 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2877 if (mode & MAY_EXEC)
2878 args.access |= NFS4_ACCESS_EXECUTE;
2879 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002880
2881 res.fattr = nfs_alloc_fattr();
2882 if (res.fattr == NULL)
2883 return -ENOMEM;
2884
Bryan Schumaker7c513052011-03-24 17:12:24 +00002885 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002887 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002888 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002890 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return status;
2892}
2893
2894static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2895{
2896 struct nfs4_exception exception = { };
2897 int err;
2898 do {
2899 err = nfs4_handle_exception(NFS_SERVER(inode),
2900 _nfs4_proc_access(inode, entry),
2901 &exception);
2902 } while (exception.retry);
2903 return err;
2904}
2905
2906/*
2907 * TODO: For the time being, we don't try to get any attributes
2908 * along with any of the zero-copy operations READ, READDIR,
2909 * READLINK, WRITE.
2910 *
2911 * In the case of the first three, we want to put the GETATTR
2912 * after the read-type operation -- this is because it is hard
2913 * to predict the length of a GETATTR response in v4, and thus
2914 * align the READ data correctly. This means that the GETATTR
2915 * may end up partially falling into the page cache, and we should
2916 * shift it into the 'tail' of the xdr_buf before processing.
2917 * To do this efficiently, we need to know the total length
2918 * of data received, which doesn't seem to be available outside
2919 * of the RPC layer.
2920 *
2921 * In the case of WRITE, we also want to put the GETATTR after
2922 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002923 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 *
2925 * Both of these changes to the XDR layer would in fact be quite
2926 * minor, but I decided to leave them for a subsequent patch.
2927 */
2928static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2929 unsigned int pgbase, unsigned int pglen)
2930{
2931 struct nfs4_readlink args = {
2932 .fh = NFS_FH(inode),
2933 .pgbase = pgbase,
2934 .pglen = pglen,
2935 .pages = &page,
2936 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002937 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 struct rpc_message msg = {
2939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2940 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002941 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 };
2943
Bryan Schumaker7c513052011-03-24 17:12:24 +00002944 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 -07002945}
2946
2947static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2948 unsigned int pgbase, unsigned int pglen)
2949{
2950 struct nfs4_exception exception = { };
2951 int err;
2952 do {
2953 err = nfs4_handle_exception(NFS_SERVER(inode),
2954 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2955 &exception);
2956 } while (exception.retry);
2957 return err;
2958}
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002961 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963static int
2964nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002965 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002967 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 int status = 0;
2970
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002971 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2972 if (IS_ERR(ctx))
2973 return PTR_ERR(ctx);
2974
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002975 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002976 state = nfs4_do_open(dir, dentry, ctx->mode,
2977 flags, sattr, ctx->cred,
2978 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002979 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (IS_ERR(state)) {
2981 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002984 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002985 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002986 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002988 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return status;
2990}
2991
2992static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2993{
Trond Myklebust16e42952005-10-27 22:12:44 -04002994 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002995 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002997 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002999 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003000 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003001 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3004 .rpc_argp = &args,
3005 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003007 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003008
Bryan Schumaker7c513052011-03-24 17:12:24 +00003009 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003010 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003011 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return status;
3013}
3014
3015static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3016{
3017 struct nfs4_exception exception = { };
3018 int err;
3019 do {
3020 err = nfs4_handle_exception(NFS_SERVER(dir),
3021 _nfs4_proc_remove(dir, name),
3022 &exception);
3023 } while (exception.retry);
3024 return err;
3025}
3026
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003027static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003029 struct nfs_server *server = NFS_SERVER(dir);
3030 struct nfs_removeargs *args = msg->rpc_argp;
3031 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003033 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003035 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003038static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3039{
3040 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3041 &data->args.seq_args,
3042 &data->res.seq_res,
3043 task))
3044 return;
3045 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003048static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003050 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3051
Trond Myklebust14516c32010-07-31 14:29:06 -04003052 if (!nfs4_sequence_done(task, &res->seq_res))
3053 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003054 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003055 return 0;
3056 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003057 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058}
3059
Jeff Laytond3d41522010-09-17 17:31:57 -04003060static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3061{
3062 struct nfs_server *server = NFS_SERVER(dir);
3063 struct nfs_renameargs *arg = msg->rpc_argp;
3064 struct nfs_renameres *res = msg->rpc_resp;
3065
3066 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003067 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003068 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003069}
3070
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003071static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3072{
3073 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3074 &data->args.seq_args,
3075 &data->res.seq_res,
3076 task))
3077 return;
3078 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003079}
3080
3081static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3082 struct inode *new_dir)
3083{
3084 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3085
3086 if (!nfs4_sequence_done(task, &res->seq_res))
3087 return 0;
3088 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3089 return 0;
3090
3091 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003092 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003093 return 1;
3094}
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3097 struct inode *new_dir, struct qstr *new_name)
3098{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003099 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003100 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 .old_dir = NFS_FH(old_dir),
3102 .new_dir = NFS_FH(new_dir),
3103 .old_name = old_name,
3104 .new_name = new_name,
3105 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003106 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003107 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003108 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 struct rpc_message msg = {
3110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3111 .rpc_argp = &arg,
3112 .rpc_resp = &res,
3113 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003114 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Bryan Schumaker7c513052011-03-24 17:12:24 +00003116 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (!status) {
3118 update_changeattr(old_dir, &res.old_cinfo);
3119 update_changeattr(new_dir, &res.new_cinfo);
3120 }
3121 return status;
3122}
3123
3124static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3125 struct inode *new_dir, struct qstr *new_name)
3126{
3127 struct nfs4_exception exception = { };
3128 int err;
3129 do {
3130 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3131 _nfs4_proc_rename(old_dir, old_name,
3132 new_dir, new_name),
3133 &exception);
3134 } while (exception.retry);
3135 return err;
3136}
3137
3138static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3139{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003140 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 struct nfs4_link_arg arg = {
3142 .fh = NFS_FH(inode),
3143 .dir_fh = NFS_FH(dir),
3144 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003145 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003147 struct nfs4_link_res res = {
3148 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003149 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 struct rpc_message msg = {
3151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3152 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003153 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003155 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Trond Myklebust136f2622010-04-16 16:22:49 -04003157 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003158 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003159 goto out;
3160
Bryan Schumaker7c513052011-03-24 17:12:24 +00003161 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003162 if (!status) {
3163 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003164 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003165 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003166out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003167 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 return status;
3169}
3170
3171static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3172{
3173 struct nfs4_exception exception = { };
3174 int err;
3175 do {
3176 err = nfs4_handle_exception(NFS_SERVER(inode),
3177 _nfs4_proc_link(inode, dir, name),
3178 &exception);
3179 } while (exception.retry);
3180 return err;
3181}
3182
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003183struct nfs4_createdata {
3184 struct rpc_message msg;
3185 struct nfs4_create_arg arg;
3186 struct nfs4_create_res res;
3187 struct nfs_fh fh;
3188 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003189};
3190
3191static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3192 struct qstr *name, struct iattr *sattr, u32 ftype)
3193{
3194 struct nfs4_createdata *data;
3195
3196 data = kzalloc(sizeof(*data), GFP_KERNEL);
3197 if (data != NULL) {
3198 struct nfs_server *server = NFS_SERVER(dir);
3199
3200 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3201 data->msg.rpc_argp = &data->arg;
3202 data->msg.rpc_resp = &data->res;
3203 data->arg.dir_fh = NFS_FH(dir);
3204 data->arg.server = server;
3205 data->arg.name = name;
3206 data->arg.attrs = sattr;
3207 data->arg.ftype = ftype;
3208 data->arg.bitmask = server->attr_bitmask;
3209 data->res.server = server;
3210 data->res.fh = &data->fh;
3211 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003212 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003213 }
3214 return data;
3215}
3216
3217static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3218{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003219 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003220 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003221 if (status == 0) {
3222 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003223 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3224 }
3225 return status;
3226}
3227
3228static void nfs4_free_createdata(struct nfs4_createdata *data)
3229{
3230 kfree(data);
3231}
3232
Chuck Lever4f390c12006-08-22 20:06:22 -04003233static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003234 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003236 struct nfs4_createdata *data;
3237 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Chuck Lever94a6d752006-08-22 20:06:23 -04003239 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003240 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003241
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003242 status = -ENOMEM;
3243 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3244 if (data == NULL)
3245 goto out;
3246
3247 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3248 data->arg.u.symlink.pages = &page;
3249 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003251 status = nfs4_do_create(dir, dentry, data);
3252
3253 nfs4_free_createdata(data);
3254out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 return status;
3256}
3257
Chuck Lever4f390c12006-08-22 20:06:22 -04003258static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003259 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
3261 struct nfs4_exception exception = { };
3262 int err;
3263 do {
3264 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003265 _nfs4_proc_symlink(dir, dentry, page,
3266 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 &exception);
3268 } while (exception.retry);
3269 return err;
3270}
3271
3272static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3273 struct iattr *sattr)
3274{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003275 struct nfs4_createdata *data;
3276 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003278 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3279 if (data == NULL)
3280 goto out;
3281
3282 status = nfs4_do_create(dir, dentry, data);
3283
3284 nfs4_free_createdata(data);
3285out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 return status;
3287}
3288
3289static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3290 struct iattr *sattr)
3291{
3292 struct nfs4_exception exception = { };
3293 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003294
3295 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 do {
3297 err = nfs4_handle_exception(NFS_SERVER(dir),
3298 _nfs4_proc_mkdir(dir, dentry, sattr),
3299 &exception);
3300 } while (exception.retry);
3301 return err;
3302}
3303
3304static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003305 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
3307 struct inode *dir = dentry->d_inode;
3308 struct nfs4_readdir_arg args = {
3309 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003310 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 .pgbase = 0,
3312 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003313 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003314 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 };
3316 struct nfs4_readdir_res res;
3317 struct rpc_message msg = {
3318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3319 .rpc_argp = &args,
3320 .rpc_resp = &res,
3321 .rpc_cred = cred,
3322 };
3323 int status;
3324
Harvey Harrison3110ff82008-05-02 13:42:44 -07003325 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003326 dentry->d_parent->d_name.name,
3327 dentry->d_name.name,
3328 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003329 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003331 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 -05003332 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003333 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003334 status += args.pgbase;
3335 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003336
3337 nfs_invalidate_atime(dir);
3338
Harvey Harrison3110ff82008-05-02 13:42:44 -07003339 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return status;
3341}
3342
3343static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003344 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345{
3346 struct nfs4_exception exception = { };
3347 int err;
3348 do {
3349 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3350 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003351 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 &exception);
3353 } while (exception.retry);
3354 return err;
3355}
3356
3357static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3358 struct iattr *sattr, dev_t rdev)
3359{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003360 struct nfs4_createdata *data;
3361 int mode = sattr->ia_mode;
3362 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003364 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3365 if (data == NULL)
3366 goto out;
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003369 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003371 data->arg.ftype = NF4BLK;
3372 data->arg.u.device.specdata1 = MAJOR(rdev);
3373 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
3375 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003376 data->arg.ftype = NF4CHR;
3377 data->arg.u.device.specdata1 = MAJOR(rdev);
3378 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003379 } else if (!S_ISSOCK(mode)) {
3380 status = -EINVAL;
3381 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003384 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003385out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003386 nfs4_free_createdata(data);
3387out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 return status;
3389}
3390
3391static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3392 struct iattr *sattr, dev_t rdev)
3393{
3394 struct nfs4_exception exception = { };
3395 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003396
3397 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 do {
3399 err = nfs4_handle_exception(NFS_SERVER(dir),
3400 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3401 &exception);
3402 } while (exception.retry);
3403 return err;
3404}
3405
3406static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3407 struct nfs_fsstat *fsstat)
3408{
3409 struct nfs4_statfs_arg args = {
3410 .fh = fhandle,
3411 .bitmask = server->attr_bitmask,
3412 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003413 struct nfs4_statfs_res res = {
3414 .fsstat = fsstat,
3415 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 struct rpc_message msg = {
3417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3418 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003419 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 };
3421
Trond Myklebust0e574af2005-10-27 22:12:38 -04003422 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003423 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
3425
3426static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3427{
3428 struct nfs4_exception exception = { };
3429 int err;
3430 do {
3431 err = nfs4_handle_exception(server,
3432 _nfs4_proc_statfs(server, fhandle, fsstat),
3433 &exception);
3434 } while (exception.retry);
3435 return err;
3436}
3437
3438static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3439 struct nfs_fsinfo *fsinfo)
3440{
3441 struct nfs4_fsinfo_arg args = {
3442 .fh = fhandle,
3443 .bitmask = server->attr_bitmask,
3444 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003445 struct nfs4_fsinfo_res res = {
3446 .fsinfo = fsinfo,
3447 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 struct rpc_message msg = {
3449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3450 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003451 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 };
3453
Bryan Schumaker7c513052011-03-24 17:12:24 +00003454 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455}
3456
3457static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3458{
3459 struct nfs4_exception exception = { };
3460 int err;
3461
3462 do {
3463 err = nfs4_handle_exception(server,
3464 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3465 &exception);
3466 } while (exception.retry);
3467 return err;
3468}
3469
3470static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3471{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003472 int error;
3473
Trond Myklebust0e574af2005-10-27 22:12:38 -04003474 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003475 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003476 if (error == 0) {
3477 /* block layout checks this! */
3478 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003479 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003480 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003481
3482 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
3485static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3486 struct nfs_pathconf *pathconf)
3487{
3488 struct nfs4_pathconf_arg args = {
3489 .fh = fhandle,
3490 .bitmask = server->attr_bitmask,
3491 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003492 struct nfs4_pathconf_res res = {
3493 .pathconf = pathconf,
3494 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 struct rpc_message msg = {
3496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3497 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003498 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 };
3500
3501 /* None of the pathconf attributes are mandatory to implement */
3502 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3503 memset(pathconf, 0, sizeof(*pathconf));
3504 return 0;
3505 }
3506
Trond Myklebust0e574af2005-10-27 22:12:38 -04003507 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003508 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509}
3510
3511static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3512 struct nfs_pathconf *pathconf)
3513{
3514 struct nfs4_exception exception = { };
3515 int err;
3516
3517 do {
3518 err = nfs4_handle_exception(server,
3519 _nfs4_proc_pathconf(server, fhandle, pathconf),
3520 &exception);
3521 } while (exception.retry);
3522 return err;
3523}
3524
Benny Halevyd20581a2011-05-22 19:52:03 +03003525void __nfs4_read_done_cb(struct nfs_read_data *data)
3526{
Fred Isamancd841602012-04-20 14:47:44 -04003527 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003528}
3529
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003530static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
Fred Isamancd841602012-04-20 14:47:44 -04003532 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003534 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003535 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003536 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003538
Benny Halevyd20581a2011-05-22 19:52:03 +03003539 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003541 renew_lease(server, data->timestamp);
3542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543}
3544
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003545static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3546{
3547
3548 dprintk("--> %s\n", __func__);
3549
3550 if (!nfs4_sequence_done(task, &data->res.seq_res))
3551 return -EAGAIN;
3552
Benny Halevyd20581a2011-05-22 19:52:03 +03003553 return data->read_done_cb ? data->read_done_cb(task, data) :
3554 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003555}
3556
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003557static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003560 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003561 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003562 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003565static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3566{
Fred Isamancd841602012-04-20 14:47:44 -04003567 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003568 &data->args.seq_args,
3569 &data->res.seq_res,
3570 task))
3571 return;
3572 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573}
3574
Fred Isamanb029bc92011-03-03 15:13:42 +00003575static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576{
Fred Isamancd841602012-04-20 14:47:44 -04003577 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003579 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003580 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003581 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003583 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003585 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003586 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588}
3589
Fred Isamanb029bc92011-03-03 15:13:42 +00003590static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3591{
3592 if (!nfs4_sequence_done(task, &data->res.seq_res))
3593 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003594 return data->write_done_cb ? data->write_done_cb(task, data) :
3595 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003596}
3597
Trond Myklebust5a37f852012-04-28 14:55:16 -04003598static
3599bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003600{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003601 const struct nfs_pgio_header *hdr = data->header;
3602
3603 /* Don't request attributes for pNFS or O_DIRECT writes */
3604 if (data->ds_clp != NULL || hdr->dreq != NULL)
3605 return false;
3606 /* Otherwise, request attributes if and only if we don't hold
3607 * a delegation
3608 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003609 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003610}
Fred Isamana69aef12011-03-03 15:13:47 +00003611
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003612static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
Fred Isamancd841602012-04-20 14:47:44 -04003614 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003615
Trond Myklebust5a37f852012-04-28 14:55:16 -04003616 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003617 data->args.bitmask = NULL;
3618 data->res.fattr = NULL;
3619 } else
3620 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003621
Fred Isamanb029bc92011-03-03 15:13:42 +00003622 if (!data->write_done_cb)
3623 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003624 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 data->timestamp = jiffies;
3626
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003627 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003628 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629}
3630
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003631static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3632{
Fred Isamancd841602012-04-20 14:47:44 -04003633 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003634 &data->args.seq_args,
3635 &data->res.seq_res,
3636 task))
3637 return;
3638 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
Fred Isaman0b7c0152012-04-20 14:47:39 -04003641static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3642{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3644 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003645 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 return;
3648 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003649}
3650
Fred Isaman0b7c0152012-04-20 14:47:39 -04003651static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
3653 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003654
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003655 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003656 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003657 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660}
3661
Fred Isaman0b7c0152012-04-20 14:47:39 -04003662static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003663{
3664 if (!nfs4_sequence_done(task, &data->res.seq_res))
3665 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003666 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003667}
3668
Fred Isaman0b7c0152012-04-20 14:47:39 -04003669static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003671 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003672
Fred Isaman0b7c0152012-04-20 14:47:39 -04003673 if (data->commit_done_cb == NULL)
3674 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003675 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003676 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003677 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
3679
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003680struct nfs4_renewdata {
3681 struct nfs_client *client;
3682 unsigned long timestamp;
3683};
3684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685/*
3686 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3687 * standalone procedure for queueing an asynchronous RENEW.
3688 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003689static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003690{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003691 struct nfs4_renewdata *data = calldata;
3692 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003693
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003694 if (atomic_read(&clp->cl_count) > 1)
3695 nfs4_schedule_state_renewal(clp);
3696 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003697 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003698}
3699
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003700static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003702 struct nfs4_renewdata *data = calldata;
3703 struct nfs_client *clp = data->client;
3704 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
3706 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003707 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003708 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3709 return;
3710 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003711 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003712 return;
3713 }
3714 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003716 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003719static const struct rpc_call_ops nfs4_renew_ops = {
3720 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003721 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003722};
3723
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003724static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
3726 struct rpc_message msg = {
3727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3728 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003729 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003731 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003733 if (renew_flags == 0)
3734 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003735 if (!atomic_inc_not_zero(&clp->cl_count))
3736 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003737 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003738 if (data == NULL)
3739 return -ENOMEM;
3740 data->client = clp;
3741 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003743 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744}
3745
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003746static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{
3748 struct rpc_message msg = {
3749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3750 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003751 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 };
3753 unsigned long now = jiffies;
3754 int status;
3755
3756 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3757 if (status < 0)
3758 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003759 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return 0;
3761}
3762
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003763static inline int nfs4_server_supports_acls(struct nfs_server *server)
3764{
3765 return (server->caps & NFS_CAP_ACLS)
3766 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3767 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3768}
3769
Trond Myklebust21f498c2012-08-24 10:59:25 -04003770/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3771 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003772 * the stack.
3773 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003774#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003775
Neil Hormane9e3d722011-03-04 19:26:03 -05003776static int buf_to_pages_noslab(const void *buf, size_t buflen,
3777 struct page **pages, unsigned int *pgbase)
3778{
3779 struct page *newpage, **spages;
3780 int rc = 0;
3781 size_t len;
3782 spages = pages;
3783
3784 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003785 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003786 newpage = alloc_page(GFP_KERNEL);
3787
3788 if (newpage == NULL)
3789 goto unwind;
3790 memcpy(page_address(newpage), buf, len);
3791 buf += len;
3792 buflen -= len;
3793 *pages++ = newpage;
3794 rc++;
3795 } while (buflen != 0);
3796
3797 return rc;
3798
3799unwind:
3800 for(; rc > 0; rc--)
3801 __free_page(spages[rc-1]);
3802 return -ENOMEM;
3803}
3804
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003805struct nfs4_cached_acl {
3806 int cached;
3807 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003808 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003809};
3810
3811static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003812{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003813 struct nfs_inode *nfsi = NFS_I(inode);
3814
3815 spin_lock(&inode->i_lock);
3816 kfree(nfsi->nfs4_acl);
3817 nfsi->nfs4_acl = acl;
3818 spin_unlock(&inode->i_lock);
3819}
3820
3821static void nfs4_zap_acl_attr(struct inode *inode)
3822{
3823 nfs4_set_cached_acl(inode, NULL);
3824}
3825
3826static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3827{
3828 struct nfs_inode *nfsi = NFS_I(inode);
3829 struct nfs4_cached_acl *acl;
3830 int ret = -ENOENT;
3831
3832 spin_lock(&inode->i_lock);
3833 acl = nfsi->nfs4_acl;
3834 if (acl == NULL)
3835 goto out;
3836 if (buf == NULL) /* user is just asking for length */
3837 goto out_len;
3838 if (acl->cached == 0)
3839 goto out;
3840 ret = -ERANGE; /* see getxattr(2) man page */
3841 if (acl->len > buflen)
3842 goto out;
3843 memcpy(buf, acl->data, acl->len);
3844out_len:
3845 ret = acl->len;
3846out:
3847 spin_unlock(&inode->i_lock);
3848 return ret;
3849}
3850
Sachin Prabhu5794d212012-04-17 14:36:40 +01003851static 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 +00003852{
3853 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003854 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003855
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003856 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003857 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003858 if (acl == NULL)
3859 goto out;
3860 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003861 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003862 } else {
3863 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3864 if (acl == NULL)
3865 goto out;
3866 acl->cached = 0;
3867 }
3868 acl->len = acl_len;
3869out:
3870 nfs4_set_cached_acl(inode, acl);
3871}
3872
Andy Adamsonbf118a32011-12-07 11:55:27 -05003873/*
3874 * The getxattr API returns the required buffer length when called with a
3875 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3876 * the required buf. On a NULL buf, we send a page of data to the server
3877 * guessing that the ACL request can be serviced by a page. If so, we cache
3878 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3879 * the cache. If not so, we throw away the page, and cache the required
3880 * length. The next getxattr call will then produce another round trip to
3881 * the server, this time with the input buf of the required size.
3882 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003883static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003884{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003885 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003886 struct nfs_getaclargs args = {
3887 .fh = NFS_FH(inode),
3888 .acl_pages = pages,
3889 .acl_len = buflen,
3890 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003891 struct nfs_getaclres res = {
3892 .acl_len = buflen,
3893 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003894 struct rpc_message msg = {
3895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3896 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003897 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003898 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003899 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3900 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003901
Andy Adamsonbf118a32011-12-07 11:55:27 -05003902 /* As long as we're doing a round trip to the server anyway,
3903 * let's be prepared for a page of acl data. */
3904 if (npages == 0)
3905 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003906 if (npages > ARRAY_SIZE(pages))
3907 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003908
Andy Adamsonbf118a32011-12-07 11:55:27 -05003909 for (i = 0; i < npages; i++) {
3910 pages[i] = alloc_page(GFP_KERNEL);
3911 if (!pages[i])
3912 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003913 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003914
3915 /* for decoding across pages */
3916 res.acl_scratch = alloc_page(GFP_KERNEL);
3917 if (!res.acl_scratch)
3918 goto out_free;
3919
Andy Adamsonbf118a32011-12-07 11:55:27 -05003920 args.acl_len = npages * PAGE_SIZE;
3921 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003922
Peng Taode040be2012-01-10 22:42:47 +08003923 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003924 __func__, buf, buflen, npages, args.acl_len);
3925 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3926 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003927 if (ret)
3928 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003929
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003930 /* Handle the case where the passed-in buffer is too short */
3931 if (res.acl_flags & NFS4_ACL_TRUNC) {
3932 /* Did the user only issue a request for the acl length? */
3933 if (buf == NULL)
3934 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003935 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003936 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003937 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003938 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3939 if (buf)
3940 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3941out_ok:
3942 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003943out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003944 for (i = 0; i < npages; i++)
3945 if (pages[i])
3946 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003947 if (res.acl_scratch)
3948 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003949 return ret;
3950}
3951
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003952static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3953{
3954 struct nfs4_exception exception = { };
3955 ssize_t ret;
3956 do {
3957 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3958 if (ret >= 0)
3959 break;
3960 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3961 } while (exception.retry);
3962 return ret;
3963}
3964
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003965static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3966{
3967 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003968 int ret;
3969
3970 if (!nfs4_server_supports_acls(server))
3971 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003972 ret = nfs_revalidate_inode(server, inode);
3973 if (ret < 0)
3974 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003975 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3976 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003977 ret = nfs4_read_cached_acl(inode, buf, buflen);
3978 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003979 /* -ENOENT is returned if there is no ACL or if there is an ACL
3980 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003981 return ret;
3982 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003983}
3984
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003985static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003986{
3987 struct nfs_server *server = NFS_SERVER(inode);
3988 struct page *pages[NFS4ACL_MAXPAGES];
3989 struct nfs_setaclargs arg = {
3990 .fh = NFS_FH(inode),
3991 .acl_pages = pages,
3992 .acl_len = buflen,
3993 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003994 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003995 struct rpc_message msg = {
3996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3997 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003998 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003999 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004000 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004001 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004002
4003 if (!nfs4_server_supports_acls(server))
4004 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004005 if (npages > ARRAY_SIZE(pages))
4006 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004007 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4008 if (i < 0)
4009 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004010 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004011 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004012
4013 /*
4014 * Free each page after tx, so the only ref left is
4015 * held by the network stack
4016 */
4017 for (; i > 0; i--)
4018 put_page(pages[i-1]);
4019
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004020 /*
4021 * Acl update can result in inode attribute update.
4022 * so mark the attribute cache invalid.
4023 */
4024 spin_lock(&inode->i_lock);
4025 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4026 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004027 nfs_access_zap_cache(inode);
4028 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004029 return ret;
4030}
4031
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004032static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4033{
4034 struct nfs4_exception exception = { };
4035 int err;
4036 do {
4037 err = nfs4_handle_exception(NFS_SERVER(inode),
4038 __nfs4_proc_set_acl(inode, buf, buflen),
4039 &exception);
4040 } while (exception.retry);
4041 return err;
4042}
4043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004045nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004047 struct nfs_client *clp = server->nfs_client;
4048
4049 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 return 0;
4051 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004052 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004053 case -NFS4ERR_ADMIN_REVOKED:
4054 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004055 if (state == NULL)
4056 break;
4057 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004058 case -NFS4ERR_OPENMODE:
4059 if (state == NULL)
4060 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004061 nfs4_schedule_stateid_recovery(server, state);
4062 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004063 case -NFS4ERR_EXPIRED:
4064 if (state != NULL)
4065 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004067 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004068 nfs4_schedule_lease_recovery(clp);
4069 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004070#if defined(CONFIG_NFS_V4_1)
4071 case -NFS4ERR_BADSESSION:
4072 case -NFS4ERR_BADSLOT:
4073 case -NFS4ERR_BAD_HIGH_SLOT:
4074 case -NFS4ERR_DEADSESSION:
4075 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4076 case -NFS4ERR_SEQ_FALSE_RETRY:
4077 case -NFS4ERR_SEQ_MISORDERED:
4078 dprintk("%s ERROR %d, Reset session\n", __func__,
4079 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004080 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004081 task->tk_status = 0;
4082 return -EAGAIN;
4083#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004085 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004086 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004087 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4089 task->tk_status = 0;
4090 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004091 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 case -NFS4ERR_OLD_STATEID:
4093 task->tk_status = 0;
4094 return -EAGAIN;
4095 }
4096 task->tk_status = nfs4_map_errors(task->tk_status);
4097 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004098wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004099 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004100 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4101 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4102 task->tk_status = 0;
4103 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104}
4105
Chuck Leverf0920752012-05-21 22:45:41 -04004106static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4107 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004108{
4109 __be32 verf[2];
4110
Chuck Lever2c820d92012-05-21 22:45:33 -04004111 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4112 /* An impossible timestamp guarantees this value
4113 * will never match a generated boot time. */
4114 verf[0] = 0;
4115 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4116 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004117 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4118 verf[0] = (__be32)nn->boot_time.tv_sec;
4119 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004120 }
Chuck Levercd937102012-03-02 17:14:31 -05004121 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4122}
4123
Chuck Levere984a552012-09-14 17:24:21 -04004124static unsigned int
4125nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4126 char *buf, size_t len)
4127{
4128 unsigned int result;
4129
4130 rcu_read_lock();
4131 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4132 clp->cl_ipaddr,
4133 rpc_peeraddr2str(clp->cl_rpcclient,
4134 RPC_DISPLAY_ADDR),
4135 rpc_peeraddr2str(clp->cl_rpcclient,
4136 RPC_DISPLAY_PROTO));
4137 rcu_read_unlock();
4138 return result;
4139}
4140
4141static unsigned int
4142nfs4_init_uniform_client_string(const struct nfs_client *clp,
4143 char *buf, size_t len)
4144{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004145 char *nodename = clp->cl_rpcclient->cl_nodename;
4146
4147 if (nfs4_client_id_uniquifier[0] != '\0')
4148 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004149 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4150 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004151 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004152}
4153
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004154/**
4155 * nfs4_proc_setclientid - Negotiate client ID
4156 * @clp: state data structure
4157 * @program: RPC program for NFSv4 callback service
4158 * @port: IP port number for NFS4 callback service
4159 * @cred: RPC credential to use for this call
4160 * @res: where to place the result
4161 *
4162 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4163 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004164int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4165 unsigned short port, struct rpc_cred *cred,
4166 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
4168 nfs4_verifier sc_verifier;
4169 struct nfs4_setclientid setclientid = {
4170 .sc_verifier = &sc_verifier,
4171 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004172 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 };
4174 struct rpc_message msg = {
4175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4176 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004177 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004178 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004180 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Chuck Leverde734832012-07-11 16:30:50 -04004182 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004183 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004184 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4185 setclientid.sc_name_len =
4186 nfs4_init_uniform_client_string(clp,
4187 setclientid.sc_name,
4188 sizeof(setclientid.sc_name));
4189 else
4190 setclientid.sc_name_len =
4191 nfs4_init_nonuniform_client_string(clp,
4192 setclientid.sc_name,
4193 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004194 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004195 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004196 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004197 sizeof(setclientid.sc_netid),
4198 rpc_peeraddr2str(clp->cl_rpcclient,
4199 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004200 rcu_read_unlock();
4201 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004202 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 clp->cl_ipaddr, port >> 8, port & 255);
4204
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004205 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4206 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4207 setclientid.sc_name_len, setclientid.sc_name);
4208 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4209 dprintk("NFS reply setclientid: %d\n", status);
4210 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211}
4212
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004213/**
4214 * nfs4_proc_setclientid_confirm - Confirm client ID
4215 * @clp: state data structure
4216 * @res: result of a previous SETCLIENTID
4217 * @cred: RPC credential to use for this call
4218 *
4219 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4220 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004221int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004222 struct nfs4_setclientid_res *arg,
4223 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
4225 struct nfs_fsinfo fsinfo;
4226 struct rpc_message msg = {
4227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004228 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004230 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 };
4232 unsigned long now;
4233 int status;
4234
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004235 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4236 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4237 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004239 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 if (status == 0) {
4241 spin_lock(&clp->cl_lock);
4242 clp->cl_lease_time = fsinfo.lease_time * HZ;
4243 clp->cl_last_renewal = now;
4244 spin_unlock(&clp->cl_lock);
4245 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004246 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 return status;
4248}
4249
Trond Myklebustfe650402006-01-03 09:55:18 +01004250struct nfs4_delegreturndata {
4251 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004252 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004253 struct nfs_fh fh;
4254 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004255 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004256 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004257 int rpc_status;
4258};
4259
Trond Myklebustfe650402006-01-03 09:55:18 +01004260static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4261{
4262 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004263
Trond Myklebust14516c32010-07-31 14:29:06 -04004264 if (!nfs4_sequence_done(task, &data->res.seq_res))
4265 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004266
Ricardo Labiaga79708862009-12-07 09:23:21 -05004267 switch (task->tk_status) {
4268 case -NFS4ERR_STALE_STATEID:
4269 case -NFS4ERR_EXPIRED:
4270 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004271 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004272 break;
4273 default:
4274 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4275 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004276 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004277 return;
4278 }
4279 }
4280 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004281}
4282
4283static void nfs4_delegreturn_release(void *calldata)
4284{
Trond Myklebustfe650402006-01-03 09:55:18 +01004285 kfree(calldata);
4286}
4287
Andy Adamson938e1012009-04-01 09:22:28 -04004288#if defined(CONFIG_NFS_V4_1)
4289static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4290{
4291 struct nfs4_delegreturndata *d_data;
4292
4293 d_data = (struct nfs4_delegreturndata *)data;
4294
Trond Myklebust035168a2010-06-16 09:52:26 -04004295 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004296 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004297 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004298 return;
4299 rpc_call_start(task);
4300}
4301#endif /* CONFIG_NFS_V4_1 */
4302
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004303static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004304#if defined(CONFIG_NFS_V4_1)
4305 .rpc_call_prepare = nfs4_delegreturn_prepare,
4306#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004307 .rpc_call_done = nfs4_delegreturn_done,
4308 .rpc_release = nfs4_delegreturn_release,
4309};
4310
Trond Myklebuste6f81072008-01-24 18:14:34 -05004311static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004312{
4313 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004314 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004315 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004316 struct rpc_message msg = {
4317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4318 .rpc_cred = cred,
4319 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004320 struct rpc_task_setup task_setup_data = {
4321 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004322 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004323 .callback_ops = &nfs4_delegreturn_ops,
4324 .flags = RPC_TASK_ASYNC,
4325 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004326 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004327
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004328 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004329 if (data == NULL)
4330 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004331 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004332 data->args.fhandle = &data->fh;
4333 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004334 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004335 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004336 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004337 data->res.fattr = &data->fattr;
4338 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004339 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004340 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004341 data->rpc_status = 0;
4342
Trond Myklebustc970aa82007-07-14 15:39:59 -04004343 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004344 msg.rpc_argp = &data->args;
4345 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004346 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004347 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004348 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004349 if (!issync)
4350 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004351 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004352 if (status != 0)
4353 goto out;
4354 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004355 if (status == 0)
4356 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4357 else
4358 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004359out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004360 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004361 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362}
4363
Trond Myklebuste6f81072008-01-24 18:14:34 -05004364int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365{
4366 struct nfs_server *server = NFS_SERVER(inode);
4367 struct nfs4_exception exception = { };
4368 int err;
4369 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004370 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 switch (err) {
4372 case -NFS4ERR_STALE_STATEID:
4373 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 case 0:
4375 return 0;
4376 }
4377 err = nfs4_handle_exception(server, err, &exception);
4378 } while (exception.retry);
4379 return err;
4380}
4381
4382#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4383#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4384
4385/*
4386 * sleep, with exponential backoff, and retry the LOCK operation.
4387 */
4388static unsigned long
4389nfs4_set_lock_task_retry(unsigned long timeout)
4390{
Jeff Laytond3103102011-12-01 22:44:39 +01004391 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 timeout <<= 1;
4393 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4394 return NFS4_LOCK_MAXTIMEOUT;
4395 return timeout;
4396}
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4399{
4400 struct inode *inode = state->inode;
4401 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004402 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004403 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004405 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004407 struct nfs_lockt_res res = {
4408 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 };
4410 struct rpc_message msg = {
4411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4412 .rpc_argp = &arg,
4413 .rpc_resp = &res,
4414 .rpc_cred = state->owner->so_cred,
4415 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 struct nfs4_lock_state *lsp;
4417 int status;
4418
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004419 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004420 status = nfs4_set_lock_state(state, request);
4421 if (status != 0)
4422 goto out;
4423 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004424 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004425 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004426 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004427 switch (status) {
4428 case 0:
4429 request->fl_type = F_UNLCK;
4430 break;
4431 case -NFS4ERR_DENIED:
4432 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004434 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004435out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return status;
4437}
4438
4439static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4440{
4441 struct nfs4_exception exception = { };
4442 int err;
4443
4444 do {
4445 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4446 _nfs4_proc_getlk(state, cmd, request),
4447 &exception);
4448 } while (exception.retry);
4449 return err;
4450}
4451
4452static int do_vfs_lock(struct file *file, struct file_lock *fl)
4453{
4454 int res = 0;
4455 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4456 case FL_POSIX:
4457 res = posix_lock_file_wait(file, fl);
4458 break;
4459 case FL_FLOCK:
4460 res = flock_lock_file_wait(file, fl);
4461 break;
4462 default:
4463 BUG();
4464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 return res;
4466}
4467
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004468struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004469 struct nfs_locku_args arg;
4470 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004471 struct nfs4_lock_state *lsp;
4472 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004473 struct file_lock fl;
4474 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004475 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004476};
4477
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004478static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4479 struct nfs_open_context *ctx,
4480 struct nfs4_lock_state *lsp,
4481 struct nfs_seqid *seqid)
4482{
4483 struct nfs4_unlockdata *p;
4484 struct inode *inode = lsp->ls_state->inode;
4485
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004486 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004487 if (p == NULL)
4488 return NULL;
4489 p->arg.fh = NFS_FH(inode);
4490 p->arg.fl = &p->fl;
4491 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004492 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004493 p->arg.stateid = &lsp->ls_stateid;
4494 p->lsp = lsp;
4495 atomic_inc(&lsp->ls_count);
4496 /* Ensure we don't close file until we're done freeing locks! */
4497 p->ctx = get_nfs_open_context(ctx);
4498 memcpy(&p->fl, fl, sizeof(p->fl));
4499 p->server = NFS_SERVER(inode);
4500 return p;
4501}
4502
Trond Myklebust06f814a2006-01-03 09:55:07 +01004503static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004504{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004505 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004506 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004507 nfs4_put_lock_state(calldata->lsp);
4508 put_nfs_open_context(calldata->ctx);
4509 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004510}
4511
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004512static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004513{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004514 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004515
Trond Myklebust14516c32010-07-31 14:29:06 -04004516 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4517 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004518 switch (task->tk_status) {
4519 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004520 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4521 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004522 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004523 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004524 case -NFS4ERR_BAD_STATEID:
4525 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004526 case -NFS4ERR_STALE_STATEID:
4527 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004528 break;
4529 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004530 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004531 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004532 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004533 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004534}
4535
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004536static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004537{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004538 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004540 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004541 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004542 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004543 /* Note: exit _without_ running nfs4_locku_done */
4544 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004545 return;
4546 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004547 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004548 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004549 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004550 &calldata->res.seq_res,
4551 task) != 0)
4552 nfs_release_seqid(calldata->arg.seqid);
4553 else
4554 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555}
4556
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004557static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004558 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004559 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004560 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004561};
4562
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004563static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4564 struct nfs_open_context *ctx,
4565 struct nfs4_lock_state *lsp,
4566 struct nfs_seqid *seqid)
4567{
4568 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004569 struct rpc_message msg = {
4570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4571 .rpc_cred = ctx->cred,
4572 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004573 struct rpc_task_setup task_setup_data = {
4574 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004575 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004576 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004577 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004578 .flags = RPC_TASK_ASYNC,
4579 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004580
Frank Filz137d6ac2007-07-09 15:32:29 -07004581 /* Ensure this is an unlock - when canceling a lock, the
4582 * canceled lock is passed in, and it won't be an unlock.
4583 */
4584 fl->fl_type = F_UNLCK;
4585
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004586 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4587 if (data == NULL) {
4588 nfs_free_seqid(seqid);
4589 return ERR_PTR(-ENOMEM);
4590 }
4591
Trond Myklebust9d12b212012-01-17 22:04:25 -05004592 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004593 msg.rpc_argp = &data->arg;
4594 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004595 task_setup_data.callback_data = data;
4596 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004597}
4598
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4600{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004601 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004602 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004603 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004604 struct rpc_task *task;
4605 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004606 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
Trond Myklebust9b073572006-06-29 16:38:34 -04004608 status = nfs4_set_lock_state(state, request);
4609 /* Unlock _before_ we do the RPC call */
4610 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004611 down_read(&nfsi->rwsem);
4612 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4613 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004614 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004615 }
4616 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004617 if (status != 0)
4618 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004619 /* Is this a delegated lock? */
4620 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004621 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004622 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004623 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004624 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004625 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004626 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004627 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004628 status = PTR_ERR(task);
4629 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004630 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004631 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004632 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004633out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004634 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004635 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636}
4637
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004638struct nfs4_lockdata {
4639 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004640 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004641 struct nfs4_lock_state *lsp;
4642 struct nfs_open_context *ctx;
4643 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004644 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004645 int rpc_status;
4646 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004647 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004648};
4649
4650static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004651 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4652 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004653{
4654 struct nfs4_lockdata *p;
4655 struct inode *inode = lsp->ls_state->inode;
4656 struct nfs_server *server = NFS_SERVER(inode);
4657
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004658 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004659 if (p == NULL)
4660 return NULL;
4661
4662 p->arg.fh = NFS_FH(inode);
4663 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004664 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004665 if (p->arg.open_seqid == NULL)
4666 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004667 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004668 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004669 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004670 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004671 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004672 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004673 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004674 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004675 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004676 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004677 atomic_inc(&lsp->ls_count);
4678 p->ctx = get_nfs_open_context(ctx);
4679 memcpy(&p->fl, fl, sizeof(p->fl));
4680 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004681out_free_seqid:
4682 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004683out_free:
4684 kfree(p);
4685 return NULL;
4686}
4687
4688static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4689{
4690 struct nfs4_lockdata *data = calldata;
4691 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
Harvey Harrison3110ff82008-05-02 13:42:44 -07004693 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004694 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4695 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004696 /* Do we need to do an open_to_lock_owner? */
4697 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004698 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004699 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004700 data->arg.open_stateid = &state->stateid;
4701 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004702 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004703 } else
4704 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004705 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004706 if (nfs4_setup_sequence(data->server,
4707 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004708 &data->res.seq_res,
4709 task) == 0) {
4710 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004711 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004712 }
4713 nfs_release_seqid(data->arg.open_seqid);
4714out_release_lock_seqid:
4715 nfs_release_seqid(data->arg.lock_seqid);
4716 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004717}
4718
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004719static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4720{
4721 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4722 nfs4_lock_prepare(task, calldata);
4723}
4724
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004725static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4726{
4727 struct nfs4_lockdata *data = calldata;
4728
Harvey Harrison3110ff82008-05-02 13:42:44 -07004729 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004730
Trond Myklebust14516c32010-07-31 14:29:06 -04004731 if (!nfs4_sequence_done(task, &data->res.seq_res))
4732 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004733
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004734 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004735 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004736 if (data->rpc_status == 0)
4737 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4738 else
4739 goto out;
4740 }
4741 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004742 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004743 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004744 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004745 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004746out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004747 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004748}
4749
4750static void nfs4_lock_release(void *calldata)
4751{
4752 struct nfs4_lockdata *data = calldata;
4753
Harvey Harrison3110ff82008-05-02 13:42:44 -07004754 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004755 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004756 if (data->cancelled != 0) {
4757 struct rpc_task *task;
4758 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4759 data->arg.lock_seqid);
4760 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004761 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004762 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004763 } else
4764 nfs_free_seqid(data->arg.lock_seqid);
4765 nfs4_put_lock_state(data->lsp);
4766 put_nfs_open_context(data->ctx);
4767 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004768 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004769}
4770
4771static const struct rpc_call_ops nfs4_lock_ops = {
4772 .rpc_call_prepare = nfs4_lock_prepare,
4773 .rpc_call_done = nfs4_lock_done,
4774 .rpc_release = nfs4_lock_release,
4775};
4776
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004777static const struct rpc_call_ops nfs4_recover_lock_ops = {
4778 .rpc_call_prepare = nfs4_recover_lock_prepare,
4779 .rpc_call_done = nfs4_lock_done,
4780 .rpc_release = nfs4_lock_release,
4781};
4782
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004783static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4784{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004785 switch (error) {
4786 case -NFS4ERR_ADMIN_REVOKED:
4787 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004788 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004789 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004790 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004791 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004792 break;
4793 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004794 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004795 case -NFS4ERR_EXPIRED:
4796 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004797 };
4798}
4799
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004800static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004801{
4802 struct nfs4_lockdata *data;
4803 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004804 struct rpc_message msg = {
4805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4806 .rpc_cred = state->owner->so_cred,
4807 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004808 struct rpc_task_setup task_setup_data = {
4809 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004810 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004811 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004812 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004813 .flags = RPC_TASK_ASYNC,
4814 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004815 int ret;
4816
Harvey Harrison3110ff82008-05-02 13:42:44 -07004817 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004818 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004819 fl->fl_u.nfs4_fl.owner,
4820 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004821 if (data == NULL)
4822 return -ENOMEM;
4823 if (IS_SETLKW(cmd))
4824 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004825 if (recovery_type > NFS_LOCK_NEW) {
4826 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004827 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004828 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4829 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004830 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004831 msg.rpc_argp = &data->arg;
4832 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004833 task_setup_data.callback_data = data;
4834 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004835 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004836 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004837 ret = nfs4_wait_for_completion_rpc_task(task);
4838 if (ret == 0) {
4839 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004840 if (ret)
4841 nfs4_handle_setlk_error(data->server, data->lsp,
4842 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004843 } else
4844 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004845 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004846 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004847 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848}
4849
4850static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4851{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004852 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004853 struct nfs4_exception exception = {
4854 .inode = state->inode,
4855 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004856 int err;
4857
4858 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004859 /* Cache the lock if possible... */
4860 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4861 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004862 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004863 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004864 break;
4865 nfs4_handle_exception(server, err, &exception);
4866 } while (exception.retry);
4867 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868}
4869
4870static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4871{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004872 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004873 struct nfs4_exception exception = {
4874 .inode = state->inode,
4875 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004876 int err;
4877
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004878 err = nfs4_set_lock_state(state, request);
4879 if (err != 0)
4880 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004881 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004882 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4883 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004884 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004885 switch (err) {
4886 default:
4887 goto out;
4888 case -NFS4ERR_GRACE:
4889 case -NFS4ERR_DELAY:
4890 nfs4_handle_exception(server, err, &exception);
4891 err = 0;
4892 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004893 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004894out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004895 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896}
4897
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004898#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004899/**
4900 * nfs41_check_expired_locks - possibly free a lock stateid
4901 *
4902 * @state: NFSv4 state for an inode
4903 *
4904 * Returns NFS_OK if recovery for this stateid is now finished.
4905 * Otherwise a negative NFS4ERR value is returned.
4906 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004907static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004908{
Chuck Levereb64cf92012-07-11 16:30:05 -04004909 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004910 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004911 struct nfs_server *server = NFS_SERVER(state->inode);
4912
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004913 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004914 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004915 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4916 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004917 /* Free the stateid unless the server
4918 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004919 if (status != -NFS4ERR_BAD_STATEID)
4920 nfs41_free_stateid(server,
4921 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004922 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004923 ret = status;
4924 }
4925 }
4926 };
4927
4928 return ret;
4929}
4930
4931static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4932{
4933 int status = NFS_OK;
4934
4935 if (test_bit(LK_STATE_IN_USE, &state->flags))
4936 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004937 if (status != NFS_OK)
4938 status = nfs4_lock_expired(state, request);
4939 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004940}
4941#endif
4942
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4944{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004945 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004946 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004947 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004949 if ((fl_flags & FL_POSIX) &&
4950 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4951 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004952 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004953 status = nfs4_set_lock_state(state, request);
4954 if (status != 0)
4955 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004956 request->fl_flags |= FL_ACCESS;
4957 status = do_vfs_lock(request->fl_file, request);
4958 if (status < 0)
4959 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004960 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004961 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004962 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004963 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004964 request->fl_flags = fl_flags & ~FL_SLEEP;
4965 status = do_vfs_lock(request->fl_file, request);
4966 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004967 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004968 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004969 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004970 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004971 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004972 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004973 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004974 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4975 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004976out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004977 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004978out:
4979 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 return status;
4981}
4982
4983static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4984{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004985 struct nfs4_exception exception = {
4986 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004987 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004988 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 int err;
4990
4991 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004992 err = _nfs4_proc_setlk(state, cmd, request);
4993 if (err == -NFS4ERR_DENIED)
4994 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004996 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 } while (exception.retry);
4998 return err;
4999}
5000
5001static int
5002nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5003{
5004 struct nfs_open_context *ctx;
5005 struct nfs4_state *state;
5006 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5007 int status;
5008
5009 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005010 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 state = ctx->state;
5012
5013 if (request->fl_start < 0 || request->fl_end < 0)
5014 return -EINVAL;
5015
Trond Myklebustd9531262009-07-21 19:22:38 -04005016 if (IS_GETLK(cmd)) {
5017 if (state != NULL)
5018 return nfs4_proc_getlk(state, F_GETLK, request);
5019 return 0;
5020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
5022 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5023 return -EINVAL;
5024
Trond Myklebustd9531262009-07-21 19:22:38 -04005025 if (request->fl_type == F_UNLCK) {
5026 if (state != NULL)
5027 return nfs4_proc_unlck(state, cmd, request);
5028 return 0;
5029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Trond Myklebustd9531262009-07-21 19:22:38 -04005031 if (state == NULL)
5032 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005033 /*
5034 * Don't rely on the VFS having checked the file open mode,
5035 * since it won't do this for flock() locks.
5036 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005037 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005038 case F_RDLCK:
5039 if (!(filp->f_mode & FMODE_READ))
5040 return -EBADF;
5041 break;
5042 case F_WRLCK:
5043 if (!(filp->f_mode & FMODE_WRITE))
5044 return -EBADF;
5045 }
5046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 do {
5048 status = nfs4_proc_setlk(state, cmd, request);
5049 if ((status != -EAGAIN) || IS_SETLK(cmd))
5050 break;
5051 timeout = nfs4_set_lock_task_retry(timeout);
5052 status = -ERESTARTSYS;
5053 if (signalled())
5054 break;
5055 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 return status;
5057}
5058
Trond Myklebust888e6942005-11-04 15:38:11 -05005059int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5060{
5061 struct nfs_server *server = NFS_SERVER(state->inode);
5062 struct nfs4_exception exception = { };
5063 int err;
5064
5065 err = nfs4_set_lock_state(state, fl);
5066 if (err != 0)
5067 goto out;
5068 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005069 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005070 switch (err) {
5071 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005072 printk(KERN_ERR "NFS: %s: unhandled error "
5073 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005074 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005075 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005076 goto out;
5077 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005078 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005079 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005080 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005081 nfs4_schedule_lease_recovery(server->nfs_client);
5082 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005083 case -NFS4ERR_BADSESSION:
5084 case -NFS4ERR_BADSLOT:
5085 case -NFS4ERR_BAD_HIGH_SLOT:
5086 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5087 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005088 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005089 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005090 case -ERESTARTSYS:
5091 /*
5092 * The show must go on: exit, but mark the
5093 * stateid as needing recovery.
5094 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005095 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005096 case -NFS4ERR_ADMIN_REVOKED:
5097 case -NFS4ERR_BAD_STATEID:
5098 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005099 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005100 err = 0;
5101 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005102 case -EKEYEXPIRED:
5103 /*
5104 * User RPCSEC_GSS context has expired.
5105 * We cannot recover this stateid now, so
5106 * skip it and allow recovery thread to
5107 * proceed.
5108 */
5109 err = 0;
5110 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005111 case -ENOMEM:
5112 case -NFS4ERR_DENIED:
5113 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5114 err = 0;
5115 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005116 case -NFS4ERR_DELAY:
5117 break;
5118 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005119 err = nfs4_handle_exception(server, err, &exception);
5120 } while (exception.retry);
5121out:
5122 return err;
5123}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005124
Trond Myklebustcf470c32012-03-07 13:49:12 -05005125struct nfs_release_lockowner_data {
5126 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005127 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005128 struct nfs_release_lockowner_args args;
5129};
5130
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005131static void nfs4_release_lockowner_release(void *calldata)
5132{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005133 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005134 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005135 kfree(calldata);
5136}
5137
Trond Myklebust17280172012-03-11 13:11:00 -04005138static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005139 .rpc_release = nfs4_release_lockowner_release,
5140};
5141
Trond Myklebustcf470c32012-03-07 13:49:12 -05005142int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005143{
5144 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005145 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005146 struct rpc_message msg = {
5147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5148 };
5149
5150 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005151 return -EINVAL;
5152 data = kmalloc(sizeof(*data), GFP_NOFS);
5153 if (!data)
5154 return -ENOMEM;
5155 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005156 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005157 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5158 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5159 data->args.lock_owner.s_dev = server->s_dev;
5160 msg.rpc_argp = &data->args;
5161 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5162 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005163}
5164
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005165#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5166
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005167static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5168 const void *buf, size_t buflen,
5169 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005170{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005171 if (strcmp(key, "") != 0)
5172 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005173
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005174 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005175}
5176
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005177static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5178 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005179{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005180 if (strcmp(key, "") != 0)
5181 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005182
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005183 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005184}
5185
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005186static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5187 size_t list_len, const char *name,
5188 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005189{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005190 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005191
J. Bruce Fields096455a2006-03-20 23:23:42 -05005192 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5193 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005194
5195 if (list && len <= list_len)
5196 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005197 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005198}
5199
Andy Adamson533eb462011-06-13 18:25:56 -04005200/*
5201 * nfs_fhget will use either the mounted_on_fileid or the fileid
5202 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005203static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5204{
Andy Adamson533eb462011-06-13 18:25:56 -04005205 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5206 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5207 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005208 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005209 return;
5210
5211 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005212 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005213 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5214 fattr->nlink = 2;
5215}
5216
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005217static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5218 const struct qstr *name,
5219 struct nfs4_fs_locations *fs_locations,
5220 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005221{
5222 struct nfs_server *server = NFS_SERVER(dir);
5223 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005224 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005225 };
5226 struct nfs4_fs_locations_arg args = {
5227 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005228 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005229 .page = page,
5230 .bitmask = bitmask,
5231 };
Benny Halevy22958462009-04-01 09:22:02 -04005232 struct nfs4_fs_locations_res res = {
5233 .fs_locations = fs_locations,
5234 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005235 struct rpc_message msg = {
5236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5237 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005238 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005239 };
5240 int status;
5241
Harvey Harrison3110ff82008-05-02 13:42:44 -07005242 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005243
5244 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5245 * is not supported */
5246 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5247 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5248 else
5249 bitmask[0] |= FATTR4_WORD0_FILEID;
5250
Trond Myklebustc228fd32007-01-13 02:28:11 -05005251 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005252 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005253 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005254 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005255 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005256 return status;
5257}
5258
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005259int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5260 const struct qstr *name,
5261 struct nfs4_fs_locations *fs_locations,
5262 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005263{
5264 struct nfs4_exception exception = { };
5265 int err;
5266 do {
5267 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005268 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005269 &exception);
5270 } while (exception.retry);
5271 return err;
5272}
5273
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005274static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5275{
5276 int status;
5277 struct nfs4_secinfo_arg args = {
5278 .dir_fh = NFS_FH(dir),
5279 .name = name,
5280 };
5281 struct nfs4_secinfo_res res = {
5282 .flavors = flavors,
5283 };
5284 struct rpc_message msg = {
5285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5286 .rpc_argp = &args,
5287 .rpc_resp = &res,
5288 };
5289
5290 dprintk("NFS call secinfo %s\n", name->name);
5291 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5292 dprintk("NFS reply secinfo: %d\n", status);
5293 return status;
5294}
5295
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005296int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5297 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005298{
5299 struct nfs4_exception exception = { };
5300 int err;
5301 do {
5302 err = nfs4_handle_exception(NFS_SERVER(dir),
5303 _nfs4_proc_secinfo(dir, name, flavors),
5304 &exception);
5305 } while (exception.retry);
5306 return err;
5307}
5308
Andy Adamson557134a2009-04-01 09:21:53 -04005309#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005310/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005311 * Check the exchange flags returned by the server for invalid flags, having
5312 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5313 * DS flags set.
5314 */
5315static int nfs4_check_cl_exchange_flags(u32 flags)
5316{
5317 if (flags & ~EXCHGID4_FLAG_MASK_R)
5318 goto out_inval;
5319 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5320 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5321 goto out_inval;
5322 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5323 goto out_inval;
5324 return NFS_OK;
5325out_inval:
5326 return -NFS4ERR_INVAL;
5327}
5328
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005329static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005330nfs41_same_server_scope(struct nfs41_server_scope *a,
5331 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005332{
5333 if (a->server_scope_sz == b->server_scope_sz &&
5334 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5335 return true;
5336
5337 return false;
5338}
5339
Andy Adamson357f54d2010-12-14 10:11:57 -05005340/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005341 * nfs4_proc_bind_conn_to_session()
5342 *
5343 * The 4.1 client currently uses the same TCP connection for the
5344 * fore and backchannel.
5345 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005346int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005347{
5348 int status;
5349 struct nfs41_bind_conn_to_session_res res;
5350 struct rpc_message msg = {
5351 .rpc_proc =
5352 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5353 .rpc_argp = clp,
5354 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005355 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005356 };
5357
5358 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005359
5360 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5361 if (unlikely(res.session == NULL)) {
5362 status = -ENOMEM;
5363 goto out;
5364 }
5365
5366 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5367 if (status == 0) {
5368 if (memcmp(res.session->sess_id.data,
5369 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5370 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5371 status = -EIO;
5372 goto out_session;
5373 }
5374 if (res.dir != NFS4_CDFS4_BOTH) {
5375 dprintk("NFS: %s: Unexpected direction from server\n",
5376 __func__);
5377 status = -EIO;
5378 goto out_session;
5379 }
5380 if (res.use_conn_in_rdma_mode) {
5381 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5382 __func__);
5383 status = -EIO;
5384 goto out_session;
5385 }
5386 }
5387out_session:
5388 kfree(res.session);
5389out:
5390 dprintk("<-- %s status= %d\n", __func__, status);
5391 return status;
5392}
5393
5394/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005395 * nfs4_proc_exchange_id()
5396 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005397 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5398 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005399 * Since the clientid has expired, all compounds using sessions
5400 * associated with the stale clientid will be returning
5401 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5402 * be in some phase of session reset.
5403 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005404int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005405{
5406 nfs4_verifier verifier;
5407 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005408 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005409 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005410 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005411 };
5412 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005413 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005414 };
5415 int status;
5416 struct rpc_message msg = {
5417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5418 .rpc_argp = &args,
5419 .rpc_resp = &res,
5420 .rpc_cred = cred,
5421 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005422
Chuck Leverf0920752012-05-21 22:45:41 -04005423 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005424 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5425 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005426 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5427 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5428 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005429
Chuck Leveracdeb692012-05-21 22:46:16 -04005430 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005431 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005432 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005433 status = -ENOMEM;
5434 goto out;
5435 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005436
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005437 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005438 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005439 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005440 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005441 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005442 }
5443
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005444 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005445 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005446 status = -ENOMEM;
5447 goto out_server_scope;
5448 }
5449
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005450 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005451 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005452 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005453
Chuck Lever177313f2012-05-21 22:44:58 -04005454 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005455 clp->cl_clientid = res.clientid;
5456 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5457 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5458 clp->cl_seqid = res.seqid;
5459
Chuck Leveracdeb692012-05-21 22:46:16 -04005460 kfree(clp->cl_serverowner);
5461 clp->cl_serverowner = res.server_owner;
5462 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005463
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005464 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005465 kfree(clp->cl_implid);
5466 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005467
Chuck Lever177313f2012-05-21 22:44:58 -04005468 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005469 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005470 res.server_scope)) {
5471 dprintk("%s: server_scope mismatch detected\n",
5472 __func__);
5473 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005474 kfree(clp->cl_serverscope);
5475 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005476 }
5477
Chuck Lever177313f2012-05-21 22:44:58 -04005478 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005479 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005480 goto out;
5481 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005482 } else
5483 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005484
Chuck Leveracdeb692012-05-21 22:46:16 -04005485out_server_owner:
5486 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005487out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005488 kfree(res.server_scope);
5489out:
Chuck Lever177313f2012-05-21 22:44:58 -04005490 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005491 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005492 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005493 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005494 clp->cl_implid->date.seconds,
5495 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005496 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005497 return status;
5498}
5499
Trond Myklebust66245532012-05-25 17:18:09 -04005500static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5501 struct rpc_cred *cred)
5502{
5503 struct rpc_message msg = {
5504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5505 .rpc_argp = clp,
5506 .rpc_cred = cred,
5507 };
5508 int status;
5509
5510 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5511 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005512 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005513 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5514 return status;
5515}
5516
5517static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5518 struct rpc_cred *cred)
5519{
5520 unsigned int loop;
5521 int ret;
5522
5523 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5524 ret = _nfs4_proc_destroy_clientid(clp, cred);
5525 switch (ret) {
5526 case -NFS4ERR_DELAY:
5527 case -NFS4ERR_CLIENTID_BUSY:
5528 ssleep(1);
5529 break;
5530 default:
5531 return ret;
5532 }
5533 }
5534 return 0;
5535}
5536
5537int nfs4_destroy_clientid(struct nfs_client *clp)
5538{
5539 struct rpc_cred *cred;
5540 int ret = 0;
5541
5542 if (clp->cl_mvops->minor_version < 1)
5543 goto out;
5544 if (clp->cl_exchange_flags == 0)
5545 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005546 if (clp->cl_preserve_clid)
5547 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005548 cred = nfs4_get_exchange_id_cred(clp);
5549 ret = nfs4_proc_destroy_clientid(clp, cred);
5550 if (cred)
5551 put_rpccred(cred);
5552 switch (ret) {
5553 case 0:
5554 case -NFS4ERR_STALE_CLIENTID:
5555 clp->cl_exchange_flags = 0;
5556 }
5557out:
5558 return ret;
5559}
5560
Andy Adamson2050f0c2009-04-01 09:22:30 -04005561struct nfs4_get_lease_time_data {
5562 struct nfs4_get_lease_time_args *args;
5563 struct nfs4_get_lease_time_res *res;
5564 struct nfs_client *clp;
5565};
5566
5567static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5568 void *calldata)
5569{
5570 int ret;
5571 struct nfs4_get_lease_time_data *data =
5572 (struct nfs4_get_lease_time_data *)calldata;
5573
5574 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005575 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005576 /* just setup sequence, do not trigger session recovery
5577 since we're invoked within one */
5578 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005579 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005580 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005581
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005582 if (ret != -EAGAIN)
5583 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005584 dprintk("<-- %s\n", __func__);
5585}
5586
5587/*
5588 * Called from nfs4_state_manager thread for session setup, so don't recover
5589 * from sequence operation or clientid errors.
5590 */
5591static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5592{
5593 struct nfs4_get_lease_time_data *data =
5594 (struct nfs4_get_lease_time_data *)calldata;
5595
5596 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005597 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5598 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005599 switch (task->tk_status) {
5600 case -NFS4ERR_DELAY:
5601 case -NFS4ERR_GRACE:
5602 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5603 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5604 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005605 /* fall through */
5606 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005607 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005608 return;
5609 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005610 dprintk("<-- %s\n", __func__);
5611}
5612
Trond Myklebust17280172012-03-11 13:11:00 -04005613static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005614 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5615 .rpc_call_done = nfs4_get_lease_time_done,
5616};
5617
5618int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5619{
5620 struct rpc_task *task;
5621 struct nfs4_get_lease_time_args args;
5622 struct nfs4_get_lease_time_res res = {
5623 .lr_fsinfo = fsinfo,
5624 };
5625 struct nfs4_get_lease_time_data data = {
5626 .args = &args,
5627 .res = &res,
5628 .clp = clp,
5629 };
5630 struct rpc_message msg = {
5631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5632 .rpc_argp = &args,
5633 .rpc_resp = &res,
5634 };
5635 struct rpc_task_setup task_setup = {
5636 .rpc_client = clp->cl_rpcclient,
5637 .rpc_message = &msg,
5638 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005639 .callback_data = &data,
5640 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005641 };
5642 int status;
5643
Trond Myklebust9d12b212012-01-17 22:04:25 -05005644 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005645 dprintk("--> %s\n", __func__);
5646 task = rpc_run_task(&task_setup);
5647
5648 if (IS_ERR(task))
5649 status = PTR_ERR(task);
5650 else {
5651 status = task->tk_status;
5652 rpc_put_task(task);
5653 }
5654 dprintk("<-- %s return %d\n", __func__, status);
5655
5656 return status;
5657}
5658
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005659static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5660{
5661 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5662}
5663
5664static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5665 struct nfs4_slot *new,
5666 u32 max_slots,
5667 u32 ivalue)
5668{
5669 struct nfs4_slot *old = NULL;
5670 u32 i;
5671
5672 spin_lock(&tbl->slot_tbl_lock);
5673 if (new) {
5674 old = tbl->slots;
5675 tbl->slots = new;
5676 tbl->max_slots = max_slots;
5677 }
Trond Myklebustf9b1ef52012-10-29 16:48:40 -04005678 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005679 for (i = 0; i < tbl->max_slots; i++)
5680 tbl->slots[i].seq_nr = ivalue;
5681 spin_unlock(&tbl->slot_tbl_lock);
5682 kfree(old);
5683}
5684
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005685/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005686 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005687 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005688static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5689 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005690{
Andy Adamson104aeba2010-01-14 17:45:10 -05005691 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005692 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005693
Andy Adamson104aeba2010-01-14 17:45:10 -05005694 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5695 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005696
Andy Adamson104aeba2010-01-14 17:45:10 -05005697 /* Does the newly negotiated max_reqs match the existing slot table? */
5698 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005699 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005700 if (!new)
5701 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005702 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005703 ret = 0;
5704
5705 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005706 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5707 tbl, tbl->slots, tbl->max_slots);
5708out:
5709 dprintk("<-- %s: return %d\n", __func__, ret);
5710 return ret;
5711}
5712
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005713/* Destroy the slot table */
5714static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5715{
5716 if (session->fc_slot_table.slots != NULL) {
5717 kfree(session->fc_slot_table.slots);
5718 session->fc_slot_table.slots = NULL;
5719 }
5720 if (session->bc_slot_table.slots != NULL) {
5721 kfree(session->bc_slot_table.slots);
5722 session->bc_slot_table.slots = NULL;
5723 }
5724 return;
5725}
5726
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005727/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005728 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005729 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005730static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005731{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005732 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005733 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005734
Andy Adamsonaacd5532011-11-09 13:58:21 -05005735 dprintk("--> %s\n", __func__);
5736 /* Fore channel */
5737 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005738 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5739 if (status) /* -ENOMEM */
5740 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005741 /* Back channel */
5742 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005743 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5744 if (status && tbl->slots == NULL)
5745 /* Fore and back channel share a connection so get
5746 * both slot tables or neither */
5747 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005748 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005749}
5750
5751struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5752{
5753 struct nfs4_session *session;
5754 struct nfs4_slot_table *tbl;
5755
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005756 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005757 if (!session)
5758 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005759
Andy Adamson557134a2009-04-01 09:21:53 -04005760 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005761 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005762 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005763 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005764 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005765
5766 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005767 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005768 spin_lock_init(&tbl->slot_tbl_lock);
5769 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005770 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005771
Trond Myklebust1055d762010-06-16 09:52:27 -04005772 session->session_state = 1<<NFS4_SESSION_INITING;
5773
Andy Adamson557134a2009-04-01 09:21:53 -04005774 session->clp = clp;
5775 return session;
5776}
5777
5778void nfs4_destroy_session(struct nfs4_session *session)
5779{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005780 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005781 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005782
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005783 cred = nfs4_get_exchange_id_cred(session->clp);
5784 nfs4_proc_destroy_session(session, cred);
5785 if (cred)
5786 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005787
5788 rcu_read_lock();
5789 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5790 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005791 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005792 __func__, xprt);
5793 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005794 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005795 kfree(session);
5796}
5797
Andy Adamsonfc931582009-04-01 09:22:31 -04005798/*
5799 * Initialize the values to be used by the client in CREATE_SESSION
5800 * If nfs4_init_session set the fore channel request and response sizes,
5801 * use them.
5802 *
5803 * Set the back channel max_resp_sz_cached to zero to force the client to
5804 * always set csa_cachethis to FALSE because the current implementation
5805 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5806 */
5807static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5808{
5809 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005810 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5811 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005812
5813 if (mxrqst_sz == 0)
5814 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5815 if (mxresp_sz == 0)
5816 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5817 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005818 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5819 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005820 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005821 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005822
5823 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005824 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005825 __func__,
5826 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005827 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005828
5829 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005830 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5831 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5832 args->bc_attrs.max_resp_sz_cached = 0;
5833 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5834 args->bc_attrs.max_reqs = 1;
5835
5836 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5837 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5838 __func__,
5839 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5840 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5841 args->bc_attrs.max_reqs);
5842}
5843
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005844static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005845{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005846 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5847 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5848
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005849 if (rcvd->max_resp_sz > sent->max_resp_sz)
5850 return -EINVAL;
5851 /*
5852 * Our requested max_ops is the minimum we need; we're not
5853 * prepared to break up compounds into smaller pieces than that.
5854 * So, no point even trying to continue if the server won't
5855 * cooperate:
5856 */
5857 if (rcvd->max_ops < sent->max_ops)
5858 return -EINVAL;
5859 if (rcvd->max_reqs == 0)
5860 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005861 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5862 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005863 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005864}
5865
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005866static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5867{
5868 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5869 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005870
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005871 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5872 return -EINVAL;
5873 if (rcvd->max_resp_sz < sent->max_resp_sz)
5874 return -EINVAL;
5875 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5876 return -EINVAL;
5877 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005878 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005879 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005880 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005881 return -EINVAL;
5882 return 0;
5883}
Andy Adamson8d353012009-04-01 09:22:32 -04005884
Andy Adamson8d353012009-04-01 09:22:32 -04005885static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5886 struct nfs4_session *session)
5887{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005888 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005889
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005890 ret = nfs4_verify_fore_channel_attrs(args, session);
5891 if (ret)
5892 return ret;
5893 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005894}
5895
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005896static int _nfs4_proc_create_session(struct nfs_client *clp,
5897 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005898{
5899 struct nfs4_session *session = clp->cl_session;
5900 struct nfs41_create_session_args args = {
5901 .client = clp,
5902 .cb_program = NFS4_CALLBACK,
5903 };
5904 struct nfs41_create_session_res res = {
5905 .client = clp,
5906 };
5907 struct rpc_message msg = {
5908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5909 .rpc_argp = &args,
5910 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005911 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005912 };
5913 int status;
5914
5915 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005916 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005917
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005918 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005919
Trond Myklebust43095d32012-11-20 11:13:12 -05005920 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005921 /* Verify the session's negotiated channel_attrs values */
5922 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005923 /* Increment the clientid slot sequence id */
5924 clp->cl_seqid++;
5925 }
5926
5927 return status;
5928}
5929
5930/*
5931 * Issues a CREATE_SESSION operation to the server.
5932 * It is the responsibility of the caller to verify the session is
5933 * expired before calling this routine.
5934 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005935int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005936{
5937 int status;
5938 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005939 struct nfs4_session *session = clp->cl_session;
5940
5941 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5942
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005943 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005944 if (status)
5945 goto out;
5946
Andy Adamsonaacd5532011-11-09 13:58:21 -05005947 /* Init or reset the session slot tables */
5948 status = nfs4_setup_session_slot_tables(session);
5949 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005950 if (status)
5951 goto out;
5952
5953 ptr = (unsigned *)&session->sess_id.data[0];
5954 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5955 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005956out:
5957 dprintk("<-- %s\n", __func__);
5958 return status;
5959}
5960
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005961/*
5962 * Issue the over-the-wire RPC DESTROY_SESSION.
5963 * The caller must serialize access to this routine.
5964 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005965int nfs4_proc_destroy_session(struct nfs4_session *session,
5966 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005967{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005968 struct rpc_message msg = {
5969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5970 .rpc_argp = session,
5971 .rpc_cred = cred,
5972 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005973 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005974
5975 dprintk("--> nfs4_proc_destroy_session\n");
5976
5977 /* session is still being setup */
5978 if (session->clp->cl_cons_state != NFS_CS_READY)
5979 return status;
5980
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005981 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005982
5983 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005984 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005985 "Session has been destroyed regardless...\n", status);
5986
5987 dprintk("<-- nfs4_proc_destroy_session\n");
5988 return status;
5989}
5990
Trond Myklebust7b38c362012-05-23 13:23:31 -04005991/*
5992 * With sessions, the client is not marked ready until after a
5993 * successful EXCHANGE_ID and CREATE_SESSION.
5994 *
5995 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5996 * other versions of NFS can be tried.
5997 */
5998static int nfs41_check_session_ready(struct nfs_client *clp)
5999{
6000 int ret;
6001
6002 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6003 ret = nfs4_client_recover_expired_lease(clp);
6004 if (ret)
6005 return ret;
6006 }
6007 if (clp->cl_cons_state < NFS_CS_READY)
6008 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006009 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006010 return 0;
6011}
6012
Trond Myklebustfccba802009-07-21 16:48:07 -04006013int nfs4_init_session(struct nfs_server *server)
6014{
6015 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006016 struct nfs4_session *session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006017 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6018 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
Trond Myklebustfccba802009-07-21 16:48:07 -04006019
6020 if (!nfs4_has_session(clp))
6021 return 0;
6022
Trond Myklebustae72ae672012-11-20 11:02:55 -05006023 if (server->rsize != 0)
6024 target_max_resp_sz = server->rsize;
6025 target_max_resp_sz += nfs41_maxread_overhead;
6026
6027 if (server->wsize != 0)
6028 target_max_rqst_sz = server->wsize;
6029 target_max_rqst_sz += nfs41_maxwrite_overhead;
6030
Trond Myklebust1055d762010-06-16 09:52:27 -04006031 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006032 spin_lock(&clp->cl_lock);
6033 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebustae72ae672012-11-20 11:02:55 -05006034 /* Initialise targets and channel attributes */
6035 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6036 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6037 session->fc_target_max_resp_sz = target_max_resp_sz;
6038 session->fc_attrs.max_resp_sz = target_max_resp_sz;
Trond Myklebust688a9022012-11-20 10:53:39 -05006039 } else {
6040 /* Just adjust the targets */
6041 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6042 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6043 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6044 }
6045 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6046 session->fc_target_max_resp_sz = target_max_resp_sz;
6047 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6048 }
Trond Myklebust7b38c362012-05-23 13:23:31 -04006049 }
6050 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006051
Trond Myklebust688a9022012-11-20 10:53:39 -05006052 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6053 nfs4_schedule_lease_recovery(clp);
6054
Trond Myklebust7b38c362012-05-23 13:23:31 -04006055 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006056}
6057
Trond Myklebust7b38c362012-05-23 13:23:31 -04006058int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006059{
6060 struct nfs4_session *session = clp->cl_session;
6061 int ret;
6062
Trond Myklebust7b38c362012-05-23 13:23:31 -04006063 spin_lock(&clp->cl_lock);
6064 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6065 /*
6066 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6067 * DS lease to be equal to the MDS lease.
6068 */
6069 clp->cl_lease_time = lease_time;
6070 clp->cl_last_renewal = jiffies;
6071 }
6072 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006073
Trond Myklebust7b38c362012-05-23 13:23:31 -04006074 ret = nfs41_check_session_ready(clp);
6075 if (ret)
6076 return ret;
6077 /* Test for the DS role */
6078 if (!is_ds_client(clp))
6079 return -ENODEV;
6080 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006081}
6082EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6083
6084
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006085/*
6086 * Renew the cl_session lease.
6087 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006088struct nfs4_sequence_data {
6089 struct nfs_client *clp;
6090 struct nfs4_sequence_args args;
6091 struct nfs4_sequence_res res;
6092};
6093
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006094static void nfs41_sequence_release(void *data)
6095{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006096 struct nfs4_sequence_data *calldata = data;
6097 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006098
Alexandros Batsakis71358402010-02-05 03:45:05 -08006099 if (atomic_read(&clp->cl_count) > 1)
6100 nfs4_schedule_state_renewal(clp);
6101 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006102 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006103}
6104
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006105static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6106{
6107 switch(task->tk_status) {
6108 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006109 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6110 return -EAGAIN;
6111 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006112 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006113 }
6114 return 0;
6115}
6116
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006117static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006118{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006119 struct nfs4_sequence_data *calldata = data;
6120 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006121
Trond Myklebust14516c32010-07-31 14:29:06 -04006122 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6123 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006124
6125 if (task->tk_status < 0) {
6126 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006127 if (atomic_read(&clp->cl_count) == 1)
6128 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006129
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006130 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6131 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006132 return;
6133 }
6134 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006135 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006136out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006137 dprintk("<-- %s\n", __func__);
6138}
6139
6140static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6141{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006142 struct nfs4_sequence_data *calldata = data;
6143 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006144 struct nfs4_sequence_args *args;
6145 struct nfs4_sequence_res *res;
6146
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006147 args = task->tk_msg.rpc_argp;
6148 res = task->tk_msg.rpc_resp;
6149
Trond Myklebust9d12b212012-01-17 22:04:25 -05006150 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006151 return;
6152 rpc_call_start(task);
6153}
6154
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006155static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6156{
6157 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6158 nfs41_sequence_prepare(task, data);
6159}
6160
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006161static const struct rpc_call_ops nfs41_sequence_ops = {
6162 .rpc_call_done = nfs41_sequence_call_done,
6163 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006164 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006165};
6166
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006167static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6168 .rpc_call_done = nfs41_sequence_call_done,
6169 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6170 .rpc_release = nfs41_sequence_release,
6171};
6172
6173static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6174 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006175{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006176 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006177 struct rpc_message msg = {
6178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6179 .rpc_cred = cred,
6180 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006181 struct rpc_task_setup task_setup_data = {
6182 .rpc_client = clp->cl_rpcclient,
6183 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006184 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006185 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6186 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006187
Alexandros Batsakis71358402010-02-05 03:45:05 -08006188 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006189 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006190 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006191 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006192 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006193 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006194 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006195 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006196 msg.rpc_argp = &calldata->args;
6197 msg.rpc_resp = &calldata->res;
6198 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006199 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006200
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006201 return rpc_run_task(&task_setup_data);
6202}
6203
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006204static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006205{
6206 struct rpc_task *task;
6207 int ret = 0;
6208
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006209 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6210 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006211 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006212 if (IS_ERR(task))
6213 ret = PTR_ERR(task);
6214 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006215 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006216 dprintk("<-- %s status=%d\n", __func__, ret);
6217 return ret;
6218}
6219
6220static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6221{
6222 struct rpc_task *task;
6223 int ret;
6224
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006225 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006226 if (IS_ERR(task)) {
6227 ret = PTR_ERR(task);
6228 goto out;
6229 }
6230 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006231 if (!ret) {
6232 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6233
6234 if (task->tk_status == 0)
6235 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006236 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006237 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006238 rpc_put_task(task);
6239out:
6240 dprintk("<-- %s status=%d\n", __func__, ret);
6241 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006242}
6243
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006244struct nfs4_reclaim_complete_data {
6245 struct nfs_client *clp;
6246 struct nfs41_reclaim_complete_args arg;
6247 struct nfs41_reclaim_complete_res res;
6248};
6249
6250static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6251{
6252 struct nfs4_reclaim_complete_data *calldata = data;
6253
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006254 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006255 if (nfs41_setup_sequence(calldata->clp->cl_session,
6256 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006257 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006258 return;
6259
6260 rpc_call_start(task);
6261}
6262
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006263static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6264{
6265 switch(task->tk_status) {
6266 case 0:
6267 case -NFS4ERR_COMPLETE_ALREADY:
6268 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6269 break;
6270 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006271 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006272 /* fall through */
6273 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006274 return -EAGAIN;
6275 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006276 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006277 }
6278 return 0;
6279}
6280
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006281static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6282{
6283 struct nfs4_reclaim_complete_data *calldata = data;
6284 struct nfs_client *clp = calldata->clp;
6285 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6286
6287 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006288 if (!nfs41_sequence_done(task, res))
6289 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006290
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006291 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6292 rpc_restart_call_prepare(task);
6293 return;
6294 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006295 dprintk("<-- %s\n", __func__);
6296}
6297
6298static void nfs4_free_reclaim_complete_data(void *data)
6299{
6300 struct nfs4_reclaim_complete_data *calldata = data;
6301
6302 kfree(calldata);
6303}
6304
6305static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6306 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6307 .rpc_call_done = nfs4_reclaim_complete_done,
6308 .rpc_release = nfs4_free_reclaim_complete_data,
6309};
6310
6311/*
6312 * Issue a global reclaim complete.
6313 */
6314static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6315{
6316 struct nfs4_reclaim_complete_data *calldata;
6317 struct rpc_task *task;
6318 struct rpc_message msg = {
6319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6320 };
6321 struct rpc_task_setup task_setup_data = {
6322 .rpc_client = clp->cl_rpcclient,
6323 .rpc_message = &msg,
6324 .callback_ops = &nfs4_reclaim_complete_call_ops,
6325 .flags = RPC_TASK_ASYNC,
6326 };
6327 int status = -ENOMEM;
6328
6329 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006330 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006331 if (calldata == NULL)
6332 goto out;
6333 calldata->clp = clp;
6334 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006335
Trond Myklebust9d12b212012-01-17 22:04:25 -05006336 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006337 msg.rpc_argp = &calldata->arg;
6338 msg.rpc_resp = &calldata->res;
6339 task_setup_data.callback_data = calldata;
6340 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006341 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006342 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006343 goto out;
6344 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006345 status = nfs4_wait_for_completion_rpc_task(task);
6346 if (status == 0)
6347 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006348 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006349 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006350out:
6351 dprintk("<-- %s status=%d\n", __func__, status);
6352 return status;
6353}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006354
6355static void
6356nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6357{
6358 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006359 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006360
6361 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006362 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6363 * right now covering the LAYOUTGET we are about to send.
6364 * However, that is not so catastrophic, and there seems
6365 * to be no way to prevent it completely.
6366 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006367 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006368 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006369 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006370 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6371 NFS_I(lgp->args.inode)->layout,
6372 lgp->args.ctx->state)) {
6373 rpc_exit(task, NFS4_OK);
6374 return;
6375 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006376 rpc_call_start(task);
6377}
6378
6379static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6380{
6381 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006382 struct inode *inode = lgp->args.inode;
6383 struct nfs_server *server = NFS_SERVER(inode);
6384 struct pnfs_layout_hdr *lo;
6385 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006386
6387 dprintk("--> %s\n", __func__);
6388
6389 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006390 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006391
6392 switch (task->tk_status) {
6393 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006394 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006395 case -NFS4ERR_LAYOUTTRYLATER:
6396 case -NFS4ERR_RECALLCONFLICT:
6397 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006398 break;
6399 case -NFS4ERR_EXPIRED:
6400 case -NFS4ERR_BAD_STATEID:
6401 spin_lock(&inode->i_lock);
6402 lo = NFS_I(inode)->layout;
6403 if (!lo || list_empty(&lo->plh_segs)) {
6404 spin_unlock(&inode->i_lock);
6405 /* If the open stateid was bad, then recover it. */
6406 state = lgp->args.ctx->state;
6407 } else {
6408 LIST_HEAD(head);
6409
6410 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6411 spin_unlock(&inode->i_lock);
6412 /* Mark the bad layout state as invalid, then
6413 * retry using the open stateid. */
6414 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006415 }
6416 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006417 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6418 rpc_restart_call_prepare(task);
6419out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006420 dprintk("<-- %s\n", __func__);
6421}
6422
Idan Kedar85541162012-08-02 11:47:10 +03006423static size_t max_response_pages(struct nfs_server *server)
6424{
6425 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6426 return nfs_page_array_len(0, max_resp_sz);
6427}
6428
6429static void nfs4_free_pages(struct page **pages, size_t size)
6430{
6431 int i;
6432
6433 if (!pages)
6434 return;
6435
6436 for (i = 0; i < size; i++) {
6437 if (!pages[i])
6438 break;
6439 __free_page(pages[i]);
6440 }
6441 kfree(pages);
6442}
6443
6444static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6445{
6446 struct page **pages;
6447 int i;
6448
6449 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6450 if (!pages) {
6451 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6452 return NULL;
6453 }
6454
6455 for (i = 0; i < size; i++) {
6456 pages[i] = alloc_page(gfp_flags);
6457 if (!pages[i]) {
6458 dprintk("%s: failed to allocate page\n", __func__);
6459 nfs4_free_pages(pages, size);
6460 return NULL;
6461 }
6462 }
6463
6464 return pages;
6465}
6466
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006467static void nfs4_layoutget_release(void *calldata)
6468{
6469 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006470 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6471 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006472
6473 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006474 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006475 put_nfs_open_context(lgp->args.ctx);
6476 kfree(calldata);
6477 dprintk("<-- %s\n", __func__);
6478}
6479
6480static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6481 .rpc_call_prepare = nfs4_layoutget_prepare,
6482 .rpc_call_done = nfs4_layoutget_done,
6483 .rpc_release = nfs4_layoutget_release,
6484};
6485
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006486struct pnfs_layout_segment *
6487nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006488{
6489 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006490 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006491 struct rpc_task *task;
6492 struct rpc_message msg = {
6493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6494 .rpc_argp = &lgp->args,
6495 .rpc_resp = &lgp->res,
6496 };
6497 struct rpc_task_setup task_setup_data = {
6498 .rpc_client = server->client,
6499 .rpc_message = &msg,
6500 .callback_ops = &nfs4_layoutget_call_ops,
6501 .callback_data = lgp,
6502 .flags = RPC_TASK_ASYNC,
6503 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006504 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006505 int status = 0;
6506
6507 dprintk("--> %s\n", __func__);
6508
Idan Kedar85541162012-08-02 11:47:10 +03006509 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6510 if (!lgp->args.layout.pages) {
6511 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006512 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006513 }
6514 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6515
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006516 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006517 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006518 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006519 task = rpc_run_task(&task_setup_data);
6520 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006521 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006522 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006523 if (status == 0)
6524 status = task->tk_status;
6525 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006526 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006527 rpc_put_task(task);
6528 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006529 if (status)
6530 return ERR_PTR(status);
6531 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006532}
6533
Benny Halevycbe82602011-05-22 19:52:37 +03006534static void
6535nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6536{
6537 struct nfs4_layoutreturn *lrp = calldata;
6538
6539 dprintk("--> %s\n", __func__);
6540 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006541 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006542 return;
6543 rpc_call_start(task);
6544}
6545
6546static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6547{
6548 struct nfs4_layoutreturn *lrp = calldata;
6549 struct nfs_server *server;
6550
6551 dprintk("--> %s\n", __func__);
6552
6553 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6554 return;
6555
6556 server = NFS_SERVER(lrp->args.inode);
6557 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006558 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006559 return;
6560 }
Benny Halevycbe82602011-05-22 19:52:37 +03006561 dprintk("<-- %s\n", __func__);
6562}
6563
6564static void nfs4_layoutreturn_release(void *calldata)
6565{
6566 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006567 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006568
6569 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006570 spin_lock(&lo->plh_inode->i_lock);
6571 if (lrp->res.lrs_present)
6572 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6573 lo->plh_block_lgets--;
6574 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006575 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006576 kfree(calldata);
6577 dprintk("<-- %s\n", __func__);
6578}
6579
6580static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6581 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6582 .rpc_call_done = nfs4_layoutreturn_done,
6583 .rpc_release = nfs4_layoutreturn_release,
6584};
6585
6586int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6587{
6588 struct rpc_task *task;
6589 struct rpc_message msg = {
6590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6591 .rpc_argp = &lrp->args,
6592 .rpc_resp = &lrp->res,
6593 };
6594 struct rpc_task_setup task_setup_data = {
6595 .rpc_client = lrp->clp->cl_rpcclient,
6596 .rpc_message = &msg,
6597 .callback_ops = &nfs4_layoutreturn_call_ops,
6598 .callback_data = lrp,
6599 };
6600 int status;
6601
6602 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006603 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006604 task = rpc_run_task(&task_setup_data);
6605 if (IS_ERR(task))
6606 return PTR_ERR(task);
6607 status = task->tk_status;
6608 dprintk("<-- %s status=%d\n", __func__, status);
6609 rpc_put_task(task);
6610 return status;
6611}
6612
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006613/*
6614 * Retrieve the list of Data Server devices from the MDS.
6615 */
6616static int _nfs4_getdevicelist(struct nfs_server *server,
6617 const struct nfs_fh *fh,
6618 struct pnfs_devicelist *devlist)
6619{
6620 struct nfs4_getdevicelist_args args = {
6621 .fh = fh,
6622 .layoutclass = server->pnfs_curr_ld->id,
6623 };
6624 struct nfs4_getdevicelist_res res = {
6625 .devlist = devlist,
6626 };
6627 struct rpc_message msg = {
6628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6629 .rpc_argp = &args,
6630 .rpc_resp = &res,
6631 };
6632 int status;
6633
6634 dprintk("--> %s\n", __func__);
6635 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6636 &res.seq_res, 0);
6637 dprintk("<-- %s status=%d\n", __func__, status);
6638 return status;
6639}
6640
6641int nfs4_proc_getdevicelist(struct nfs_server *server,
6642 const struct nfs_fh *fh,
6643 struct pnfs_devicelist *devlist)
6644{
6645 struct nfs4_exception exception = { };
6646 int err;
6647
6648 do {
6649 err = nfs4_handle_exception(server,
6650 _nfs4_getdevicelist(server, fh, devlist),
6651 &exception);
6652 } while (exception.retry);
6653
6654 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6655 err, devlist->num_devs);
6656
6657 return err;
6658}
6659EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6660
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006661static int
6662_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6663{
6664 struct nfs4_getdeviceinfo_args args = {
6665 .pdev = pdev,
6666 };
6667 struct nfs4_getdeviceinfo_res res = {
6668 .pdev = pdev,
6669 };
6670 struct rpc_message msg = {
6671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6672 .rpc_argp = &args,
6673 .rpc_resp = &res,
6674 };
6675 int status;
6676
6677 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006678 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006679 dprintk("<-- %s status=%d\n", __func__, status);
6680
6681 return status;
6682}
6683
6684int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6685{
6686 struct nfs4_exception exception = { };
6687 int err;
6688
6689 do {
6690 err = nfs4_handle_exception(server,
6691 _nfs4_proc_getdeviceinfo(server, pdev),
6692 &exception);
6693 } while (exception.retry);
6694 return err;
6695}
6696EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6697
Andy Adamson863a3c62011-03-23 13:27:54 +00006698static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6699{
6700 struct nfs4_layoutcommit_data *data = calldata;
6701 struct nfs_server *server = NFS_SERVER(data->args.inode);
6702
6703 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006704 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006705 return;
6706 rpc_call_start(task);
6707}
6708
6709static void
6710nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6711{
6712 struct nfs4_layoutcommit_data *data = calldata;
6713 struct nfs_server *server = NFS_SERVER(data->args.inode);
6714
6715 if (!nfs4_sequence_done(task, &data->res.seq_res))
6716 return;
6717
6718 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006719 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6720 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6721 case -NFS4ERR_BADLAYOUT: /* no layout */
6722 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006723 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006724 break;
6725 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006726 nfs_post_op_update_inode_force_wcc(data->args.inode,
6727 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006728 break;
6729 default:
6730 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6731 rpc_restart_call_prepare(task);
6732 return;
6733 }
6734 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006735}
6736
6737static void nfs4_layoutcommit_release(void *calldata)
6738{
6739 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006740 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006741 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006742
Andy Adamsondb29c082011-07-30 20:52:38 -04006743 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006744 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006745 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6746 list_del_init(&lseg->pls_lc_list);
6747 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6748 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006749 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006750 }
Peng Tao92407e72011-10-23 20:21:17 -07006751
6752 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6753 smp_mb__after_clear_bit();
6754 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6755
Andy Adamson863a3c62011-03-23 13:27:54 +00006756 put_rpccred(data->cred);
6757 kfree(data);
6758}
6759
6760static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6761 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6762 .rpc_call_done = nfs4_layoutcommit_done,
6763 .rpc_release = nfs4_layoutcommit_release,
6764};
6765
6766int
Andy Adamsonef311532011-03-12 02:58:10 -05006767nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006768{
6769 struct rpc_message msg = {
6770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6771 .rpc_argp = &data->args,
6772 .rpc_resp = &data->res,
6773 .rpc_cred = data->cred,
6774 };
6775 struct rpc_task_setup task_setup_data = {
6776 .task = &data->task,
6777 .rpc_client = NFS_CLIENT(data->args.inode),
6778 .rpc_message = &msg,
6779 .callback_ops = &nfs4_layoutcommit_ops,
6780 .callback_data = data,
6781 .flags = RPC_TASK_ASYNC,
6782 };
6783 struct rpc_task *task;
6784 int status = 0;
6785
6786 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6787 "lbw: %llu inode %lu\n",
6788 data->task.tk_pid, sync,
6789 data->args.lastbytewritten,
6790 data->args.inode->i_ino);
6791
Trond Myklebust9d12b212012-01-17 22:04:25 -05006792 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006793 task = rpc_run_task(&task_setup_data);
6794 if (IS_ERR(task))
6795 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006796 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006797 goto out;
6798 status = nfs4_wait_for_completion_rpc_task(task);
6799 if (status != 0)
6800 goto out;
6801 status = task->tk_status;
6802out:
6803 dprintk("%s: status %d\n", __func__, status);
6804 rpc_put_task(task);
6805 return status;
6806}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006807
6808static int
6809_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6810 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6811{
6812 struct nfs41_secinfo_no_name_args args = {
6813 .style = SECINFO_STYLE_CURRENT_FH,
6814 };
6815 struct nfs4_secinfo_res res = {
6816 .flavors = flavors,
6817 };
6818 struct rpc_message msg = {
6819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6820 .rpc_argp = &args,
6821 .rpc_resp = &res,
6822 };
6823 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6824}
6825
6826static int
6827nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6828 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6829{
6830 struct nfs4_exception exception = { };
6831 int err;
6832 do {
6833 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6834 switch (err) {
6835 case 0:
6836 case -NFS4ERR_WRONGSEC:
6837 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006838 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006839 default:
6840 err = nfs4_handle_exception(server, err, &exception);
6841 }
6842 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006843out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006844 return err;
6845}
6846
6847static int
6848nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6849 struct nfs_fsinfo *info)
6850{
6851 int err;
6852 struct page *page;
6853 rpc_authflavor_t flavor;
6854 struct nfs4_secinfo_flavors *flavors;
6855
6856 page = alloc_page(GFP_KERNEL);
6857 if (!page) {
6858 err = -ENOMEM;
6859 goto out;
6860 }
6861
6862 flavors = page_address(page);
6863 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6864
6865 /*
6866 * Fall back on "guess and check" method if
6867 * the server doesn't support SECINFO_NO_NAME
6868 */
6869 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6870 err = nfs4_find_root_sec(server, fhandle, info);
6871 goto out_freepage;
6872 }
6873 if (err)
6874 goto out_freepage;
6875
6876 flavor = nfs_find_best_sec(flavors);
6877 if (err == 0)
6878 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6879
6880out_freepage:
6881 put_page(page);
6882 if (err == -EACCES)
6883 return -EPERM;
6884out:
6885 return err;
6886}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006887
6888static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006889{
6890 int status;
6891 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006892 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006893 };
6894 struct nfs41_test_stateid_res res;
6895 struct rpc_message msg = {
6896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6897 .rpc_argp = &args,
6898 .rpc_resp = &res,
6899 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006900
Chuck Lever38527b12012-07-11 16:30:23 -04006901 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006902 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006903 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006904 if (status != NFS_OK) {
6905 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006906 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006907 }
6908 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006909 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006910}
6911
Chuck Lever38527b12012-07-11 16:30:23 -04006912/**
6913 * nfs41_test_stateid - perform a TEST_STATEID operation
6914 *
6915 * @server: server / transport on which to perform the operation
6916 * @stateid: state ID to test
6917 *
6918 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6919 * Otherwise a negative NFS4ERR value is returned if the operation
6920 * failed or the state ID is not currently valid.
6921 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006922static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006923{
6924 struct nfs4_exception exception = { };
6925 int err;
6926 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006927 err = _nfs41_test_stateid(server, stateid);
6928 if (err != -NFS4ERR_DELAY)
6929 break;
6930 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006931 } while (exception.retry);
6932 return err;
6933}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006934
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006935static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006936{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006937 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006938 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006939 };
6940 struct nfs41_free_stateid_res res;
6941 struct rpc_message msg = {
6942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6943 .rpc_argp = &args,
6944 .rpc_resp = &res,
6945 };
Chuck Lever38527b12012-07-11 16:30:23 -04006946 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006947
Chuck Lever38527b12012-07-11 16:30:23 -04006948 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006949 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006950 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006951 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006952 dprintk("NFS reply free_stateid: %d\n", status);
6953 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006954}
6955
Chuck Lever38527b12012-07-11 16:30:23 -04006956/**
6957 * nfs41_free_stateid - perform a FREE_STATEID operation
6958 *
6959 * @server: server / transport on which to perform the operation
6960 * @stateid: state ID to release
6961 *
6962 * Returns NFS_OK if the server freed "stateid". Otherwise a
6963 * negative NFS4ERR value is returned.
6964 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006965static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006966{
6967 struct nfs4_exception exception = { };
6968 int err;
6969 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006970 err = _nfs4_free_stateid(server, stateid);
6971 if (err != -NFS4ERR_DELAY)
6972 break;
6973 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006974 } while (exception.retry);
6975 return err;
6976}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006977
6978static bool nfs41_match_stateid(const nfs4_stateid *s1,
6979 const nfs4_stateid *s2)
6980{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006981 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006982 return false;
6983
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006984 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006985 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006986 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006987 return true;
6988
6989 return false;
6990}
6991
Andy Adamson557134a2009-04-01 09:21:53 -04006992#endif /* CONFIG_NFS_V4_1 */
6993
Trond Myklebust36281ca2012-03-04 18:13:56 -05006994static bool nfs4_match_stateid(const nfs4_stateid *s1,
6995 const nfs4_stateid *s2)
6996{
Trond Myklebustf597c532012-03-04 18:13:56 -05006997 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006998}
6999
7000
Trond Myklebust17280172012-03-11 13:11:00 -04007001static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007002 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007003 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 .recover_open = nfs4_open_reclaim,
7005 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007006 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007007 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007008 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009};
7010
Andy Adamson591d71c2009-04-01 09:22:47 -04007011#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007012static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007013 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7014 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7015 .recover_open = nfs4_open_reclaim,
7016 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007017 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007018 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007019 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007020 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007021};
7022#endif /* CONFIG_NFS_V4_1 */
7023
Trond Myklebust17280172012-03-11 13:11:00 -04007024static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007025 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007026 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 .recover_open = nfs4_open_expired,
7028 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007029 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007030 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031};
7032
Andy Adamson591d71c2009-04-01 09:22:47 -04007033#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007034static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007035 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7036 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007037 .recover_open = nfs41_open_expired,
7038 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007039 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007040 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007041};
7042#endif /* CONFIG_NFS_V4_1 */
7043
Trond Myklebust17280172012-03-11 13:11:00 -04007044static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007045 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007046 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007047 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007048};
7049
7050#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007051static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007052 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007053 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007054 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007055};
7056#endif
7057
Trond Myklebust97dc1352010-06-16 09:52:26 -04007058static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7059 .minor_version = 0,
7060 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007061 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007062 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007063 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7064 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7065 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007066};
7067
7068#if defined(CONFIG_NFS_V4_1)
7069static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7070 .minor_version = 1,
7071 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007072 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007073 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007074 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7075 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7076 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007077};
7078#endif
7079
Trond Myklebust97dc1352010-06-16 09:52:26 -04007080const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7081 [0] = &nfs_v4_0_minor_ops,
7082#if defined(CONFIG_NFS_V4_1)
7083 [1] = &nfs_v4_1_minor_ops,
7084#endif
7085};
7086
Bryan Schumaker73a79702012-07-16 16:39:12 -04007087const struct inode_operations nfs4_dir_inode_operations = {
7088 .create = nfs_create,
7089 .lookup = nfs_lookup,
7090 .atomic_open = nfs_atomic_open,
7091 .link = nfs_link,
7092 .unlink = nfs_unlink,
7093 .symlink = nfs_symlink,
7094 .mkdir = nfs_mkdir,
7095 .rmdir = nfs_rmdir,
7096 .mknod = nfs_mknod,
7097 .rename = nfs_rename,
7098 .permission = nfs_permission,
7099 .getattr = nfs_getattr,
7100 .setattr = nfs_setattr,
7101 .getxattr = generic_getxattr,
7102 .setxattr = generic_setxattr,
7103 .listxattr = generic_listxattr,
7104 .removexattr = generic_removexattr,
7105};
7106
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007107static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007108 .permission = nfs_permission,
7109 .getattr = nfs_getattr,
7110 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007111 .getxattr = generic_getxattr,
7112 .setxattr = generic_setxattr,
7113 .listxattr = generic_listxattr,
7114 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007115};
7116
David Howells509de812006-08-22 20:06:11 -04007117const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 .version = 4, /* protocol version */
7119 .dentry_ops = &nfs4_dentry_operations,
7120 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007121 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007122 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007124 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007125 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 .getattr = nfs4_proc_getattr,
7127 .setattr = nfs4_proc_setattr,
7128 .lookup = nfs4_proc_lookup,
7129 .access = nfs4_proc_access,
7130 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 .create = nfs4_proc_create,
7132 .remove = nfs4_proc_remove,
7133 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007134 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 .unlink_done = nfs4_proc_unlink_done,
7136 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007137 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007138 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007139 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 .link = nfs4_proc_link,
7141 .symlink = nfs4_proc_symlink,
7142 .mkdir = nfs4_proc_mkdir,
7143 .rmdir = nfs4_proc_remove,
7144 .readdir = nfs4_proc_readdir,
7145 .mknod = nfs4_proc_mknod,
7146 .statfs = nfs4_proc_statfs,
7147 .fsinfo = nfs4_proc_fsinfo,
7148 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007149 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 .decode_dirent = nfs4_decode_dirent,
7151 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007152 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007153 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007154 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007156 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007157 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007158 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007160 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007161 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007163 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007164 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007165 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007166 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007167 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007168 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007169 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007170 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007171 .create_server = nfs4_create_server,
7172 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173};
7174
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007175static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7176 .prefix = XATTR_NAME_NFSV4_ACL,
7177 .list = nfs4_xattr_list_nfs4_acl,
7178 .get = nfs4_xattr_get_nfs4_acl,
7179 .set = nfs4_xattr_set_nfs4_acl,
7180};
7181
7182const struct xattr_handler *nfs4_xattr_handlers[] = {
7183 &nfs4_xattr_nfs4_acl_handler,
7184 NULL
7185};
7186
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187/*
7188 * Local variables:
7189 * c-basic-offset: 8
7190 * End:
7191 */