blob: 2b62e3f79c917001b727f9d8b7bac1e0171c8f4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010058#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebust4ce79712005-06-22 17:16:21 +000060#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050062#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050063#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040064#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040065#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040066#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustcdd4e682006-01-03 09:55:12 +010075struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010076static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080077static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050079static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050080static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040081static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040082static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040083static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050091static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Trond Myklebust52567b02009-10-23 14:46:42 -040093 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
97 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000098 case -NFS4ERR_WRONGSEC:
99 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
102 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400103 case -NFS4ERR_SHARE_DENIED:
104 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400107 case -NFS4ERR_ACCESS:
108 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700111 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400114 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/*
118 * This is our standard bitmap for GETATTR requests.
119 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400120const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 FATTR4_WORD0_TYPE
122 | FATTR4_WORD0_CHANGE
123 | FATTR4_WORD0_SIZE
124 | FATTR4_WORD0_FSID
125 | FATTR4_WORD0_FILEID,
126 FATTR4_WORD1_MODE
127 | FATTR4_WORD1_NUMLINKS
128 | FATTR4_WORD1_OWNER
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
135};
136
Trond Myklebust1549210f2012-06-05 09:16:47 -0400137static const u32 nfs4_pnfs_open_bitmap[3] = {
138 FATTR4_WORD0_TYPE
139 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_SIZE
141 | FATTR4_WORD0_FSID
142 | FATTR4_WORD0_FILEID,
143 FATTR4_WORD1_MODE
144 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
153};
154
Andy Adamsone23008e2012-10-02 21:07:32 -0400155static const u32 nfs4_open_noattr_bitmap[3] = {
156 FATTR4_WORD0_TYPE
157 | FATTR4_WORD0_CHANGE
158 | FATTR4_WORD0_FILEID,
159};
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161const u32 nfs4_statfs_bitmap[2] = {
162 FATTR4_WORD0_FILES_AVAIL
163 | FATTR4_WORD0_FILES_FREE
164 | FATTR4_WORD0_FILES_TOTAL,
165 FATTR4_WORD1_SPACE_AVAIL
166 | FATTR4_WORD1_SPACE_FREE
167 | FATTR4_WORD1_SPACE_TOTAL
168};
169
Trond Myklebust4ce79712005-06-22 17:16:21 +0000170const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 FATTR4_WORD0_MAXLINK
172 | FATTR4_WORD0_MAXNAME,
173 0
174};
175
Fred Isamandae100c2011-07-30 20:52:37 -0400176const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 | FATTR4_WORD0_MAXREAD
178 | FATTR4_WORD0_MAXWRITE
179 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700180 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400181 | FATTR4_WORD1_FS_LAYOUT_TYPES,
182 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184
Manoj Naik830b8e32006-06-09 09:34:25 -0400185const u32 nfs4_fs_locations_bitmap[2] = {
186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID
191 | FATTR4_WORD0_FS_LOCATIONS,
192 FATTR4_WORD1_MODE
193 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER
195 | FATTR4_WORD1_OWNER_GROUP
196 | FATTR4_WORD1_RAWDEV
197 | FATTR4_WORD1_SPACE_USED
198 | FATTR4_WORD1_TIME_ACCESS
199 | FATTR4_WORD1_TIME_METADATA
200 | FATTR4_WORD1_TIME_MODIFY
201 | FATTR4_WORD1_MOUNTED_ON_FILEID
202};
203
Al Virobc4785c2006-10-19 23:28:51 -0700204static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct nfs4_readdir_arg *readdir)
206{
Al Viro0dbb4c62006-10-19 23:28:49 -0700207 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 BUG_ON(readdir->count < 80);
210 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000211 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
213 return;
214 }
215
216 readdir->cookie = 0;
217 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
218 if (cookie == 2)
219 return;
220
221 /*
222 * NFSv4 servers do not return entries for '.' and '..'
223 * Therefore, we fake these entries here. We let '.'
224 * have cookie 0 and '..' have cookie 1. Note that
225 * when talking to the server, we always send cookie 0
226 * instead of 1 or 2.
227 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800228 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (cookie == 0) {
231 *p++ = xdr_one; /* next */
232 *p++ = xdr_zero; /* cookie, first word */
233 *p++ = xdr_one; /* cookie, second word */
234 *p++ = xdr_one; /* entry len */
235 memcpy(p, ".\0\0\0", 4); /* entry */
236 p++;
237 *p++ = xdr_one; /* bitmap length */
238 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
239 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400240 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
243 *p++ = xdr_one; /* next */
244 *p++ = xdr_zero; /* cookie, first word */
245 *p++ = xdr_two; /* cookie, second word */
246 *p++ = xdr_two; /* entry len */
247 memcpy(p, "..\0\0", 4); /* entry */
248 p++;
249 *p++ = xdr_one; /* bitmap length */
250 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
251 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400252 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 readdir->pgbase = (char *)p - (char *)start;
255 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800256 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Trond Myklebust65de8722008-12-23 15:21:44 -0500259static int nfs4_wait_clnt_recover(struct nfs_client *clp)
260{
261 int res;
262
263 might_sleep();
264
Trond Myklebuste005e802008-12-23 15:21:48 -0500265 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400266 nfs_wait_bit_killable, TASK_KILLABLE);
Chuck Lever46a87b82012-07-11 16:30:41 -0400267 if (res)
268 return res;
269
270 if (clp->cl_cons_state < 0)
271 return clp->cl_cons_state;
272 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500273}
274
275static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
276{
277 int res = 0;
278
279 might_sleep();
280
281 if (*timeout <= 0)
282 *timeout = NFS4_POLL_RETRY_MIN;
283 if (*timeout > NFS4_POLL_RETRY_MAX)
284 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100285 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 if (fatal_signal_pending(current))
287 res = -ERESTARTSYS;
288 *timeout <<= 1;
289 return res;
290}
291
292/* This is the error handling routine for processes that are allowed
293 * to sleep.
294 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800295static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500296{
297 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500298 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500299 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500300 int ret = errorcode;
301
302 exception->retry = 0;
303 switch(errorcode) {
304 case 0:
305 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500306 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400307 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400308 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500309 exception->retry = 1;
310 return 0;
311 }
312 if (state == NULL)
313 break;
314 nfs4_schedule_stateid_recovery(server, state);
315 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500316 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500317 case -NFS4ERR_ADMIN_REVOKED:
318 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500319 if (state == NULL)
320 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400321 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500322 nfs4_schedule_stateid_recovery(server, state);
323 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400324 case -NFS4ERR_EXPIRED:
325 if (state != NULL)
326 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500327 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500328 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500329 nfs4_schedule_lease_recovery(clp);
330 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500331#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400332 case -NFS4ERR_BADSESSION:
333 case -NFS4ERR_BADSLOT:
334 case -NFS4ERR_BAD_HIGH_SLOT:
335 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
336 case -NFS4ERR_DEADSESSION:
337 case -NFS4ERR_SEQ_FALSE_RETRY:
338 case -NFS4ERR_SEQ_MISORDERED:
339 dprintk("%s ERROR: %d Reset session\n", __func__,
340 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400341 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400342 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500343 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500344#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500346 if (exception->timeout > HZ) {
347 /* We have retried a decent amount, time to
348 * fail
349 */
350 ret = -EBUSY;
351 break;
352 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500353 case -NFS4ERR_GRACE:
354 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500355 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500356 ret = nfs4_delay(server->client, &exception->timeout);
357 if (ret != 0)
358 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400359 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500360 case -NFS4ERR_OLD_STATEID:
361 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800362 break;
363 case -NFS4ERR_BADOWNER:
364 /* The following works around a Linux server bug! */
365 case -NFS4ERR_BADNAME:
366 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
367 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
368 exception->retry = 1;
369 printk(KERN_WARNING "NFS: v4 server %s "
370 "does not accept raw "
371 "uid/gids. "
372 "Reenabling the idmapper.\n",
373 server->nfs_client->cl_hostname);
374 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500375 }
376 /* We failed to handle the error */
377 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500378wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500379 ret = nfs4_wait_clnt_recover(clp);
380 if (ret == 0)
381 exception->retry = 1;
382 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500383}
384
385
Trond Myklebust452e9352010-07-31 14:29:06 -0400386static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 spin_lock(&clp->cl_lock);
389 if (time_before(clp->cl_last_renewal,timestamp))
390 clp->cl_last_renewal = timestamp;
391 spin_unlock(&clp->cl_lock);
392}
393
Trond Myklebust452e9352010-07-31 14:29:06 -0400394static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
395{
396 do_renew_lease(server->nfs_client, timestamp);
397}
398
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400399#if defined(CONFIG_NFS_V4_1)
400
Benny Halevy510b8172009-04-01 09:22:14 -0400401/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400402 * nfs4_free_slot - free a slot and efficiently update slot table.
403 *
404 * freeing a slot is trivially done by clearing its respective bit
405 * in the bitmap.
406 * If the freed slotid equals highest_used_slotid we want to update it
407 * so that the server would be able to size down the slot table if needed,
408 * otherwise we know that the highest_used_slotid is still in use.
409 * When updating highest_used_slotid there may be "holes" in the bitmap
410 * so we need to scan down from highest_used_slotid to 0 looking for the now
411 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500412 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500413 *
414 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400415 */
416static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500417nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400418{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500419 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400420 /* clear used bit in bitmap */
421 __clear_bit(slotid, tbl->used_slots);
422
423 /* update highest_used_slotid when it is freed */
424 if (slotid == tbl->highest_used_slotid) {
425 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500426 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400427 tbl->highest_used_slotid = slotid;
428 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500429 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400430 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500431 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
432 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400433}
434
Trond Myklebust961a8282012-01-17 22:57:37 -0500435bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
436{
437 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
438 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400439}
440
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800441/*
Andy Adamson42acd022011-01-06 02:04:34 +0000442 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800443 */
Andy Adamson42acd022011-01-06 02:04:34 +0000444static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800445{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400446 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500447 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
448 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800449 return;
450 }
451
Trond Myklebust45d43c22012-02-06 19:38:51 -0500452 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800453 return;
454
Andy Adamson42acd022011-01-06 02:04:34 +0000455 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
456 complete(&ses->fc_slot_table.complete);
457}
458
459/*
460 * Signal state manager thread if session back channel is drained
461 */
462void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
463{
464 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500465 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000466 return;
467 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
468 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800469}
470
Trond Myklebustd185a332010-06-16 09:52:25 -0400471static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400472{
473 struct nfs4_slot_table *tbl;
474
Trond Myklebustd185a332010-06-16 09:52:25 -0400475 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400476 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400477 /* just wake up the next guy waiting since
478 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500479 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500480 return;
Andy Adamson13615872009-04-01 09:22:17 -0400481 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500482
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500483 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500484 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000485 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500486 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400487 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400488}
489
Trond Myklebust14516c32010-07-31 14:29:06 -0400490static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400491{
492 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400493 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400494
495 /*
496 * sr_status remains 1 if an RPC level error occurred. The server
497 * may or may not have processed the sequence operation..
498 * Proceed as if the server received and processed the sequence
499 * operation.
500 */
501 if (res->sr_status == 1)
502 res->sr_status = NFS_OK;
503
Bryan Schumaker468f8612011-04-18 15:57:32 -0400504 /* don't increment the sequence number if the task wasn't sent */
505 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400506 goto out;
507
Andy Adamson691daf32009-12-04 15:55:39 -0500508 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400509 switch (res->sr_status) {
510 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400511 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400512 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400513 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400514 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400515 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500516 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500517 if (res->sr_status_flags != 0)
518 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400519 break;
520 case -NFS4ERR_DELAY:
521 /* The server detected a resend of the RPC call and
522 * returned NFS4ERR_DELAY as per Section 2.10.6.2
523 * of RFC5661.
524 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200525 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400526 __func__,
527 res->sr_slot - res->sr_session->fc_slot_table.slots,
528 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400529 goto out_retry;
530 default:
531 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400532 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400533 }
534out:
535 /* The session may be reset by one of the error handlers. */
536 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400537 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400538 return 1;
539out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400540 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400541 goto out;
542 rpc_delay(task, NFS4_POLL_RETRY_MAX);
543 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400544}
545
Trond Myklebust14516c32010-07-31 14:29:06 -0400546static int nfs4_sequence_done(struct rpc_task *task,
547 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400548{
Trond Myklebust14516c32010-07-31 14:29:06 -0400549 if (res->sr_session == NULL)
550 return 1;
551 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400552}
553
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400554/*
Benny Halevy510b8172009-04-01 09:22:14 -0400555 * nfs4_find_slot - efficiently look for a free slot
556 *
557 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
558 * If found, we mark the slot as used, update the highest_used_slotid,
559 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500560 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400561 *
562 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400563 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500564static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800565nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400566{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500567 u32 slotid;
568 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400569
Trond Myklebust45d43c22012-02-06 19:38:51 -0500570 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400571 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
572 tbl->max_slots);
573 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
574 if (slotid >= tbl->max_slots)
575 goto out;
576 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500577 if (slotid > tbl->highest_used_slotid ||
578 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400579 tbl->highest_used_slotid = slotid;
580 ret_id = slotid;
581out:
582 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
583 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400584 return ret_id;
585}
586
Trond Myklebust9d12b212012-01-17 22:04:25 -0500587static void nfs41_init_sequence(struct nfs4_sequence_args *args,
588 struct nfs4_sequence_res *res, int cache_reply)
589{
590 args->sa_session = NULL;
591 args->sa_cache_this = 0;
592 if (cache_reply)
593 args->sa_cache_this = 1;
594 res->sr_session = NULL;
595 res->sr_slot = NULL;
596}
597
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000598int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599 struct nfs4_sequence_args *args,
600 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601 struct rpc_task *task)
602{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400603 struct nfs4_slot *slot;
604 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500605 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400606
607 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400609 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610 return 0;
611
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400612 tbl = &session->fc_slot_table;
613
614 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400615 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800616 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500617 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500618 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400619 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500620 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500621 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400622 }
623
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800624 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
625 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
626 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
627 spin_unlock(&tbl->slot_tbl_lock);
628 dprintk("%s enforce FIFO order\n", __func__);
629 return -EAGAIN;
630 }
631
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800632 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500633 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400634 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
635 spin_unlock(&tbl->slot_tbl_lock);
636 dprintk("<-- %s: no free slots\n", __func__);
637 return -EAGAIN;
638 }
639 spin_unlock(&tbl->slot_tbl_lock);
640
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800641 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400642 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400643 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400644 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400645
646 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
647
Benny Halevy99fe60d2009-04-01 09:22:29 -0400648 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400649 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400650 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400651 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400652 /*
653 * sr_status is only set in decode_sequence, and so will remain
654 * set to 1 if an rpc level failure occurs.
655 */
656 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657 return 0;
658}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000659EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400660
Trond Myklebust035168a2010-06-16 09:52:26 -0400661int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662 struct nfs4_sequence_args *args,
663 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 struct rpc_task *task)
665{
Trond Myklebust035168a2010-06-16 09:52:26 -0400666 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 int ret = 0;
668
Trond Myklebust9d12b212012-01-17 22:04:25 -0500669 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400670 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400671
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200672 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400673 __func__, session->clp, session, res->sr_slot ?
674 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400675
Trond Myklebust9d12b212012-01-17 22:04:25 -0500676 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677out:
678 dprintk("<-- %s status=%d\n", __func__, ret);
679 return ret;
680}
681
682struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400683 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 struct nfs4_sequence_args *seq_args;
685 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686};
687
688static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
689{
690 struct nfs41_call_sync_data *data = calldata;
691
Trond Myklebust035168a2010-06-16 09:52:26 -0400692 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
693
694 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500695 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696 return;
697 rpc_call_start(task);
698}
699
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800700static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
701{
702 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
703 nfs41_call_sync_prepare(task, calldata);
704}
705
Andy Adamson69ab40c2009-04-01 09:22:19 -0400706static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
707{
708 struct nfs41_call_sync_data *data = calldata;
709
Trond Myklebust14516c32010-07-31 14:29:06 -0400710 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400711}
712
Trond Myklebust17280172012-03-11 13:11:00 -0400713static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400714 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400715 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400716};
717
Trond Myklebust17280172012-03-11 13:11:00 -0400718static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800719 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
720 .rpc_call_done = nfs41_call_sync_done,
721};
722
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
724 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400725 struct rpc_message *msg,
726 struct nfs4_sequence_args *args,
727 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800728 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400729{
730 int ret;
731 struct rpc_task *task;
732 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400733 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400734 .seq_args = args,
735 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400736 };
737 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000738 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400739 .rpc_message = msg,
740 .callback_ops = &nfs41_call_sync_ops,
741 .callback_data = &data
742 };
743
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800744 if (privileged)
745 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400746 task = rpc_run_task(&task_setup);
747 if (IS_ERR(task))
748 ret = PTR_ERR(task);
749 else {
750 ret = task->tk_status;
751 rpc_put_task(task);
752 }
753 return ret;
754}
755
Bryan Schumaker7c513052011-03-24 17:12:24 +0000756int _nfs4_call_sync_session(struct rpc_clnt *clnt,
757 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400758 struct rpc_message *msg,
759 struct nfs4_sequence_args *args,
760 struct nfs4_sequence_res *res,
761 int cache_reply)
762{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500763 nfs41_init_sequence(args, res, cache_reply);
764 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400765}
766
Trond Myklebustdf896452010-06-16 09:52:26 -0400767#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500768static inline
769void nfs41_init_sequence(struct nfs4_sequence_args *args,
770 struct nfs4_sequence_res *res, int cache_reply)
771{
772}
773
Trond Myklebust14516c32010-07-31 14:29:06 -0400774static int nfs4_sequence_done(struct rpc_task *task,
775 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400776{
Trond Myklebust14516c32010-07-31 14:29:06 -0400777 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400778}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400779#endif /* CONFIG_NFS_V4_1 */
780
Bryan Schumaker7c513052011-03-24 17:12:24 +0000781int _nfs4_call_sync(struct rpc_clnt *clnt,
782 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400783 struct rpc_message *msg,
784 struct nfs4_sequence_args *args,
785 struct nfs4_sequence_res *res,
786 int cache_reply)
787{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500788 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000789 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400790}
791
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000792static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000793int nfs4_call_sync(struct rpc_clnt *clnt,
794 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000795 struct rpc_message *msg,
796 struct nfs4_sequence_args *args,
797 struct nfs4_sequence_res *res,
798 int cache_reply)
799{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000800 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
801 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000802}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400803
Trond Myklebust38478b22006-05-25 01:40:57 -0400804static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Trond Myklebust38478b22006-05-25 01:40:57 -0400806 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Trond Myklebust38478b22006-05-25 01:40:57 -0400808 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400809 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700810 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400811 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700812 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400813 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400817 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 struct nfs_openargs o_arg;
819 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100820 struct nfs_open_confirmargs c_arg;
821 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500822 struct nfs4_string owner_name;
823 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400826 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400828 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100830 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400831 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100832 int rpc_status;
833 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834};
835
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400836
837static void nfs4_init_opendata_res(struct nfs4_opendata *p)
838{
839 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400840 p->o_res.seqid = p->o_arg.seqid;
841 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400842 p->o_res.server = p->o_arg.server;
843 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500844 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400845}
846
Al Viro82a2c1b2011-06-22 18:30:55 -0400847static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500848 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400849 const struct iattr *attrs,
850 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100851{
Al Viro82a2c1b2011-06-22 18:30:55 -0400852 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 struct inode *dir = parent->d_inode;
854 struct nfs_server *server = NFS_SERVER(dir);
855 struct nfs4_opendata *p;
856
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400857 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858 if (p == NULL)
859 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400860 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100861 if (p->o_arg.seqid == NULL)
862 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400863 nfs_sb_active(dentry->d_sb);
864 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100865 p->dir = parent;
866 p->owner = sp;
867 atomic_inc(&sp->so_count);
868 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869 p->o_arg.open_flags = flags;
870 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700871 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
872 * will return permission denied for all bits until close */
873 if (!(flags & O_EXCL)) {
874 /* ask server to check for all possible rights as results
875 * are cached */
876 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
877 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
878 p->o_res.access_request = p->o_arg.access;
879 }
David Howells7539bba2006-08-22 20:06:09 -0400880 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400881 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
882 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400883 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100884 p->o_arg.server = server;
885 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400886 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100887 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500888 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500889 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400890
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100891 p->o_arg.u.attrs = &p->attrs;
892 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500893
894 verf[0] = jiffies;
895 verf[1] = current->pid;
896 memcpy(p->o_arg.u.verifier.data, verf,
897 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100898 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100899 p->c_arg.fh = &p->o_res.fh;
900 p->c_arg.stateid = &p->o_res.stateid;
901 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400902 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400903 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100904 return p;
905err_free:
906 kfree(p);
907err:
908 dput(parent);
909 return NULL;
910}
911
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400912static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100913{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400914 struct nfs4_opendata *p = container_of(kref,
915 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400916 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400917
918 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400919 if (p->state != NULL)
920 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400921 nfs4_put_state_owner(p->owner);
922 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400923 dput(p->dentry);
924 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500925 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400926 kfree(p);
927}
928
929static void nfs4_opendata_put(struct nfs4_opendata *p)
930{
931 if (p != NULL)
932 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100933}
934
Trond Myklebust06f814a2006-01-03 09:55:07 +0100935static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
936{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100937 int ret;
938
Trond Myklebust06f814a2006-01-03 09:55:07 +0100939 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100940 return ret;
941}
942
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944{
945 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946
Trond Myklebust536e43d2012-01-17 22:04:26 -0500947 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948 goto out;
949 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400950 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500951 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
952 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400953 break;
954 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500955 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
956 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400957 break;
958 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500959 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
960 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400961 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400963 return ret;
964}
965
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400967{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500968 if (delegation == NULL)
969 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500972 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500974 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400975 return 1;
976}
977
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100979{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100981 case FMODE_WRITE:
982 state->n_wronly++;
983 break;
984 case FMODE_READ:
985 state->n_rdonly++;
986 break;
987 case FMODE_READ|FMODE_WRITE:
988 state->n_rdwr++;
989 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100991}
992
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400994{
995 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500996 nfs4_stateid_copy(&state->stateid, stateid);
997 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400999 case FMODE_READ:
1000 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1001 break;
1002 case FMODE_WRITE:
1003 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1004 break;
1005 case FMODE_READ|FMODE_WRITE:
1006 set_bit(NFS_O_RDWR_STATE, &state->flags);
1007 }
1008}
1009
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001011{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001012 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001014 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001015}
1016
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001019 /*
1020 * Protect the call to nfs4_state_set_mode_locked and
1021 * serialise the stateid update
1022 */
1023 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001024 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001025 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001026 set_bit(NFS_DELEGATED_STATE, &state->flags);
1027 }
1028 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001030 write_sequnlock(&state->seqlock);
1031 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001033 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001037{
1038 struct nfs_inode *nfsi = NFS_I(state->inode);
1039 struct nfs_delegation *deleg_cur;
1040 int ret = 0;
1041
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001043
1044 rcu_read_lock();
1045 deleg_cur = rcu_dereference(nfsi->delegation);
1046 if (deleg_cur == NULL)
1047 goto no_delegation;
1048
1049 spin_lock(&deleg_cur->lock);
1050 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001052 goto no_delegation_unlock;
1053
1054 if (delegation == NULL)
1055 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001056 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001057 goto no_delegation_unlock;
1058
Trond Myklebustb7391f42008-12-23 15:21:52 -05001059 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001060 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001061 ret = 1;
1062no_delegation_unlock:
1063 spin_unlock(&deleg_cur->lock);
1064no_delegation:
1065 rcu_read_unlock();
1066
1067 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001068 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001069 ret = 1;
1070 }
1071
1072 return ret;
1073}
1074
1075
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077{
1078 struct nfs_delegation *delegation;
1079
1080 rcu_read_lock();
1081 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001082 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001083 rcu_read_unlock();
1084 return;
1085 }
1086 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001087 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088}
1089
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001091{
1092 struct nfs4_state *state = opendata->state;
1093 struct nfs_inode *nfsi = NFS_I(state->inode);
1094 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001095 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001096 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001097 nfs4_stateid stateid;
1098 int ret = -EAGAIN;
1099
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001101 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001102 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001103 if (can_open_cached(state, fmode, open_mode)) {
1104 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001105 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001106 goto out_return_state;
1107 }
1108 spin_unlock(&state->owner->so_lock);
1109 }
Trond Myklebust34310432008-12-23 15:21:38 -05001110 rcu_read_lock();
1111 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001112 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001113 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001114 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001115 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001117 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001119 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001120 if (ret != 0)
1121 goto out;
1122 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001123
1124 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001126 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001128out:
1129 return ERR_PTR(ret);
1130out_return_state:
1131 atomic_inc(&state->count);
1132 return state;
1133}
1134
Andy Adamsone23008e2012-10-02 21:07:32 -04001135static void
1136nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1137{
1138 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1139 struct nfs_delegation *delegation;
1140 int delegation_flags = 0;
1141
1142 rcu_read_lock();
1143 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1144 if (delegation)
1145 delegation_flags = delegation->flags;
1146 rcu_read_unlock();
1147 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1148 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1149 "returning a delegation for "
1150 "OPEN(CLAIM_DELEGATE_CUR)\n",
1151 clp->cl_hostname);
1152 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1153 nfs_inode_set_delegation(state->inode,
1154 data->owner->so_cred,
1155 &data->o_res);
1156 else
1157 nfs_inode_reclaim_delegation(state->inode,
1158 data->owner->so_cred,
1159 &data->o_res);
1160}
1161
1162/*
1163 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1164 * and update the nfs4_state.
1165 */
1166static struct nfs4_state *
1167_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1168{
1169 struct inode *inode = data->state->inode;
1170 struct nfs4_state *state = data->state;
1171 int ret;
1172
1173 if (!data->rpc_done) {
1174 ret = data->rpc_status;
1175 goto err;
1176 }
1177
1178 ret = -ESTALE;
1179 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1180 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1181 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1182 goto err;
1183
1184 ret = -ENOMEM;
1185 state = nfs4_get_open_state(inode, data->owner);
1186 if (state == NULL)
1187 goto err;
1188
1189 ret = nfs_refresh_inode(inode, &data->f_attr);
1190 if (ret)
1191 goto err;
1192
1193 if (data->o_res.delegation_type != 0)
1194 nfs4_opendata_check_deleg(data, state);
1195 update_open_stateid(state, &data->o_res.stateid, NULL,
1196 data->o_arg.fmode);
1197
1198 return state;
1199err:
1200 return ERR_PTR(ret);
1201
1202}
1203
1204static struct nfs4_state *
1205_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001206{
1207 struct inode *inode;
1208 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001209 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001210
Trond Myklebustaac00a82007-07-05 19:02:21 -04001211 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001212 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001213 goto out;
1214 }
1215
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001216 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001217 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001218 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001219 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001220 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001221 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001222 goto err;
1223 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001224 state = nfs4_get_open_state(inode, data->owner);
1225 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001226 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001227 if (data->o_res.delegation_type != 0)
1228 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001229 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001230 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001231 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001232out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001233 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001234err_put_inode:
1235 iput(inode);
1236err:
1237 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001238}
1239
Andy Adamsone23008e2012-10-02 21:07:32 -04001240static struct nfs4_state *
1241nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1242{
1243 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1244 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1245 return _nfs4_opendata_to_nfs4_state(data);
1246}
1247
Trond Myklebust864472e2006-01-03 09:55:15 +01001248static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1249{
1250 struct nfs_inode *nfsi = NFS_I(state->inode);
1251 struct nfs_open_context *ctx;
1252
1253 spin_lock(&state->inode->i_lock);
1254 list_for_each_entry(ctx, &nfsi->open_files, list) {
1255 if (ctx->state != state)
1256 continue;
1257 get_nfs_open_context(ctx);
1258 spin_unlock(&state->inode->i_lock);
1259 return ctx;
1260 }
1261 spin_unlock(&state->inode->i_lock);
1262 return ERR_PTR(-ENOENT);
1263}
1264
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001265static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1266{
1267 struct nfs4_opendata *opendata;
1268
Al Viro3d4ff432011-06-22 18:40:12 -04001269 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001270 if (opendata == NULL)
1271 return ERR_PTR(-ENOMEM);
1272 opendata->state = state;
1273 atomic_inc(&state->count);
1274 return opendata;
1275}
1276
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001277static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001278{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001279 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 int ret;
1281
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001282 opendata->o_arg.open_flags = 0;
1283 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001284 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1285 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1286 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001287 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 if (ret != 0)
1289 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001290 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001291 if (IS_ERR(newstate))
1292 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001293 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001294 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001295 return 0;
1296}
1297
1298static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1299{
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001301 int ret;
1302
1303 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001304 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 smp_rmb();
1306 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001307 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001308 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001309 if (ret != 0)
1310 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001311 if (newstate != state)
1312 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001313 }
1314 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001315 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001316 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001317 if (ret != 0)
1318 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001319 if (newstate != state)
1320 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001321 }
1322 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001323 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001324 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001325 if (ret != 0)
1326 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001327 if (newstate != state)
1328 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001329 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001330 /*
1331 * We may have performed cached opens for all three recoveries.
1332 * Check if we need to update the current stateid.
1333 */
1334 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001335 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001336 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001337 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001338 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001339 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001340 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001341 return 0;
1342}
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344/*
1345 * OPEN_RECLAIM:
1346 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001348static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001350 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001351 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 int status;
1354
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001355 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1356 if (IS_ERR(opendata))
1357 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001358 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1359 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001360 rcu_read_lock();
1361 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001362 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001363 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001364 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001365 opendata->o_arg.u.delegation_type = delegation_type;
1366 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001367 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return status;
1369}
1370
Trond Myklebust539cd032007-06-05 11:46:42 -04001371static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 struct nfs_server *server = NFS_SERVER(state->inode);
1374 struct nfs4_exception exception = { };
1375 int err;
1376 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001377 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001378 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001379 break;
1380 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 } while (exception.retry);
1382 return err;
1383}
1384
Trond Myklebust864472e2006-01-03 09:55:15 +01001385static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1386{
1387 struct nfs_open_context *ctx;
1388 int ret;
1389
1390 ctx = nfs4_state_find_open_context(state);
1391 if (IS_ERR(ctx))
1392 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001393 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001394 put_nfs_open_context(ctx);
1395 return ret;
1396}
1397
Trond Myklebust13437e12007-07-06 15:10:43 -04001398static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001400 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001401 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001403 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1404 if (IS_ERR(opendata))
1405 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001406 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001407 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001408 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001409 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001410 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Trond Myklebust13437e12007-07-06 15:10:43 -04001413int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001416 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 int err;
1418 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001419 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 switch (err) {
1421 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001422 case -ENOENT:
1423 case -ESTALE:
1424 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001425 case -NFS4ERR_BADSESSION:
1426 case -NFS4ERR_BADSLOT:
1427 case -NFS4ERR_BAD_HIGH_SLOT:
1428 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1429 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001430 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001431 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 case -NFS4ERR_STALE_CLIENTID:
1433 case -NFS4ERR_STALE_STATEID:
1434 case -NFS4ERR_EXPIRED:
1435 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001436 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001437 goto out;
1438 case -ERESTARTSYS:
1439 /*
1440 * The show must go on: exit, but mark the
1441 * stateid as needing recovery.
1442 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001443 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001444 case -NFS4ERR_ADMIN_REVOKED:
1445 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001446 nfs_inode_find_state_and_recover(state->inode,
1447 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001448 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001449 case -EKEYEXPIRED:
1450 /*
1451 * User RPCSEC_GSS context has expired.
1452 * We cannot recover this stateid now, so
1453 * skip it and allow recovery thread to
1454 * proceed.
1455 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001456 case -ENOMEM:
1457 err = 0;
1458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460 err = nfs4_handle_exception(server, err, &exception);
1461 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001462out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return err;
1464}
1465
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001466static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1467{
1468 struct nfs4_opendata *data = calldata;
1469
1470 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001471 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001472 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001473 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001474 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001475 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001476 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001477}
1478
1479static void nfs4_open_confirm_release(void *calldata)
1480{
1481 struct nfs4_opendata *data = calldata;
1482 struct nfs4_state *state = NULL;
1483
1484 /* If this request hasn't been cancelled, do nothing */
1485 if (data->cancelled == 0)
1486 goto out_free;
1487 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001488 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001489 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001490 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001491 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001492 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001493out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001494 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001495}
1496
1497static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001498 .rpc_call_done = nfs4_open_confirm_done,
1499 .rpc_release = nfs4_open_confirm_release,
1500};
1501
1502/*
1503 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1504 */
1505static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1506{
1507 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1508 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001509 struct rpc_message msg = {
1510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1511 .rpc_argp = &data->c_arg,
1512 .rpc_resp = &data->c_res,
1513 .rpc_cred = data->owner->so_cred,
1514 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001515 struct rpc_task_setup task_setup_data = {
1516 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001517 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001518 .callback_ops = &nfs4_open_confirm_ops,
1519 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001520 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001521 .flags = RPC_TASK_ASYNC,
1522 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 int status;
1524
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001525 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001526 data->rpc_done = 0;
1527 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001528 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001529 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001530 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001531 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001532 status = nfs4_wait_for_completion_rpc_task(task);
1533 if (status != 0) {
1534 data->cancelled = 1;
1535 smp_wmb();
1536 } else
1537 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001538 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return status;
1540}
1541
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 struct nfs4_opendata *data = calldata;
1545 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001546
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001547 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1548 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001549 /*
1550 * Check if we still need to send an OPEN call, or if we can use
1551 * a delegation instead.
1552 */
1553 if (data->state != NULL) {
1554 struct nfs_delegation *delegation;
1555
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001556 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001557 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001558 rcu_read_lock();
1559 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001560 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1561 can_open_delegated(delegation, data->o_arg.fmode))
1562 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001563 rcu_read_unlock();
1564 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001565 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001566 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001567 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001568 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001569 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001570 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1571 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001572 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001573 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001574 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001575 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001576 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001577 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001578 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001579unlock_no_action:
1580 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001581out_no_action:
1582 task->tk_action = NULL;
1583
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001584}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001586static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1587{
1588 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1589 nfs4_open_prepare(task, calldata);
1590}
1591
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001592static void nfs4_open_done(struct rpc_task *task, void *calldata)
1593{
1594 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001596 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001597
Trond Myklebust14516c32010-07-31 14:29:06 -04001598 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1599 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001600
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001601 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001602 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1603 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001604 case S_IFREG:
1605 break;
1606 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001607 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001608 break;
1609 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001611 break;
1612 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001613 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001614 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001615 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001616 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001617 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1618 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001619 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001620 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001621}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001622
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001623static void nfs4_open_release(void *calldata)
1624{
1625 struct nfs4_opendata *data = calldata;
1626 struct nfs4_state *state = NULL;
1627
1628 /* If this request hasn't been cancelled, do nothing */
1629 if (data->cancelled == 0)
1630 goto out_free;
1631 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001632 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001633 goto out_free;
1634 /* In case we need an open_confirm, no cleanup! */
1635 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1636 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001637 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001638 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001639 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001640out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001641 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001642}
1643
1644static const struct rpc_call_ops nfs4_open_ops = {
1645 .rpc_call_prepare = nfs4_open_prepare,
1646 .rpc_call_done = nfs4_open_done,
1647 .rpc_release = nfs4_open_release,
1648};
1649
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001650static const struct rpc_call_ops nfs4_recover_open_ops = {
1651 .rpc_call_prepare = nfs4_recover_open_prepare,
1652 .rpc_call_done = nfs4_open_done,
1653 .rpc_release = nfs4_open_release,
1654};
1655
1656static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001657{
1658 struct inode *dir = data->dir->d_inode;
1659 struct nfs_server *server = NFS_SERVER(dir);
1660 struct nfs_openargs *o_arg = &data->o_arg;
1661 struct nfs_openres *o_res = &data->o_res;
1662 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001663 struct rpc_message msg = {
1664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1665 .rpc_argp = o_arg,
1666 .rpc_resp = o_res,
1667 .rpc_cred = data->owner->so_cred,
1668 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001669 struct rpc_task_setup task_setup_data = {
1670 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001671 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001672 .callback_ops = &nfs4_open_ops,
1673 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001674 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001675 .flags = RPC_TASK_ASYNC,
1676 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001677 int status;
1678
Trond Myklebust9d12b212012-01-17 22:04:25 -05001679 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001680 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001681 data->rpc_done = 0;
1682 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001683 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001684 if (isrecover)
1685 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001686 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001687 if (IS_ERR(task))
1688 return PTR_ERR(task);
1689 status = nfs4_wait_for_completion_rpc_task(task);
1690 if (status != 0) {
1691 data->cancelled = 1;
1692 smp_wmb();
1693 } else
1694 status = data->rpc_status;
1695 rpc_put_task(task);
1696
1697 return status;
1698}
1699
1700static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1701{
1702 struct inode *dir = data->dir->d_inode;
1703 struct nfs_openres *o_res = &data->o_res;
1704 int status;
1705
1706 status = nfs4_run_open_task(data, 1);
1707 if (status != 0 || !data->rpc_done)
1708 return status;
1709
Trond Myklebust6926afd2012-01-07 13:22:46 -05001710 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1711
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001712 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1713 status = _nfs4_proc_open_confirm(data);
1714 if (status != 0)
1715 return status;
1716 }
1717
1718 return status;
1719}
1720
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001721static int nfs4_opendata_access(struct rpc_cred *cred,
1722 struct nfs4_opendata *opendata,
1723 struct nfs4_state *state, fmode_t fmode)
1724{
1725 struct nfs_access_entry cache;
1726 u32 mask;
1727
1728 /* access call failed or for some reason the server doesn't
1729 * support any access modes -- defer access call until later */
1730 if (opendata->o_res.access_supported == 0)
1731 return 0;
1732
1733 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001734 /* don't check MAY_WRITE - a newly created file may not have
1735 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001736 if (fmode & FMODE_READ)
1737 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001738 if (fmode & FMODE_EXEC)
1739 mask |= MAY_EXEC;
1740
1741 cache.cred = cred;
1742 cache.jiffies = jiffies;
1743 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1744 nfs_access_add_cache(state->inode, &cache);
1745
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001746 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001747 return 0;
1748
1749 /* even though OPEN succeeded, access is denied. Close the file */
1750 nfs4_close_state(state, fmode);
1751 return -NFS4ERR_ACCESS;
1752}
1753
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001754/*
1755 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1756 */
1757static int _nfs4_proc_open(struct nfs4_opendata *data)
1758{
1759 struct inode *dir = data->dir->d_inode;
1760 struct nfs_server *server = NFS_SERVER(dir);
1761 struct nfs_openargs *o_arg = &data->o_arg;
1762 struct nfs_openres *o_res = &data->o_res;
1763 int status;
1764
1765 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001766 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001768 if (status != 0) {
1769 if (status == -NFS4ERR_BADNAME &&
1770 !(o_arg->open_flags & O_CREAT))
1771 return -ENOENT;
1772 return status;
1773 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001774
Trond Myklebust6926afd2012-01-07 13:22:46 -05001775 nfs_fattr_map_and_free_names(server, &data->f_attr);
1776
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001777 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001778 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001779 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1780 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001782 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001784 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001787 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Andy Adamsond83217c2011-03-01 01:34:17 +00001791static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001792{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001793 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001794 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001795
Trond Myklebusta78cb572009-08-09 15:06:19 -04001796 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001797 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001798 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001799 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001800 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1801 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001802 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001803 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001804 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001805 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001806 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001807}
1808
Andy Adamsond83217c2011-03-01 01:34:17 +00001809static int nfs4_recover_expired_lease(struct nfs_server *server)
1810{
1811 return nfs4_client_recover_expired_lease(server->nfs_client);
1812}
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814/*
1815 * OPEN_EXPIRED:
1816 * reclaim state on the server after a network partition.
1817 * Assumes caller holds the appropriate lock
1818 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001819static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001821 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001822 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001824 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1825 if (IS_ERR(opendata))
1826 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001827 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001828 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001829 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001830 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001831 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001834static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001835{
Trond Myklebust539cd032007-06-05 11:46:42 -04001836 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001837 struct nfs4_exception exception = { };
1838 int err;
1839
1840 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001841 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001842 switch (err) {
1843 default:
1844 goto out;
1845 case -NFS4ERR_GRACE:
1846 case -NFS4ERR_DELAY:
1847 nfs4_handle_exception(server, err, &exception);
1848 err = 0;
1849 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001850 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001851out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001852 return err;
1853}
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1856{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001858 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Trond Myklebust864472e2006-01-03 09:55:15 +01001860 ctx = nfs4_state_find_open_context(state);
1861 if (IS_ERR(ctx))
1862 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001863 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001864 put_nfs_open_context(ctx);
1865 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001868#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001869static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001870{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001871 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001872 nfs4_stateid *stateid = &state->stateid;
1873 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001874
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001875 /* If a state reset has been done, test_stateid is unneeded */
1876 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1877 return;
1878
1879 status = nfs41_test_stateid(server, stateid);
1880 if (status != NFS_OK) {
1881 /* Free the stateid unless the server explicitly
1882 * informs us the stateid is unrecognized. */
1883 if (status != -NFS4ERR_BAD_STATEID)
1884 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001885 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001886
Bryan Schumaker69388672012-09-26 15:25:52 -04001887 write_seqlock(&state->seqlock);
1888 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1889 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001890 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1891 }
1892}
1893
1894/**
1895 * nfs41_check_open_stateid - possibly free an open stateid
1896 *
1897 * @state: NFSv4 state for an inode
1898 *
1899 * Returns NFS_OK if recovery for this stateid is now finished.
1900 * Otherwise a negative NFS4ERR value is returned.
1901 */
1902static int nfs41_check_open_stateid(struct nfs4_state *state)
1903{
1904 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001905 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001906 int status;
1907
1908 /* If a state reset has been done, test_stateid is unneeded */
1909 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1910 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1911 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1912 return -NFS4ERR_BAD_STATEID;
1913
1914 status = nfs41_test_stateid(server, stateid);
1915 if (status != NFS_OK) {
1916 /* Free the stateid unless the server explicitly
1917 * informs us the stateid is unrecognized. */
1918 if (status != -NFS4ERR_BAD_STATEID)
1919 nfs41_free_stateid(server, stateid);
1920
1921 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1922 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1923 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001924 }
1925 return status;
1926}
1927
1928static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1929{
Chuck Levereb64cf92012-07-11 16:30:05 -04001930 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001931
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001932 nfs41_clear_delegation_stateid(state);
1933 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001934 if (status != NFS_OK)
1935 status = nfs4_open_expired(sp, state);
1936 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001937}
1938#endif
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001941 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1942 * fields corresponding to attributes that were used to store the verifier.
1943 * Make sure we clobber those fields in the later setattr call
1944 */
1945static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1946{
1947 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1948 !(sattr->ia_valid & ATTR_ATIME_SET))
1949 sattr->ia_valid |= ATTR_ATIME;
1950
1951 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1952 !(sattr->ia_valid & ATTR_MTIME_SET))
1953 sattr->ia_valid |= ATTR_MTIME;
1954}
1955
1956/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001957 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 */
Andy Adamson82be4172012-05-23 05:02:35 -04001959static int _nfs4_do_open(struct inode *dir,
1960 struct dentry *dentry,
1961 fmode_t fmode,
1962 int flags,
1963 struct iattr *sattr,
1964 struct rpc_cred *cred,
1965 struct nfs4_state **res,
1966 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 struct nfs4_state_owner *sp;
1969 struct nfs4_state *state = NULL;
1970 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001971 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001972 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001976 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1977 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1979 goto out_err;
1980 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001981 status = nfs4_recover_expired_lease(server);
1982 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001983 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001984 if (dentry->d_inode != NULL)
1985 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001986 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001987 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001988 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001989 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Andy Adamson82be4172012-05-23 05:02:35 -04001991 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1992 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1993 if (!opendata->f_attr.mdsthreshold)
1994 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001995 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001996 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001997 if (dentry->d_inode != NULL)
1998 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001999
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002000 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002002 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002004 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002005 status = PTR_ERR(state);
2006 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002007 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002008 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002009 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002010
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002011 status = nfs4_opendata_access(cred, opendata, state, fmode);
2012 if (status != 0)
2013 goto err_opendata_put;
2014
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002015 if (opendata->o_arg.open_flags & O_EXCL) {
2016 nfs4_exclusive_attrset(opendata, sattr);
2017
2018 nfs_fattr_init(opendata->o_res.f_attr);
2019 status = nfs4_do_setattr(state->inode, cred,
2020 opendata->o_res.f_attr, sattr,
2021 state);
2022 if (status == 0)
2023 nfs_setattr_update_inode(state->inode, sattr);
2024 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2025 }
Andy Adamson82be4172012-05-23 05:02:35 -04002026
2027 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2028 *ctx_th = opendata->f_attr.mdsthreshold;
2029 else
2030 kfree(opendata->f_attr.mdsthreshold);
2031 opendata->f_attr.mdsthreshold = NULL;
2032
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002033 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 *res = state;
2036 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002037err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002038 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002039 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002040err_put_state_owner:
2041 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 *res = NULL;
2044 return status;
2045}
2046
2047
Andy Adamson82be4172012-05-23 05:02:35 -04002048static struct nfs4_state *nfs4_do_open(struct inode *dir,
2049 struct dentry *dentry,
2050 fmode_t fmode,
2051 int flags,
2052 struct iattr *sattr,
2053 struct rpc_cred *cred,
2054 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 struct nfs4_exception exception = { };
2057 struct nfs4_state *res;
2058 int status;
2059
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002060 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002062 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2063 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (status == 0)
2065 break;
2066 /* NOTE: BAD_SEQID means the server and client disagree about the
2067 * book-keeping w.r.t. state-changing operations
2068 * (OPEN/CLOSE/LOCK/LOCKU...)
2069 * It is actually a sign of a bug on the client or on the server.
2070 *
2071 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002072 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 * have unhashed the old state_owner for us, and that we can
2074 * therefore safely retry using a new one. We should still warn
2075 * the user though...
2076 */
2077 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002078 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002079 " returned a bad sequence-id error!\n",
2080 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 exception.retry = 1;
2082 continue;
2083 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002084 /*
2085 * BAD_STATEID on OPEN means that the server cancelled our
2086 * state before it received the OPEN_CONFIRM.
2087 * Recover by retrying the request as per the discussion
2088 * on Page 181 of RFC3530.
2089 */
2090 if (status == -NFS4ERR_BAD_STATEID) {
2091 exception.retry = 1;
2092 continue;
2093 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002094 if (status == -EAGAIN) {
2095 /* We must have found a delegation */
2096 exception.retry = 1;
2097 continue;
2098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2100 status, &exception));
2101 } while (exception.retry);
2102 return res;
2103}
2104
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002105static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2106 struct nfs_fattr *fattr, struct iattr *sattr,
2107 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002109 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002111 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 .iap = sattr,
2113 .server = server,
2114 .bitmask = server->attr_bitmask,
2115 };
2116 struct nfs_setattrres res = {
2117 .fattr = fattr,
2118 .server = server,
2119 };
2120 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2122 .rpc_argp = &arg,
2123 .rpc_resp = &res,
2124 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002126 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002127 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Trond Myklebust0e574af2005-10-27 22:12:38 -04002129 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Trond Myklebust4fc87962012-03-08 17:42:01 -05002131 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002132 struct nfs_lockowner lockowner = {
2133 .l_owner = current->files,
2134 .l_pid = current->tgid,
2135 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002136 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002137 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002138 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2139 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002140 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002141 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002142 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Bryan Schumaker7c513052011-03-24 17:12:24 +00002144 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002145 if (status == 0 && state != NULL)
2146 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002147 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002150static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2151 struct nfs_fattr *fattr, struct iattr *sattr,
2152 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002154 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002155 struct nfs4_exception exception = {
2156 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002157 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002158 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 int err;
2160 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002161 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2162 switch (err) {
2163 case -NFS4ERR_OPENMODE:
2164 if (state && !(state->state & FMODE_WRITE)) {
2165 err = -EBADF;
2166 if (sattr->ia_valid & ATTR_OPEN)
2167 err = -EACCES;
2168 goto out;
2169 }
2170 }
2171 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002173out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return err;
2175}
2176
2177struct nfs4_closedata {
2178 struct inode *inode;
2179 struct nfs4_state *state;
2180 struct nfs_closeargs arg;
2181 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002182 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002183 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002184 bool roc;
2185 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186};
2187
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002188static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002189{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002190 struct nfs4_closedata *calldata = data;
2191 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002192 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002193
Fred Isamanf7e89172011-01-06 11:36:32 +00002194 if (calldata->roc)
2195 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002196 nfs4_put_open_state(calldata->state);
2197 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002198 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002199 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002200 kfree(calldata);
2201}
2202
Trond Myklebust88069f72009-12-08 08:33:16 -05002203static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2204 fmode_t fmode)
2205{
2206 spin_lock(&state->owner->so_lock);
2207 if (!(fmode & FMODE_READ))
2208 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2209 if (!(fmode & FMODE_WRITE))
2210 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2211 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2212 spin_unlock(&state->owner->so_lock);
2213}
2214
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002215static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002217 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 struct nfs_server *server = NFS_SERVER(calldata->inode);
2220
Chuck Levera3ca5652012-03-01 17:00:40 -05002221 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002222 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2223 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /* hmm. we are done with the inode, and in the process of freeing
2225 * the state_owner. we keep this around to process errors
2226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 switch (task->tk_status) {
2228 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002229 if (calldata->roc)
2230 pnfs_roc_set_barrier(state->inode,
2231 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002232 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002233 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002234 nfs4_close_clear_stateid_flags(state,
2235 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 break;
2237 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002238 case -NFS4ERR_OLD_STATEID:
2239 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002241 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002242 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002244 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2245 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002247 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002248 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002249 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
2251
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002252static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002254 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002255 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002256 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002257 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002258
Chuck Levera3ca5652012-03-01 17:00:40 -05002259 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002260 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002261 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002262
Trond Myklebust88069f72009-12-08 08:33:16 -05002263 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2264 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002265 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002266 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002267 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002268 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002269 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2270 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2271 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002272 }
2273 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002274 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2275 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2276 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002277 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002278 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002279 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002280
2281 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002282 /* Note: exit _without_ calling nfs4_close_done */
2283 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002284 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002285 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002286
Fred Isamanf7e89172011-01-06 11:36:32 +00002287 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002288 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002289 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002290 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002291 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002292 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002293
Trond Myklebust516a6af2005-10-27 22:12:41 -04002294 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002295 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002296 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002297 &calldata->arg.seq_args,
2298 &calldata->res.seq_res,
2299 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002300 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002301 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));
2534 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002535
2536 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002537 /* AUTH_UNIX is the default flavor if none was specified,
2538 * thus has already been tried. */
2539 if (flav_array[i] == RPC_AUTH_UNIX)
2540 continue;
2541
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002542 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002543 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002544 continue;
2545 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002546 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002547 /*
2548 * -EACCESS could mean that the user doesn't have correct permissions
2549 * to access the mount. It could also mean that we tried to mount
2550 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2551 * existing mount programs don't handle -EACCES very well so it should
2552 * be mapped to -EPERM instead.
2553 */
2554 if (status == -EACCES)
2555 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002556 return status;
2557}
2558
2559/*
2560 * get the file handle for the "/" directory on the server
2561 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002562int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2563 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002564{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002565 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002566 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002567 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2568 /*
2569 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2570 * by nfs4_map_errors() as this function exits.
2571 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002572 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (status == 0)
2574 status = nfs4_server_capabilities(server, fhandle);
2575 if (status == 0)
2576 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002577 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
Bryan Schumakerbae36242012-05-10 15:07:31 -04002580static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2581 struct nfs_fsinfo *info)
2582{
2583 int error;
2584 struct nfs_fattr *fattr = info->fattr;
2585
2586 error = nfs4_server_capabilities(server, mntfh);
2587 if (error < 0) {
2588 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2589 return error;
2590 }
2591
2592 error = nfs4_proc_getattr(server, mntfh, fattr);
2593 if (error < 0) {
2594 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2595 return error;
2596 }
2597
2598 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2599 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2600 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2601
2602 return error;
2603}
2604
Manoj Naik6b97fd32006-06-09 09:34:29 -04002605/*
2606 * Get locations and (maybe) other attributes of a referral.
2607 * Note that we'll actually follow the referral later when
2608 * we detect fsid mismatch in inode revalidation
2609 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002610static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2611 const struct qstr *name, struct nfs_fattr *fattr,
2612 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002613{
2614 int status = -ENOMEM;
2615 struct page *page = NULL;
2616 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002617
2618 page = alloc_page(GFP_KERNEL);
2619 if (page == NULL)
2620 goto out;
2621 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2622 if (locations == NULL)
2623 goto out;
2624
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002625 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002626 if (status != 0)
2627 goto out;
2628 /* Make sure server returned a different fsid for the referral */
2629 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002630 dprintk("%s: server did not return a different fsid for"
2631 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002632 status = -EIO;
2633 goto out;
2634 }
Andy Adamson533eb462011-06-13 18:25:56 -04002635 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2636 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002637
Andy Adamson533eb462011-06-13 18:25:56 -04002638 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002639 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002640 memset(fhandle, 0, sizeof(struct nfs_fh));
2641out:
2642 if (page)
2643 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002644 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002645 return status;
2646}
2647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2649{
2650 struct nfs4_getattr_arg args = {
2651 .fh = fhandle,
2652 .bitmask = server->attr_bitmask,
2653 };
2654 struct nfs4_getattr_res res = {
2655 .fattr = fattr,
2656 .server = server,
2657 };
2658 struct rpc_message msg = {
2659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2660 .rpc_argp = &args,
2661 .rpc_resp = &res,
2662 };
2663
Trond Myklebust0e574af2005-10-27 22:12:38 -04002664 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002665 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
2668static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2669{
2670 struct nfs4_exception exception = { };
2671 int err;
2672 do {
2673 err = nfs4_handle_exception(server,
2674 _nfs4_proc_getattr(server, fhandle, fattr),
2675 &exception);
2676 } while (exception.retry);
2677 return err;
2678}
2679
2680/*
2681 * The file is not closed if it is opened due to the a request to change
2682 * the size of the file. The open call will not be needed once the
2683 * VFS layer lookup-intents are implemented.
2684 *
2685 * Close is called when the inode is destroyed.
2686 * If we haven't opened the file for O_WRONLY, we
2687 * need to in the size_change case to obtain a stateid.
2688 *
2689 * Got race?
2690 * Because OPEN is always done by name in nfsv4, it is
2691 * possible that we opened a different file by the same
2692 * name. We can recognize this race condition, but we
2693 * can't do anything about it besides returning an error.
2694 *
2695 * This will be fixed with VFS changes (lookup-intent).
2696 */
2697static int
2698nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2699 struct iattr *sattr)
2700{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002701 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002702 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002703 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 int status;
2705
Benny Halevy8a1636c2010-07-14 15:43:57 -04002706 if (pnfs_ld_layoutret_on_setattr(inode))
2707 pnfs_return_layout(inode);
2708
Trond Myklebust0e574af2005-10-27 22:12:38 -04002709 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Andy Adamson26699402012-05-30 16:12:24 -04002711 /* Deal with open(O_TRUNC) */
2712 if (sattr->ia_valid & ATTR_OPEN)
2713 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2714
2715 /* Optimization: if the end result is no change, don't RPC */
2716 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2717 return 0;
2718
Trond Myklebustd5308382005-11-04 15:33:38 -05002719 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002720 if (sattr->ia_valid & ATTR_FILE) {
2721 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002722
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002723 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002724 if (ctx) {
2725 cred = ctx->cred;
2726 state = ctx->state;
2727 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002728 }
2729
2730 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002731 if (status == 0)
2732 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return status;
2734}
2735
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002736static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2737 const struct qstr *name, struct nfs_fh *fhandle,
2738 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002739{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002740 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002741 int status;
2742 struct nfs4_lookup_arg args = {
2743 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002744 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002745 .name = name,
2746 };
2747 struct nfs4_lookup_res res = {
2748 .server = server,
2749 .fattr = fattr,
2750 .fh = fhandle,
2751 };
2752 struct rpc_message msg = {
2753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2754 .rpc_argp = &args,
2755 .rpc_resp = &res,
2756 };
2757
2758 nfs_fattr_init(fattr);
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002761 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 dprintk("NFS reply lookup: %d\n", status);
2763 return status;
2764}
2765
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002766static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002767{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002768 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002769 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002770 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2771 fattr->nlink = 2;
2772}
2773
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002774static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2775 struct qstr *name, struct nfs_fh *fhandle,
2776 struct nfs_fattr *fattr)
2777{
2778 struct nfs4_exception exception = { };
2779 struct rpc_clnt *client = *clnt;
2780 int err;
2781 do {
2782 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2783 switch (err) {
2784 case -NFS4ERR_BADNAME:
2785 err = -ENOENT;
2786 goto out;
2787 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002788 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002789 goto out;
2790 case -NFS4ERR_WRONGSEC:
2791 err = -EPERM;
2792 if (client != *clnt)
2793 goto out;
2794
2795 client = nfs4_create_sec_client(client, dir, name);
2796 if (IS_ERR(client))
2797 return PTR_ERR(client);
2798
2799 exception.retry = 1;
2800 break;
2801 default:
2802 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2803 }
2804 } while (exception.retry);
2805
2806out:
2807 if (err == 0)
2808 *clnt = client;
2809 else if (client != *clnt)
2810 rpc_shutdown_client(client);
2811
2812 return err;
2813}
2814
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002815static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002816 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002818 int status;
2819 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002820
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002821 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2822 if (client != NFS_CLIENT(dir)) {
2823 rpc_shutdown_client(client);
2824 nfs_fixup_secinfo_attributes(fattr);
2825 }
2826 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
2828
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002829struct rpc_clnt *
2830nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2831 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2832{
2833 int status;
2834 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2835
2836 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2837 if (status < 0) {
2838 rpc_shutdown_client(client);
2839 return ERR_PTR(status);
2840 }
2841 return client;
2842}
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2845{
Trond Myklebust76b32992007-08-10 17:45:11 -04002846 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 struct nfs4_accessargs args = {
2848 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002849 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002851 struct nfs4_accessres res = {
2852 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002853 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 struct rpc_message msg = {
2855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2856 .rpc_argp = &args,
2857 .rpc_resp = &res,
2858 .rpc_cred = entry->cred,
2859 };
2860 int mode = entry->mask;
2861 int status;
2862
2863 /*
2864 * Determine which access bits we want to ask for...
2865 */
2866 if (mode & MAY_READ)
2867 args.access |= NFS4_ACCESS_READ;
2868 if (S_ISDIR(inode->i_mode)) {
2869 if (mode & MAY_WRITE)
2870 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2871 if (mode & MAY_EXEC)
2872 args.access |= NFS4_ACCESS_LOOKUP;
2873 } else {
2874 if (mode & MAY_WRITE)
2875 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2876 if (mode & MAY_EXEC)
2877 args.access |= NFS4_ACCESS_EXECUTE;
2878 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002879
2880 res.fattr = nfs_alloc_fattr();
2881 if (res.fattr == NULL)
2882 return -ENOMEM;
2883
Bryan Schumaker7c513052011-03-24 17:12:24 +00002884 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002886 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002887 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002889 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return status;
2891}
2892
2893static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2894{
2895 struct nfs4_exception exception = { };
2896 int err;
2897 do {
2898 err = nfs4_handle_exception(NFS_SERVER(inode),
2899 _nfs4_proc_access(inode, entry),
2900 &exception);
2901 } while (exception.retry);
2902 return err;
2903}
2904
2905/*
2906 * TODO: For the time being, we don't try to get any attributes
2907 * along with any of the zero-copy operations READ, READDIR,
2908 * READLINK, WRITE.
2909 *
2910 * In the case of the first three, we want to put the GETATTR
2911 * after the read-type operation -- this is because it is hard
2912 * to predict the length of a GETATTR response in v4, and thus
2913 * align the READ data correctly. This means that the GETATTR
2914 * may end up partially falling into the page cache, and we should
2915 * shift it into the 'tail' of the xdr_buf before processing.
2916 * To do this efficiently, we need to know the total length
2917 * of data received, which doesn't seem to be available outside
2918 * of the RPC layer.
2919 *
2920 * In the case of WRITE, we also want to put the GETATTR after
2921 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002922 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 *
2924 * Both of these changes to the XDR layer would in fact be quite
2925 * minor, but I decided to leave them for a subsequent patch.
2926 */
2927static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2928 unsigned int pgbase, unsigned int pglen)
2929{
2930 struct nfs4_readlink args = {
2931 .fh = NFS_FH(inode),
2932 .pgbase = pgbase,
2933 .pglen = pglen,
2934 .pages = &page,
2935 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002936 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 struct rpc_message msg = {
2938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2939 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002940 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 };
2942
Bryan Schumaker7c513052011-03-24 17:12:24 +00002943 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 -07002944}
2945
2946static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2947 unsigned int pgbase, unsigned int pglen)
2948{
2949 struct nfs4_exception exception = { };
2950 int err;
2951 do {
2952 err = nfs4_handle_exception(NFS_SERVER(inode),
2953 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2954 &exception);
2955 } while (exception.retry);
2956 return err;
2957}
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002960 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962static int
2963nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002964 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002966 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 int status = 0;
2969
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002970 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2971 if (IS_ERR(ctx))
2972 return PTR_ERR(ctx);
2973
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002974 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002975 state = nfs4_do_open(dir, dentry, ctx->mode,
2976 flags, sattr, ctx->cred,
2977 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002978 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 if (IS_ERR(state)) {
2980 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002981 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002983 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002984 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002985 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002987 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 return status;
2989}
2990
2991static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2992{
Trond Myklebust16e42952005-10-27 22:12:44 -04002993 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002994 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002996 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002998 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002999 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003000 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3003 .rpc_argp = &args,
3004 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003006 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003007
Bryan Schumaker7c513052011-03-24 17:12:24 +00003008 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003009 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003010 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return status;
3012}
3013
3014static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3015{
3016 struct nfs4_exception exception = { };
3017 int err;
3018 do {
3019 err = nfs4_handle_exception(NFS_SERVER(dir),
3020 _nfs4_proc_remove(dir, name),
3021 &exception);
3022 } while (exception.retry);
3023 return err;
3024}
3025
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003026static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003028 struct nfs_server *server = NFS_SERVER(dir);
3029 struct nfs_removeargs *args = msg->rpc_argp;
3030 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003032 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003034 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035}
3036
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003037static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3038{
3039 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3040 &data->args.seq_args,
3041 &data->res.seq_res,
3042 task))
3043 return;
3044 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003047static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003049 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3050
Trond Myklebust14516c32010-07-31 14:29:06 -04003051 if (!nfs4_sequence_done(task, &res->seq_res))
3052 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003053 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003054 return 0;
3055 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003056 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
Jeff Laytond3d41522010-09-17 17:31:57 -04003059static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3060{
3061 struct nfs_server *server = NFS_SERVER(dir);
3062 struct nfs_renameargs *arg = msg->rpc_argp;
3063 struct nfs_renameres *res = msg->rpc_resp;
3064
3065 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003066 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003067 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003068}
3069
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003070static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3071{
3072 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3073 &data->args.seq_args,
3074 &data->res.seq_res,
3075 task))
3076 return;
3077 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003078}
3079
3080static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3081 struct inode *new_dir)
3082{
3083 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3084
3085 if (!nfs4_sequence_done(task, &res->seq_res))
3086 return 0;
3087 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3088 return 0;
3089
3090 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003091 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003092 return 1;
3093}
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3096 struct inode *new_dir, struct qstr *new_name)
3097{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003098 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003099 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 .old_dir = NFS_FH(old_dir),
3101 .new_dir = NFS_FH(new_dir),
3102 .old_name = old_name,
3103 .new_name = new_name,
3104 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003105 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003106 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003107 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 struct rpc_message msg = {
3109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3110 .rpc_argp = &arg,
3111 .rpc_resp = &res,
3112 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003113 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Bryan Schumaker7c513052011-03-24 17:12:24 +00003115 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 if (!status) {
3117 update_changeattr(old_dir, &res.old_cinfo);
3118 update_changeattr(new_dir, &res.new_cinfo);
3119 }
3120 return status;
3121}
3122
3123static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3124 struct inode *new_dir, struct qstr *new_name)
3125{
3126 struct nfs4_exception exception = { };
3127 int err;
3128 do {
3129 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3130 _nfs4_proc_rename(old_dir, old_name,
3131 new_dir, new_name),
3132 &exception);
3133 } while (exception.retry);
3134 return err;
3135}
3136
3137static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3138{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003139 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 struct nfs4_link_arg arg = {
3141 .fh = NFS_FH(inode),
3142 .dir_fh = NFS_FH(dir),
3143 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003144 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003146 struct nfs4_link_res res = {
3147 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003148 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 struct rpc_message msg = {
3150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3151 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003152 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003154 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Trond Myklebust136f2622010-04-16 16:22:49 -04003156 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003157 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003158 goto out;
3159
Bryan Schumaker7c513052011-03-24 17:12:24 +00003160 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003161 if (!status) {
3162 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003163 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003164 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003165out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003166 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return status;
3168}
3169
3170static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3171{
3172 struct nfs4_exception exception = { };
3173 int err;
3174 do {
3175 err = nfs4_handle_exception(NFS_SERVER(inode),
3176 _nfs4_proc_link(inode, dir, name),
3177 &exception);
3178 } while (exception.retry);
3179 return err;
3180}
3181
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003182struct nfs4_createdata {
3183 struct rpc_message msg;
3184 struct nfs4_create_arg arg;
3185 struct nfs4_create_res res;
3186 struct nfs_fh fh;
3187 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003188};
3189
3190static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3191 struct qstr *name, struct iattr *sattr, u32 ftype)
3192{
3193 struct nfs4_createdata *data;
3194
3195 data = kzalloc(sizeof(*data), GFP_KERNEL);
3196 if (data != NULL) {
3197 struct nfs_server *server = NFS_SERVER(dir);
3198
3199 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3200 data->msg.rpc_argp = &data->arg;
3201 data->msg.rpc_resp = &data->res;
3202 data->arg.dir_fh = NFS_FH(dir);
3203 data->arg.server = server;
3204 data->arg.name = name;
3205 data->arg.attrs = sattr;
3206 data->arg.ftype = ftype;
3207 data->arg.bitmask = server->attr_bitmask;
3208 data->res.server = server;
3209 data->res.fh = &data->fh;
3210 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003211 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003212 }
3213 return data;
3214}
3215
3216static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3217{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003218 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003219 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003220 if (status == 0) {
3221 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003222 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3223 }
3224 return status;
3225}
3226
3227static void nfs4_free_createdata(struct nfs4_createdata *data)
3228{
3229 kfree(data);
3230}
3231
Chuck Lever4f390c12006-08-22 20:06:22 -04003232static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003233 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003235 struct nfs4_createdata *data;
3236 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Chuck Lever94a6d752006-08-22 20:06:23 -04003238 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003239 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003240
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003241 status = -ENOMEM;
3242 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3243 if (data == NULL)
3244 goto out;
3245
3246 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3247 data->arg.u.symlink.pages = &page;
3248 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003250 status = nfs4_do_create(dir, dentry, data);
3251
3252 nfs4_free_createdata(data);
3253out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return status;
3255}
3256
Chuck Lever4f390c12006-08-22 20:06:22 -04003257static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003258 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
3260 struct nfs4_exception exception = { };
3261 int err;
3262 do {
3263 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003264 _nfs4_proc_symlink(dir, dentry, page,
3265 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 &exception);
3267 } while (exception.retry);
3268 return err;
3269}
3270
3271static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3272 struct iattr *sattr)
3273{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003274 struct nfs4_createdata *data;
3275 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003277 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3278 if (data == NULL)
3279 goto out;
3280
3281 status = nfs4_do_create(dir, dentry, data);
3282
3283 nfs4_free_createdata(data);
3284out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return status;
3286}
3287
3288static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3289 struct iattr *sattr)
3290{
3291 struct nfs4_exception exception = { };
3292 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003293
3294 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 do {
3296 err = nfs4_handle_exception(NFS_SERVER(dir),
3297 _nfs4_proc_mkdir(dir, dentry, sattr),
3298 &exception);
3299 } while (exception.retry);
3300 return err;
3301}
3302
3303static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003304 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
3306 struct inode *dir = dentry->d_inode;
3307 struct nfs4_readdir_arg args = {
3308 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003309 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 .pgbase = 0,
3311 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003312 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003313 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 };
3315 struct nfs4_readdir_res res;
3316 struct rpc_message msg = {
3317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3318 .rpc_argp = &args,
3319 .rpc_resp = &res,
3320 .rpc_cred = cred,
3321 };
3322 int status;
3323
Harvey Harrison3110ff82008-05-02 13:42:44 -07003324 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003325 dentry->d_parent->d_name.name,
3326 dentry->d_name.name,
3327 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003328 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003330 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 -05003331 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003332 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003333 status += args.pgbase;
3334 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003335
3336 nfs_invalidate_atime(dir);
3337
Harvey Harrison3110ff82008-05-02 13:42:44 -07003338 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 return status;
3340}
3341
3342static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003343 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
3345 struct nfs4_exception exception = { };
3346 int err;
3347 do {
3348 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3349 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003350 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 &exception);
3352 } while (exception.retry);
3353 return err;
3354}
3355
3356static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3357 struct iattr *sattr, dev_t rdev)
3358{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003359 struct nfs4_createdata *data;
3360 int mode = sattr->ia_mode;
3361 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3364 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003365
3366 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3367 if (data == NULL)
3368 goto out;
3369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003371 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003373 data->arg.ftype = NF4BLK;
3374 data->arg.u.device.specdata1 = MAJOR(rdev);
3375 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 }
3377 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003378 data->arg.ftype = NF4CHR;
3379 data->arg.u.device.specdata1 = MAJOR(rdev);
3380 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003383 status = nfs4_do_create(dir, dentry, data);
3384
3385 nfs4_free_createdata(data);
3386out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return status;
3388}
3389
3390static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3391 struct iattr *sattr, dev_t rdev)
3392{
3393 struct nfs4_exception exception = { };
3394 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003395
3396 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 do {
3398 err = nfs4_handle_exception(NFS_SERVER(dir),
3399 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3400 &exception);
3401 } while (exception.retry);
3402 return err;
3403}
3404
3405static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3406 struct nfs_fsstat *fsstat)
3407{
3408 struct nfs4_statfs_arg args = {
3409 .fh = fhandle,
3410 .bitmask = server->attr_bitmask,
3411 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003412 struct nfs4_statfs_res res = {
3413 .fsstat = fsstat,
3414 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 struct rpc_message msg = {
3416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3417 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003418 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 };
3420
Trond Myklebust0e574af2005-10-27 22:12:38 -04003421 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003422 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
3425static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3426{
3427 struct nfs4_exception exception = { };
3428 int err;
3429 do {
3430 err = nfs4_handle_exception(server,
3431 _nfs4_proc_statfs(server, fhandle, fsstat),
3432 &exception);
3433 } while (exception.retry);
3434 return err;
3435}
3436
3437static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3438 struct nfs_fsinfo *fsinfo)
3439{
3440 struct nfs4_fsinfo_arg args = {
3441 .fh = fhandle,
3442 .bitmask = server->attr_bitmask,
3443 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003444 struct nfs4_fsinfo_res res = {
3445 .fsinfo = fsinfo,
3446 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 struct rpc_message msg = {
3448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3449 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003450 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 };
3452
Bryan Schumaker7c513052011-03-24 17:12:24 +00003453 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
3455
3456static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3457{
3458 struct nfs4_exception exception = { };
3459 int err;
3460
3461 do {
3462 err = nfs4_handle_exception(server,
3463 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3464 &exception);
3465 } while (exception.retry);
3466 return err;
3467}
3468
3469static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3470{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003471 int error;
3472
Trond Myklebust0e574af2005-10-27 22:12:38 -04003473 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003474 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003475 if (error == 0) {
3476 /* block layout checks this! */
3477 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003478 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003479 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003480
3481 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482}
3483
3484static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3485 struct nfs_pathconf *pathconf)
3486{
3487 struct nfs4_pathconf_arg args = {
3488 .fh = fhandle,
3489 .bitmask = server->attr_bitmask,
3490 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003491 struct nfs4_pathconf_res res = {
3492 .pathconf = pathconf,
3493 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 struct rpc_message msg = {
3495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3496 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003497 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 };
3499
3500 /* None of the pathconf attributes are mandatory to implement */
3501 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3502 memset(pathconf, 0, sizeof(*pathconf));
3503 return 0;
3504 }
3505
Trond Myklebust0e574af2005-10-27 22:12:38 -04003506 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003507 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508}
3509
3510static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3511 struct nfs_pathconf *pathconf)
3512{
3513 struct nfs4_exception exception = { };
3514 int err;
3515
3516 do {
3517 err = nfs4_handle_exception(server,
3518 _nfs4_proc_pathconf(server, fhandle, pathconf),
3519 &exception);
3520 } while (exception.retry);
3521 return err;
3522}
3523
Benny Halevyd20581a2011-05-22 19:52:03 +03003524void __nfs4_read_done_cb(struct nfs_read_data *data)
3525{
Fred Isamancd841602012-04-20 14:47:44 -04003526 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003527}
3528
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003529static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Fred Isamancd841602012-04-20 14:47:44 -04003531 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003533 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003534 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003535 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003537
Benny Halevyd20581a2011-05-22 19:52:03 +03003538 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003540 renew_lease(server, data->timestamp);
3541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
3543
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003544static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3545{
3546
3547 dprintk("--> %s\n", __func__);
3548
3549 if (!nfs4_sequence_done(task, &data->res.seq_res))
3550 return -EAGAIN;
3551
Benny Halevyd20581a2011-05-22 19:52:03 +03003552 return data->read_done_cb ? data->read_done_cb(task, data) :
3553 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003554}
3555
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003556static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003559 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003560 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003561 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003564static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3565{
Fred Isamancd841602012-04-20 14:47:44 -04003566 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003567 &data->args.seq_args,
3568 &data->res.seq_res,
3569 task))
3570 return;
3571 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
Fred Isamanb029bc92011-03-03 15:13:42 +00003574static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
Fred Isamancd841602012-04-20 14:47:44 -04003576 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003578 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003579 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003580 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003582 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003584 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003585 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587}
3588
Fred Isamanb029bc92011-03-03 15:13:42 +00003589static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3590{
3591 if (!nfs4_sequence_done(task, &data->res.seq_res))
3592 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003593 return data->write_done_cb ? data->write_done_cb(task, data) :
3594 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003595}
3596
Trond Myklebust5a37f852012-04-28 14:55:16 -04003597static
3598bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003599{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003600 const struct nfs_pgio_header *hdr = data->header;
3601
3602 /* Don't request attributes for pNFS or O_DIRECT writes */
3603 if (data->ds_clp != NULL || hdr->dreq != NULL)
3604 return false;
3605 /* Otherwise, request attributes if and only if we don't hold
3606 * a delegation
3607 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003608 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003609}
Fred Isamana69aef12011-03-03 15:13:47 +00003610
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003611static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
Fred Isamancd841602012-04-20 14:47:44 -04003613 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003614
Trond Myklebust5a37f852012-04-28 14:55:16 -04003615 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003616 data->args.bitmask = NULL;
3617 data->res.fattr = NULL;
3618 } else
3619 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003620
Fred Isamanb029bc92011-03-03 15:13:42 +00003621 if (!data->write_done_cb)
3622 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003623 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 data->timestamp = jiffies;
3625
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003626 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003627 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003630static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3631{
Fred Isamancd841602012-04-20 14:47:44 -04003632 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003633 &data->args.seq_args,
3634 &data->res.seq_res,
3635 task))
3636 return;
3637 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
Fred Isaman0b7c0152012-04-20 14:47:39 -04003640static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3641{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3643 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003644 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return;
3647 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003648}
3649
Fred Isaman0b7c0152012-04-20 14:47:39 -04003650static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
3652 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003653
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003654 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003655 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003656 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659}
3660
Fred Isaman0b7c0152012-04-20 14:47:39 -04003661static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003662{
3663 if (!nfs4_sequence_done(task, &data->res.seq_res))
3664 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003665 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003666}
3667
Fred Isaman0b7c0152012-04-20 14:47:39 -04003668static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003670 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003671
Fred Isaman0b7c0152012-04-20 14:47:39 -04003672 if (data->commit_done_cb == NULL)
3673 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003674 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003675 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003676 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677}
3678
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003679struct nfs4_renewdata {
3680 struct nfs_client *client;
3681 unsigned long timestamp;
3682};
3683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684/*
3685 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3686 * standalone procedure for queueing an asynchronous RENEW.
3687 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003688static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003689{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003690 struct nfs4_renewdata *data = calldata;
3691 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003692
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003693 if (atomic_read(&clp->cl_count) > 1)
3694 nfs4_schedule_state_renewal(clp);
3695 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003696 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003697}
3698
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003699static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003701 struct nfs4_renewdata *data = calldata;
3702 struct nfs_client *clp = data->client;
3703 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
3705 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003706 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003707 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3708 return;
3709 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003710 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003711 return;
3712 }
3713 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003715 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716}
3717
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003718static const struct rpc_call_ops nfs4_renew_ops = {
3719 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003720 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003721};
3722
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003723static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
3725 struct rpc_message msg = {
3726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3727 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003728 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003730 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003732 if (renew_flags == 0)
3733 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003734 if (!atomic_inc_not_zero(&clp->cl_count))
3735 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003736 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003737 if (data == NULL)
3738 return -ENOMEM;
3739 data->client = clp;
3740 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003742 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
3744
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003745static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
3747 struct rpc_message msg = {
3748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3749 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003750 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 };
3752 unsigned long now = jiffies;
3753 int status;
3754
3755 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3756 if (status < 0)
3757 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003758 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 return 0;
3760}
3761
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003762static inline int nfs4_server_supports_acls(struct nfs_server *server)
3763{
3764 return (server->caps & NFS_CAP_ACLS)
3765 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3766 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3767}
3768
Trond Myklebust21f498c2012-08-24 10:59:25 -04003769/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3770 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003771 * the stack.
3772 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003773#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003774
Neil Hormane9e3d722011-03-04 19:26:03 -05003775static int buf_to_pages_noslab(const void *buf, size_t buflen,
3776 struct page **pages, unsigned int *pgbase)
3777{
3778 struct page *newpage, **spages;
3779 int rc = 0;
3780 size_t len;
3781 spages = pages;
3782
3783 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003784 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003785 newpage = alloc_page(GFP_KERNEL);
3786
3787 if (newpage == NULL)
3788 goto unwind;
3789 memcpy(page_address(newpage), buf, len);
3790 buf += len;
3791 buflen -= len;
3792 *pages++ = newpage;
3793 rc++;
3794 } while (buflen != 0);
3795
3796 return rc;
3797
3798unwind:
3799 for(; rc > 0; rc--)
3800 __free_page(spages[rc-1]);
3801 return -ENOMEM;
3802}
3803
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003804struct nfs4_cached_acl {
3805 int cached;
3806 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003807 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003808};
3809
3810static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003811{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003812 struct nfs_inode *nfsi = NFS_I(inode);
3813
3814 spin_lock(&inode->i_lock);
3815 kfree(nfsi->nfs4_acl);
3816 nfsi->nfs4_acl = acl;
3817 spin_unlock(&inode->i_lock);
3818}
3819
3820static void nfs4_zap_acl_attr(struct inode *inode)
3821{
3822 nfs4_set_cached_acl(inode, NULL);
3823}
3824
3825static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3826{
3827 struct nfs_inode *nfsi = NFS_I(inode);
3828 struct nfs4_cached_acl *acl;
3829 int ret = -ENOENT;
3830
3831 spin_lock(&inode->i_lock);
3832 acl = nfsi->nfs4_acl;
3833 if (acl == NULL)
3834 goto out;
3835 if (buf == NULL) /* user is just asking for length */
3836 goto out_len;
3837 if (acl->cached == 0)
3838 goto out;
3839 ret = -ERANGE; /* see getxattr(2) man page */
3840 if (acl->len > buflen)
3841 goto out;
3842 memcpy(buf, acl->data, acl->len);
3843out_len:
3844 ret = acl->len;
3845out:
3846 spin_unlock(&inode->i_lock);
3847 return ret;
3848}
3849
Sachin Prabhu5794d212012-04-17 14:36:40 +01003850static 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 +00003851{
3852 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003853 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003854
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003855 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003856 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003857 if (acl == NULL)
3858 goto out;
3859 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003860 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003861 } else {
3862 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3863 if (acl == NULL)
3864 goto out;
3865 acl->cached = 0;
3866 }
3867 acl->len = acl_len;
3868out:
3869 nfs4_set_cached_acl(inode, acl);
3870}
3871
Andy Adamsonbf118a32011-12-07 11:55:27 -05003872/*
3873 * The getxattr API returns the required buffer length when called with a
3874 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3875 * the required buf. On a NULL buf, we send a page of data to the server
3876 * guessing that the ACL request can be serviced by a page. If so, we cache
3877 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3878 * the cache. If not so, we throw away the page, and cache the required
3879 * length. The next getxattr call will then produce another round trip to
3880 * the server, this time with the input buf of the required size.
3881 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003882static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003883{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003884 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003885 struct nfs_getaclargs args = {
3886 .fh = NFS_FH(inode),
3887 .acl_pages = pages,
3888 .acl_len = buflen,
3889 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003890 struct nfs_getaclres res = {
3891 .acl_len = buflen,
3892 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3895 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003896 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003897 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003898 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3899 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003900
Andy Adamsonbf118a32011-12-07 11:55:27 -05003901 /* As long as we're doing a round trip to the server anyway,
3902 * let's be prepared for a page of acl data. */
3903 if (npages == 0)
3904 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003905 if (npages > ARRAY_SIZE(pages))
3906 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003907
Andy Adamsonbf118a32011-12-07 11:55:27 -05003908 for (i = 0; i < npages; i++) {
3909 pages[i] = alloc_page(GFP_KERNEL);
3910 if (!pages[i])
3911 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003912 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003913
3914 /* for decoding across pages */
3915 res.acl_scratch = alloc_page(GFP_KERNEL);
3916 if (!res.acl_scratch)
3917 goto out_free;
3918
Andy Adamsonbf118a32011-12-07 11:55:27 -05003919 args.acl_len = npages * PAGE_SIZE;
3920 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003921
Peng Taode040be2012-01-10 22:42:47 +08003922 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003923 __func__, buf, buflen, npages, args.acl_len);
3924 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3925 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003926 if (ret)
3927 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003928
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003929 /* Handle the case where the passed-in buffer is too short */
3930 if (res.acl_flags & NFS4_ACL_TRUNC) {
3931 /* Did the user only issue a request for the acl length? */
3932 if (buf == NULL)
3933 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003934 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003935 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003936 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003937 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3938 if (buf)
3939 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3940out_ok:
3941 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003942out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003943 for (i = 0; i < npages; i++)
3944 if (pages[i])
3945 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003946 if (res.acl_scratch)
3947 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003948 return ret;
3949}
3950
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003951static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3952{
3953 struct nfs4_exception exception = { };
3954 ssize_t ret;
3955 do {
3956 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3957 if (ret >= 0)
3958 break;
3959 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3960 } while (exception.retry);
3961 return ret;
3962}
3963
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003964static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3965{
3966 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003967 int ret;
3968
3969 if (!nfs4_server_supports_acls(server))
3970 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003971 ret = nfs_revalidate_inode(server, inode);
3972 if (ret < 0)
3973 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003974 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3975 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003976 ret = nfs4_read_cached_acl(inode, buf, buflen);
3977 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003978 /* -ENOENT is returned if there is no ACL or if there is an ACL
3979 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003980 return ret;
3981 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003982}
3983
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003984static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003985{
3986 struct nfs_server *server = NFS_SERVER(inode);
3987 struct page *pages[NFS4ACL_MAXPAGES];
3988 struct nfs_setaclargs arg = {
3989 .fh = NFS_FH(inode),
3990 .acl_pages = pages,
3991 .acl_len = buflen,
3992 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003993 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003994 struct rpc_message msg = {
3995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3996 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003997 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003998 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003999 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004000 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004001
4002 if (!nfs4_server_supports_acls(server))
4003 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004004 if (npages > ARRAY_SIZE(pages))
4005 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004006 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4007 if (i < 0)
4008 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004009 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004010 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004011
4012 /*
4013 * Free each page after tx, so the only ref left is
4014 * held by the network stack
4015 */
4016 for (; i > 0; i--)
4017 put_page(pages[i-1]);
4018
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004019 /*
4020 * Acl update can result in inode attribute update.
4021 * so mark the attribute cache invalid.
4022 */
4023 spin_lock(&inode->i_lock);
4024 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4025 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004026 nfs_access_zap_cache(inode);
4027 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004028 return ret;
4029}
4030
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004031static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4032{
4033 struct nfs4_exception exception = { };
4034 int err;
4035 do {
4036 err = nfs4_handle_exception(NFS_SERVER(inode),
4037 __nfs4_proc_set_acl(inode, buf, buflen),
4038 &exception);
4039 } while (exception.retry);
4040 return err;
4041}
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004044nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004046 struct nfs_client *clp = server->nfs_client;
4047
4048 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 return 0;
4050 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004051 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004052 case -NFS4ERR_ADMIN_REVOKED:
4053 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004054 if (state == NULL)
4055 break;
4056 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004057 case -NFS4ERR_OPENMODE:
4058 if (state == NULL)
4059 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004060 nfs4_schedule_stateid_recovery(server, state);
4061 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004062 case -NFS4ERR_EXPIRED:
4063 if (state != NULL)
4064 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004066 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004067 nfs4_schedule_lease_recovery(clp);
4068 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004069#if defined(CONFIG_NFS_V4_1)
4070 case -NFS4ERR_BADSESSION:
4071 case -NFS4ERR_BADSLOT:
4072 case -NFS4ERR_BAD_HIGH_SLOT:
4073 case -NFS4ERR_DEADSESSION:
4074 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4075 case -NFS4ERR_SEQ_FALSE_RETRY:
4076 case -NFS4ERR_SEQ_MISORDERED:
4077 dprintk("%s ERROR %d, Reset session\n", __func__,
4078 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004079 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004080 task->tk_status = 0;
4081 return -EAGAIN;
4082#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004084 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004085 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004086 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4088 task->tk_status = 0;
4089 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004090 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 case -NFS4ERR_OLD_STATEID:
4092 task->tk_status = 0;
4093 return -EAGAIN;
4094 }
4095 task->tk_status = nfs4_map_errors(task->tk_status);
4096 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004097wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004098 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004099 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4100 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4101 task->tk_status = 0;
4102 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103}
4104
Chuck Leverf0920752012-05-21 22:45:41 -04004105static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4106 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004107{
4108 __be32 verf[2];
4109
Chuck Lever2c820d92012-05-21 22:45:33 -04004110 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4111 /* An impossible timestamp guarantees this value
4112 * will never match a generated boot time. */
4113 verf[0] = 0;
4114 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4115 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004116 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4117 verf[0] = (__be32)nn->boot_time.tv_sec;
4118 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004119 }
Chuck Levercd937102012-03-02 17:14:31 -05004120 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4121}
4122
Chuck Levere984a552012-09-14 17:24:21 -04004123static unsigned int
4124nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4125 char *buf, size_t len)
4126{
4127 unsigned int result;
4128
4129 rcu_read_lock();
4130 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4131 clp->cl_ipaddr,
4132 rpc_peeraddr2str(clp->cl_rpcclient,
4133 RPC_DISPLAY_ADDR),
4134 rpc_peeraddr2str(clp->cl_rpcclient,
4135 RPC_DISPLAY_PROTO));
4136 rcu_read_unlock();
4137 return result;
4138}
4139
4140static unsigned int
4141nfs4_init_uniform_client_string(const struct nfs_client *clp,
4142 char *buf, size_t len)
4143{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004144 char *nodename = clp->cl_rpcclient->cl_nodename;
4145
4146 if (nfs4_client_id_uniquifier[0] != '\0')
4147 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004148 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4149 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004150 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004151}
4152
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004153/**
4154 * nfs4_proc_setclientid - Negotiate client ID
4155 * @clp: state data structure
4156 * @program: RPC program for NFSv4 callback service
4157 * @port: IP port number for NFS4 callback service
4158 * @cred: RPC credential to use for this call
4159 * @res: where to place the result
4160 *
4161 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4162 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004163int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4164 unsigned short port, struct rpc_cred *cred,
4165 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
4167 nfs4_verifier sc_verifier;
4168 struct nfs4_setclientid setclientid = {
4169 .sc_verifier = &sc_verifier,
4170 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004171 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 };
4173 struct rpc_message msg = {
4174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4175 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004176 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004177 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004179 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Chuck Leverde734832012-07-11 16:30:50 -04004181 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004182 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004183 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4184 setclientid.sc_name_len =
4185 nfs4_init_uniform_client_string(clp,
4186 setclientid.sc_name,
4187 sizeof(setclientid.sc_name));
4188 else
4189 setclientid.sc_name_len =
4190 nfs4_init_nonuniform_client_string(clp,
4191 setclientid.sc_name,
4192 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004193 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004194 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004195 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004196 sizeof(setclientid.sc_netid),
4197 rpc_peeraddr2str(clp->cl_rpcclient,
4198 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004199 rcu_read_unlock();
4200 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004201 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 clp->cl_ipaddr, port >> 8, port & 255);
4203
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004204 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4205 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4206 setclientid.sc_name_len, setclientid.sc_name);
4207 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4208 dprintk("NFS reply setclientid: %d\n", status);
4209 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210}
4211
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004212/**
4213 * nfs4_proc_setclientid_confirm - Confirm client ID
4214 * @clp: state data structure
4215 * @res: result of a previous SETCLIENTID
4216 * @cred: RPC credential to use for this call
4217 *
4218 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4219 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004220int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004221 struct nfs4_setclientid_res *arg,
4222 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
4224 struct nfs_fsinfo fsinfo;
4225 struct rpc_message msg = {
4226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004227 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004229 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 };
4231 unsigned long now;
4232 int status;
4233
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004234 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4235 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4236 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004238 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 if (status == 0) {
4240 spin_lock(&clp->cl_lock);
4241 clp->cl_lease_time = fsinfo.lease_time * HZ;
4242 clp->cl_last_renewal = now;
4243 spin_unlock(&clp->cl_lock);
4244 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004245 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 return status;
4247}
4248
Trond Myklebustfe650402006-01-03 09:55:18 +01004249struct nfs4_delegreturndata {
4250 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004251 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004252 struct nfs_fh fh;
4253 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004254 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004255 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004256 int rpc_status;
4257};
4258
Trond Myklebustfe650402006-01-03 09:55:18 +01004259static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4260{
4261 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004262
Trond Myklebust14516c32010-07-31 14:29:06 -04004263 if (!nfs4_sequence_done(task, &data->res.seq_res))
4264 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004265
Ricardo Labiaga79708862009-12-07 09:23:21 -05004266 switch (task->tk_status) {
4267 case -NFS4ERR_STALE_STATEID:
4268 case -NFS4ERR_EXPIRED:
4269 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004270 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004271 break;
4272 default:
4273 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4274 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004275 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004276 return;
4277 }
4278 }
4279 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004280}
4281
4282static void nfs4_delegreturn_release(void *calldata)
4283{
Trond Myklebustfe650402006-01-03 09:55:18 +01004284 kfree(calldata);
4285}
4286
Andy Adamson938e1012009-04-01 09:22:28 -04004287#if defined(CONFIG_NFS_V4_1)
4288static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4289{
4290 struct nfs4_delegreturndata *d_data;
4291
4292 d_data = (struct nfs4_delegreturndata *)data;
4293
Trond Myklebust035168a2010-06-16 09:52:26 -04004294 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004295 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004296 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004297 return;
4298 rpc_call_start(task);
4299}
4300#endif /* CONFIG_NFS_V4_1 */
4301
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004302static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004303#if defined(CONFIG_NFS_V4_1)
4304 .rpc_call_prepare = nfs4_delegreturn_prepare,
4305#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004306 .rpc_call_done = nfs4_delegreturn_done,
4307 .rpc_release = nfs4_delegreturn_release,
4308};
4309
Trond Myklebuste6f81072008-01-24 18:14:34 -05004310static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004311{
4312 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004313 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004314 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004315 struct rpc_message msg = {
4316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4317 .rpc_cred = cred,
4318 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004319 struct rpc_task_setup task_setup_data = {
4320 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004321 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004322 .callback_ops = &nfs4_delegreturn_ops,
4323 .flags = RPC_TASK_ASYNC,
4324 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004325 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004326
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004327 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004328 if (data == NULL)
4329 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004330 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004331 data->args.fhandle = &data->fh;
4332 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004333 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004334 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004335 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004336 data->res.fattr = &data->fattr;
4337 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004338 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004339 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004340 data->rpc_status = 0;
4341
Trond Myklebustc970aa82007-07-14 15:39:59 -04004342 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004343 msg.rpc_argp = &data->args;
4344 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004345 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004346 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004347 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004348 if (!issync)
4349 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004350 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004351 if (status != 0)
4352 goto out;
4353 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004354 if (status == 0)
4355 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4356 else
4357 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004358out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004359 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004360 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361}
4362
Trond Myklebuste6f81072008-01-24 18:14:34 -05004363int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364{
4365 struct nfs_server *server = NFS_SERVER(inode);
4366 struct nfs4_exception exception = { };
4367 int err;
4368 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004369 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 switch (err) {
4371 case -NFS4ERR_STALE_STATEID:
4372 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 case 0:
4374 return 0;
4375 }
4376 err = nfs4_handle_exception(server, err, &exception);
4377 } while (exception.retry);
4378 return err;
4379}
4380
4381#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4382#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4383
4384/*
4385 * sleep, with exponential backoff, and retry the LOCK operation.
4386 */
4387static unsigned long
4388nfs4_set_lock_task_retry(unsigned long timeout)
4389{
Jeff Laytond3103102011-12-01 22:44:39 +01004390 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 timeout <<= 1;
4392 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4393 return NFS4_LOCK_MAXTIMEOUT;
4394 return timeout;
4395}
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4398{
4399 struct inode *inode = state->inode;
4400 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004401 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004402 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004404 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004406 struct nfs_lockt_res res = {
4407 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 };
4409 struct rpc_message msg = {
4410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4411 .rpc_argp = &arg,
4412 .rpc_resp = &res,
4413 .rpc_cred = state->owner->so_cred,
4414 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 struct nfs4_lock_state *lsp;
4416 int status;
4417
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004418 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004419 status = nfs4_set_lock_state(state, request);
4420 if (status != 0)
4421 goto out;
4422 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004423 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004424 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004425 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004426 switch (status) {
4427 case 0:
4428 request->fl_type = F_UNLCK;
4429 break;
4430 case -NFS4ERR_DENIED:
4431 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004433 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004434out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return status;
4436}
4437
4438static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4439{
4440 struct nfs4_exception exception = { };
4441 int err;
4442
4443 do {
4444 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4445 _nfs4_proc_getlk(state, cmd, request),
4446 &exception);
4447 } while (exception.retry);
4448 return err;
4449}
4450
4451static int do_vfs_lock(struct file *file, struct file_lock *fl)
4452{
4453 int res = 0;
4454 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4455 case FL_POSIX:
4456 res = posix_lock_file_wait(file, fl);
4457 break;
4458 case FL_FLOCK:
4459 res = flock_lock_file_wait(file, fl);
4460 break;
4461 default:
4462 BUG();
4463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return res;
4465}
4466
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004467struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004468 struct nfs_locku_args arg;
4469 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004470 struct nfs4_lock_state *lsp;
4471 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004472 struct file_lock fl;
4473 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004474 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004475};
4476
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004477static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4478 struct nfs_open_context *ctx,
4479 struct nfs4_lock_state *lsp,
4480 struct nfs_seqid *seqid)
4481{
4482 struct nfs4_unlockdata *p;
4483 struct inode *inode = lsp->ls_state->inode;
4484
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004485 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004486 if (p == NULL)
4487 return NULL;
4488 p->arg.fh = NFS_FH(inode);
4489 p->arg.fl = &p->fl;
4490 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004491 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004492 p->arg.stateid = &lsp->ls_stateid;
4493 p->lsp = lsp;
4494 atomic_inc(&lsp->ls_count);
4495 /* Ensure we don't close file until we're done freeing locks! */
4496 p->ctx = get_nfs_open_context(ctx);
4497 memcpy(&p->fl, fl, sizeof(p->fl));
4498 p->server = NFS_SERVER(inode);
4499 return p;
4500}
4501
Trond Myklebust06f814a2006-01-03 09:55:07 +01004502static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004503{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004504 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004505 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004506 nfs4_put_lock_state(calldata->lsp);
4507 put_nfs_open_context(calldata->ctx);
4508 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004509}
4510
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004511static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004512{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004513 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004514
Trond Myklebust14516c32010-07-31 14:29:06 -04004515 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4516 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004517 switch (task->tk_status) {
4518 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004519 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4520 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004521 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004522 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004523 case -NFS4ERR_BAD_STATEID:
4524 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004525 case -NFS4ERR_STALE_STATEID:
4526 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004527 break;
4528 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004529 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004530 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004531 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004532}
4533
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004534static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004535{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004536 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004538 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004539 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004540 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004541 /* Note: exit _without_ running nfs4_locku_done */
4542 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004543 return;
4544 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004545 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004546 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004547 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004548 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004549 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004550 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551}
4552
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004553static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004554 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004555 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004556 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004557};
4558
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004559static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4560 struct nfs_open_context *ctx,
4561 struct nfs4_lock_state *lsp,
4562 struct nfs_seqid *seqid)
4563{
4564 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004565 struct rpc_message msg = {
4566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4567 .rpc_cred = ctx->cred,
4568 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004569 struct rpc_task_setup task_setup_data = {
4570 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004571 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004572 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004573 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004574 .flags = RPC_TASK_ASYNC,
4575 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004576
Frank Filz137d6ac2007-07-09 15:32:29 -07004577 /* Ensure this is an unlock - when canceling a lock, the
4578 * canceled lock is passed in, and it won't be an unlock.
4579 */
4580 fl->fl_type = F_UNLCK;
4581
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004582 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4583 if (data == NULL) {
4584 nfs_free_seqid(seqid);
4585 return ERR_PTR(-ENOMEM);
4586 }
4587
Trond Myklebust9d12b212012-01-17 22:04:25 -05004588 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004589 msg.rpc_argp = &data->arg;
4590 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004591 task_setup_data.callback_data = data;
4592 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593}
4594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4596{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004597 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004598 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004599 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004600 struct rpc_task *task;
4601 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004602 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Trond Myklebust9b073572006-06-29 16:38:34 -04004604 status = nfs4_set_lock_state(state, request);
4605 /* Unlock _before_ we do the RPC call */
4606 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004607 down_read(&nfsi->rwsem);
4608 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4609 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004610 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004611 }
4612 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004613 if (status != 0)
4614 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004615 /* Is this a delegated lock? */
4616 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004617 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004618 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004619 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004620 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004621 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004622 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004623 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004624 status = PTR_ERR(task);
4625 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004626 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004627 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004628 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004629out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004630 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004631 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}
4633
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004634struct nfs4_lockdata {
4635 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004636 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004637 struct nfs4_lock_state *lsp;
4638 struct nfs_open_context *ctx;
4639 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004640 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004641 int rpc_status;
4642 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004643 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004644};
4645
4646static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004647 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4648 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004649{
4650 struct nfs4_lockdata *p;
4651 struct inode *inode = lsp->ls_state->inode;
4652 struct nfs_server *server = NFS_SERVER(inode);
4653
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004654 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004655 if (p == NULL)
4656 return NULL;
4657
4658 p->arg.fh = NFS_FH(inode);
4659 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004660 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004661 if (p->arg.open_seqid == NULL)
4662 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004663 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004664 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004665 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004666 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004667 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004668 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004669 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004670 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004671 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004672 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004673 atomic_inc(&lsp->ls_count);
4674 p->ctx = get_nfs_open_context(ctx);
4675 memcpy(&p->fl, fl, sizeof(p->fl));
4676 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004677out_free_seqid:
4678 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004679out_free:
4680 kfree(p);
4681 return NULL;
4682}
4683
4684static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4685{
4686 struct nfs4_lockdata *data = calldata;
4687 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Harvey Harrison3110ff82008-05-02 13:42:44 -07004689 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004690 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4691 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004692 /* Do we need to do an open_to_lock_owner? */
4693 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004694 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4695 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004696 data->arg.open_stateid = &state->stateid;
4697 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004698 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004699 } else
4700 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004701 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004702 if (nfs4_setup_sequence(data->server,
4703 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004704 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004705 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004706 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004707 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004708}
4709
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004710static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4711{
4712 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4713 nfs4_lock_prepare(task, calldata);
4714}
4715
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004716static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4717{
4718 struct nfs4_lockdata *data = calldata;
4719
Harvey Harrison3110ff82008-05-02 13:42:44 -07004720 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004721
Trond Myklebust14516c32010-07-31 14:29:06 -04004722 if (!nfs4_sequence_done(task, &data->res.seq_res))
4723 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004724
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004725 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004726 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004727 if (data->rpc_status == 0)
4728 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4729 else
4730 goto out;
4731 }
4732 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004733 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004734 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004735 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004736 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004737out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004738 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004739}
4740
4741static void nfs4_lock_release(void *calldata)
4742{
4743 struct nfs4_lockdata *data = calldata;
4744
Harvey Harrison3110ff82008-05-02 13:42:44 -07004745 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004746 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004747 if (data->cancelled != 0) {
4748 struct rpc_task *task;
4749 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4750 data->arg.lock_seqid);
4751 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004752 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004753 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004754 } else
4755 nfs_free_seqid(data->arg.lock_seqid);
4756 nfs4_put_lock_state(data->lsp);
4757 put_nfs_open_context(data->ctx);
4758 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004759 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004760}
4761
4762static const struct rpc_call_ops nfs4_lock_ops = {
4763 .rpc_call_prepare = nfs4_lock_prepare,
4764 .rpc_call_done = nfs4_lock_done,
4765 .rpc_release = nfs4_lock_release,
4766};
4767
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004768static const struct rpc_call_ops nfs4_recover_lock_ops = {
4769 .rpc_call_prepare = nfs4_recover_lock_prepare,
4770 .rpc_call_done = nfs4_lock_done,
4771 .rpc_release = nfs4_lock_release,
4772};
4773
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004774static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4775{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004776 switch (error) {
4777 case -NFS4ERR_ADMIN_REVOKED:
4778 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004779 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004780 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004781 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004782 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004783 break;
4784 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004785 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004786 case -NFS4ERR_EXPIRED:
4787 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004788 };
4789}
4790
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004791static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004792{
4793 struct nfs4_lockdata *data;
4794 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004795 struct rpc_message msg = {
4796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4797 .rpc_cred = state->owner->so_cred,
4798 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004799 struct rpc_task_setup task_setup_data = {
4800 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004801 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004802 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004803 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004804 .flags = RPC_TASK_ASYNC,
4805 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004806 int ret;
4807
Harvey Harrison3110ff82008-05-02 13:42:44 -07004808 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004809 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004810 fl->fl_u.nfs4_fl.owner,
4811 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004812 if (data == NULL)
4813 return -ENOMEM;
4814 if (IS_SETLKW(cmd))
4815 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004816 if (recovery_type > NFS_LOCK_NEW) {
4817 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004818 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004819 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4820 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004821 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004822 msg.rpc_argp = &data->arg;
4823 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004824 task_setup_data.callback_data = data;
4825 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004826 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004827 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004828 ret = nfs4_wait_for_completion_rpc_task(task);
4829 if (ret == 0) {
4830 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004831 if (ret)
4832 nfs4_handle_setlk_error(data->server, data->lsp,
4833 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004834 } else
4835 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004836 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004837 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004838 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839}
4840
4841static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4842{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004843 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004844 struct nfs4_exception exception = {
4845 .inode = state->inode,
4846 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004847 int err;
4848
4849 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004850 /* Cache the lock if possible... */
4851 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4852 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004853 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004854 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004855 break;
4856 nfs4_handle_exception(server, err, &exception);
4857 } while (exception.retry);
4858 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859}
4860
4861static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4862{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004863 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004864 struct nfs4_exception exception = {
4865 .inode = state->inode,
4866 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004867 int err;
4868
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004869 err = nfs4_set_lock_state(state, request);
4870 if (err != 0)
4871 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004872 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004873 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4874 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004875 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004876 switch (err) {
4877 default:
4878 goto out;
4879 case -NFS4ERR_GRACE:
4880 case -NFS4ERR_DELAY:
4881 nfs4_handle_exception(server, err, &exception);
4882 err = 0;
4883 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004884 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004885out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004886 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887}
4888
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004889#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004890/**
4891 * nfs41_check_expired_locks - possibly free a lock stateid
4892 *
4893 * @state: NFSv4 state for an inode
4894 *
4895 * Returns NFS_OK if recovery for this stateid is now finished.
4896 * Otherwise a negative NFS4ERR value is returned.
4897 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004898static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004899{
Chuck Levereb64cf92012-07-11 16:30:05 -04004900 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004901 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004902 struct nfs_server *server = NFS_SERVER(state->inode);
4903
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004904 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004905 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004906 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4907 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004908 /* Free the stateid unless the server
4909 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004910 if (status != -NFS4ERR_BAD_STATEID)
4911 nfs41_free_stateid(server,
4912 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004913 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004914 ret = status;
4915 }
4916 }
4917 };
4918
4919 return ret;
4920}
4921
4922static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4923{
4924 int status = NFS_OK;
4925
4926 if (test_bit(LK_STATE_IN_USE, &state->flags))
4927 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004928 if (status != NFS_OK)
4929 status = nfs4_lock_expired(state, request);
4930 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004931}
4932#endif
4933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4935{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004936 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004937 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004938 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004940 if ((fl_flags & FL_POSIX) &&
4941 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4942 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004943 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004944 status = nfs4_set_lock_state(state, request);
4945 if (status != 0)
4946 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004947 request->fl_flags |= FL_ACCESS;
4948 status = do_vfs_lock(request->fl_file, request);
4949 if (status < 0)
4950 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004951 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004952 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004953 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004954 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004955 request->fl_flags = fl_flags & ~FL_SLEEP;
4956 status = do_vfs_lock(request->fl_file, request);
4957 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004958 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004959 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004960 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004961 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004962 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004963 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004964 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004965 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4966 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004967out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004968 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004969out:
4970 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 return status;
4972}
4973
4974static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4975{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004976 struct nfs4_exception exception = {
4977 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004978 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004979 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 int err;
4981
4982 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004983 err = _nfs4_proc_setlk(state, cmd, request);
4984 if (err == -NFS4ERR_DENIED)
4985 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004987 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 } while (exception.retry);
4989 return err;
4990}
4991
4992static int
4993nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4994{
4995 struct nfs_open_context *ctx;
4996 struct nfs4_state *state;
4997 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4998 int status;
4999
5000 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005001 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 state = ctx->state;
5003
5004 if (request->fl_start < 0 || request->fl_end < 0)
5005 return -EINVAL;
5006
Trond Myklebustd9531262009-07-21 19:22:38 -04005007 if (IS_GETLK(cmd)) {
5008 if (state != NULL)
5009 return nfs4_proc_getlk(state, F_GETLK, request);
5010 return 0;
5011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
5013 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5014 return -EINVAL;
5015
Trond Myklebustd9531262009-07-21 19:22:38 -04005016 if (request->fl_type == F_UNLCK) {
5017 if (state != NULL)
5018 return nfs4_proc_unlck(state, cmd, request);
5019 return 0;
5020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Trond Myklebustd9531262009-07-21 19:22:38 -04005022 if (state == NULL)
5023 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005024 /*
5025 * Don't rely on the VFS having checked the file open mode,
5026 * since it won't do this for flock() locks.
5027 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005028 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005029 case F_RDLCK:
5030 if (!(filp->f_mode & FMODE_READ))
5031 return -EBADF;
5032 break;
5033 case F_WRLCK:
5034 if (!(filp->f_mode & FMODE_WRITE))
5035 return -EBADF;
5036 }
5037
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 do {
5039 status = nfs4_proc_setlk(state, cmd, request);
5040 if ((status != -EAGAIN) || IS_SETLK(cmd))
5041 break;
5042 timeout = nfs4_set_lock_task_retry(timeout);
5043 status = -ERESTARTSYS;
5044 if (signalled())
5045 break;
5046 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 return status;
5048}
5049
Trond Myklebust888e6942005-11-04 15:38:11 -05005050int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5051{
5052 struct nfs_server *server = NFS_SERVER(state->inode);
5053 struct nfs4_exception exception = { };
5054 int err;
5055
5056 err = nfs4_set_lock_state(state, fl);
5057 if (err != 0)
5058 goto out;
5059 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005060 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005061 switch (err) {
5062 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005063 printk(KERN_ERR "NFS: %s: unhandled error "
5064 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005065 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005066 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005067 goto out;
5068 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005069 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005070 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005071 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005072 nfs4_schedule_lease_recovery(server->nfs_client);
5073 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005074 case -NFS4ERR_BADSESSION:
5075 case -NFS4ERR_BADSLOT:
5076 case -NFS4ERR_BAD_HIGH_SLOT:
5077 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5078 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005079 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005080 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005081 case -ERESTARTSYS:
5082 /*
5083 * The show must go on: exit, but mark the
5084 * stateid as needing recovery.
5085 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005086 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005087 case -NFS4ERR_ADMIN_REVOKED:
5088 case -NFS4ERR_BAD_STATEID:
5089 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005090 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005091 err = 0;
5092 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005093 case -EKEYEXPIRED:
5094 /*
5095 * User RPCSEC_GSS context has expired.
5096 * We cannot recover this stateid now, so
5097 * skip it and allow recovery thread to
5098 * proceed.
5099 */
5100 err = 0;
5101 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005102 case -ENOMEM:
5103 case -NFS4ERR_DENIED:
5104 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5105 err = 0;
5106 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005107 case -NFS4ERR_DELAY:
5108 break;
5109 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005110 err = nfs4_handle_exception(server, err, &exception);
5111 } while (exception.retry);
5112out:
5113 return err;
5114}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005115
Trond Myklebustcf470c32012-03-07 13:49:12 -05005116struct nfs_release_lockowner_data {
5117 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005118 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005119 struct nfs_release_lockowner_args args;
5120};
5121
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005122static void nfs4_release_lockowner_release(void *calldata)
5123{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005124 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005125 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005126 kfree(calldata);
5127}
5128
Trond Myklebust17280172012-03-11 13:11:00 -04005129static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005130 .rpc_release = nfs4_release_lockowner_release,
5131};
5132
Trond Myklebustcf470c32012-03-07 13:49:12 -05005133int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005134{
5135 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005136 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005137 struct rpc_message msg = {
5138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5139 };
5140
5141 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005142 return -EINVAL;
5143 data = kmalloc(sizeof(*data), GFP_NOFS);
5144 if (!data)
5145 return -ENOMEM;
5146 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005147 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005148 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5149 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5150 data->args.lock_owner.s_dev = server->s_dev;
5151 msg.rpc_argp = &data->args;
5152 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5153 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005154}
5155
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005156#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5157
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005158static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5159 const void *buf, size_t buflen,
5160 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005161{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005162 if (strcmp(key, "") != 0)
5163 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005164
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005165 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005166}
5167
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005168static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5169 void *buf, size_t buflen, 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 Fieldsaa1870a2005-06-22 17:16:22 +00005173
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005174 return nfs4_proc_get_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 size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5178 size_t list_len, const char *name,
5179 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005180{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005181 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005182
J. Bruce Fields096455a2006-03-20 23:23:42 -05005183 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5184 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005185
5186 if (list && len <= list_len)
5187 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005188 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005189}
5190
Andy Adamson533eb462011-06-13 18:25:56 -04005191/*
5192 * nfs_fhget will use either the mounted_on_fileid or the fileid
5193 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005194static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5195{
Andy Adamson533eb462011-06-13 18:25:56 -04005196 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5197 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5198 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005199 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005200 return;
5201
5202 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005203 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005204 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5205 fattr->nlink = 2;
5206}
5207
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005208static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5209 const struct qstr *name,
5210 struct nfs4_fs_locations *fs_locations,
5211 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005212{
5213 struct nfs_server *server = NFS_SERVER(dir);
5214 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005215 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005216 };
5217 struct nfs4_fs_locations_arg args = {
5218 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005219 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005220 .page = page,
5221 .bitmask = bitmask,
5222 };
Benny Halevy22958462009-04-01 09:22:02 -04005223 struct nfs4_fs_locations_res res = {
5224 .fs_locations = fs_locations,
5225 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005226 struct rpc_message msg = {
5227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5228 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005229 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005230 };
5231 int status;
5232
Harvey Harrison3110ff82008-05-02 13:42:44 -07005233 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005234
5235 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5236 * is not supported */
5237 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5238 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5239 else
5240 bitmask[0] |= FATTR4_WORD0_FILEID;
5241
Trond Myklebustc228fd32007-01-13 02:28:11 -05005242 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005243 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005244 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005245 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005246 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005247 return status;
5248}
5249
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005250int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5251 const struct qstr *name,
5252 struct nfs4_fs_locations *fs_locations,
5253 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005254{
5255 struct nfs4_exception exception = { };
5256 int err;
5257 do {
5258 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005259 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005260 &exception);
5261 } while (exception.retry);
5262 return err;
5263}
5264
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005265static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5266{
5267 int status;
5268 struct nfs4_secinfo_arg args = {
5269 .dir_fh = NFS_FH(dir),
5270 .name = name,
5271 };
5272 struct nfs4_secinfo_res res = {
5273 .flavors = flavors,
5274 };
5275 struct rpc_message msg = {
5276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5277 .rpc_argp = &args,
5278 .rpc_resp = &res,
5279 };
5280
5281 dprintk("NFS call secinfo %s\n", name->name);
5282 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5283 dprintk("NFS reply secinfo: %d\n", status);
5284 return status;
5285}
5286
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005287int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5288 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005289{
5290 struct nfs4_exception exception = { };
5291 int err;
5292 do {
5293 err = nfs4_handle_exception(NFS_SERVER(dir),
5294 _nfs4_proc_secinfo(dir, name, flavors),
5295 &exception);
5296 } while (exception.retry);
5297 return err;
5298}
5299
Andy Adamson557134a2009-04-01 09:21:53 -04005300#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005301/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005302 * Check the exchange flags returned by the server for invalid flags, having
5303 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5304 * DS flags set.
5305 */
5306static int nfs4_check_cl_exchange_flags(u32 flags)
5307{
5308 if (flags & ~EXCHGID4_FLAG_MASK_R)
5309 goto out_inval;
5310 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5311 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5312 goto out_inval;
5313 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5314 goto out_inval;
5315 return NFS_OK;
5316out_inval:
5317 return -NFS4ERR_INVAL;
5318}
5319
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005320static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005321nfs41_same_server_scope(struct nfs41_server_scope *a,
5322 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005323{
5324 if (a->server_scope_sz == b->server_scope_sz &&
5325 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5326 return true;
5327
5328 return false;
5329}
5330
Andy Adamson357f54d2010-12-14 10:11:57 -05005331/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005332 * nfs4_proc_bind_conn_to_session()
5333 *
5334 * The 4.1 client currently uses the same TCP connection for the
5335 * fore and backchannel.
5336 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005337int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005338{
5339 int status;
5340 struct nfs41_bind_conn_to_session_res res;
5341 struct rpc_message msg = {
5342 .rpc_proc =
5343 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5344 .rpc_argp = clp,
5345 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005346 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005347 };
5348
5349 dprintk("--> %s\n", __func__);
5350 BUG_ON(clp == NULL);
5351
5352 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5353 if (unlikely(res.session == NULL)) {
5354 status = -ENOMEM;
5355 goto out;
5356 }
5357
5358 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5359 if (status == 0) {
5360 if (memcmp(res.session->sess_id.data,
5361 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5362 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5363 status = -EIO;
5364 goto out_session;
5365 }
5366 if (res.dir != NFS4_CDFS4_BOTH) {
5367 dprintk("NFS: %s: Unexpected direction from server\n",
5368 __func__);
5369 status = -EIO;
5370 goto out_session;
5371 }
5372 if (res.use_conn_in_rdma_mode) {
5373 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5374 __func__);
5375 status = -EIO;
5376 goto out_session;
5377 }
5378 }
5379out_session:
5380 kfree(res.session);
5381out:
5382 dprintk("<-- %s status= %d\n", __func__, status);
5383 return status;
5384}
5385
5386/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005387 * nfs4_proc_exchange_id()
5388 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005389 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5390 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005391 * Since the clientid has expired, all compounds using sessions
5392 * associated with the stale clientid will be returning
5393 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5394 * be in some phase of session reset.
5395 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005396int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005397{
5398 nfs4_verifier verifier;
5399 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005400 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005401 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005402 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005403 };
5404 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005405 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005406 };
5407 int status;
5408 struct rpc_message msg = {
5409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5410 .rpc_argp = &args,
5411 .rpc_resp = &res,
5412 .rpc_cred = cred,
5413 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005414
Chuck Leverf0920752012-05-21 22:45:41 -04005415 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005416 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5417 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005418 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5419 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5420 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005421
Chuck Leveracdeb692012-05-21 22:46:16 -04005422 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005423 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005424 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005425 status = -ENOMEM;
5426 goto out;
5427 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005428
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005429 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005430 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005431 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005432 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005433 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005434 }
5435
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005436 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005437 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005438 status = -ENOMEM;
5439 goto out_server_scope;
5440 }
5441
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005442 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005443 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005444 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005445
Chuck Lever177313f2012-05-21 22:44:58 -04005446 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005447 clp->cl_clientid = res.clientid;
5448 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5449 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5450 clp->cl_seqid = res.seqid;
5451
Chuck Leveracdeb692012-05-21 22:46:16 -04005452 kfree(clp->cl_serverowner);
5453 clp->cl_serverowner = res.server_owner;
5454 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005455
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005456 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005457 kfree(clp->cl_implid);
5458 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005459
Chuck Lever177313f2012-05-21 22:44:58 -04005460 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005461 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005462 res.server_scope)) {
5463 dprintk("%s: server_scope mismatch detected\n",
5464 __func__);
5465 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005466 kfree(clp->cl_serverscope);
5467 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005468 }
5469
Chuck Lever177313f2012-05-21 22:44:58 -04005470 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005471 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005472 goto out;
5473 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005474 } else
5475 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005476
Chuck Leveracdeb692012-05-21 22:46:16 -04005477out_server_owner:
5478 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005479out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005480 kfree(res.server_scope);
5481out:
Chuck Lever177313f2012-05-21 22:44:58 -04005482 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005483 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005484 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005485 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005486 clp->cl_implid->date.seconds,
5487 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005488 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005489 return status;
5490}
5491
Trond Myklebust66245532012-05-25 17:18:09 -04005492static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5493 struct rpc_cred *cred)
5494{
5495 struct rpc_message msg = {
5496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5497 .rpc_argp = clp,
5498 .rpc_cred = cred,
5499 };
5500 int status;
5501
5502 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5503 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005504 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005505 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5506 return status;
5507}
5508
5509static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5510 struct rpc_cred *cred)
5511{
5512 unsigned int loop;
5513 int ret;
5514
5515 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5516 ret = _nfs4_proc_destroy_clientid(clp, cred);
5517 switch (ret) {
5518 case -NFS4ERR_DELAY:
5519 case -NFS4ERR_CLIENTID_BUSY:
5520 ssleep(1);
5521 break;
5522 default:
5523 return ret;
5524 }
5525 }
5526 return 0;
5527}
5528
5529int nfs4_destroy_clientid(struct nfs_client *clp)
5530{
5531 struct rpc_cred *cred;
5532 int ret = 0;
5533
5534 if (clp->cl_mvops->minor_version < 1)
5535 goto out;
5536 if (clp->cl_exchange_flags == 0)
5537 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005538 if (clp->cl_preserve_clid)
5539 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005540 cred = nfs4_get_exchange_id_cred(clp);
5541 ret = nfs4_proc_destroy_clientid(clp, cred);
5542 if (cred)
5543 put_rpccred(cred);
5544 switch (ret) {
5545 case 0:
5546 case -NFS4ERR_STALE_CLIENTID:
5547 clp->cl_exchange_flags = 0;
5548 }
5549out:
5550 return ret;
5551}
5552
Andy Adamson2050f0c2009-04-01 09:22:30 -04005553struct nfs4_get_lease_time_data {
5554 struct nfs4_get_lease_time_args *args;
5555 struct nfs4_get_lease_time_res *res;
5556 struct nfs_client *clp;
5557};
5558
5559static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5560 void *calldata)
5561{
5562 int ret;
5563 struct nfs4_get_lease_time_data *data =
5564 (struct nfs4_get_lease_time_data *)calldata;
5565
5566 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005567 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005568 /* just setup sequence, do not trigger session recovery
5569 since we're invoked within one */
5570 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005571 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005572 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005573
5574 BUG_ON(ret == -EAGAIN);
5575 rpc_call_start(task);
5576 dprintk("<-- %s\n", __func__);
5577}
5578
5579/*
5580 * Called from nfs4_state_manager thread for session setup, so don't recover
5581 * from sequence operation or clientid errors.
5582 */
5583static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5584{
5585 struct nfs4_get_lease_time_data *data =
5586 (struct nfs4_get_lease_time_data *)calldata;
5587
5588 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005589 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5590 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005591 switch (task->tk_status) {
5592 case -NFS4ERR_DELAY:
5593 case -NFS4ERR_GRACE:
5594 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5595 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5596 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005597 /* fall through */
5598 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005599 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005600 return;
5601 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005602 dprintk("<-- %s\n", __func__);
5603}
5604
Trond Myklebust17280172012-03-11 13:11:00 -04005605static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005606 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5607 .rpc_call_done = nfs4_get_lease_time_done,
5608};
5609
5610int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5611{
5612 struct rpc_task *task;
5613 struct nfs4_get_lease_time_args args;
5614 struct nfs4_get_lease_time_res res = {
5615 .lr_fsinfo = fsinfo,
5616 };
5617 struct nfs4_get_lease_time_data data = {
5618 .args = &args,
5619 .res = &res,
5620 .clp = clp,
5621 };
5622 struct rpc_message msg = {
5623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5624 .rpc_argp = &args,
5625 .rpc_resp = &res,
5626 };
5627 struct rpc_task_setup task_setup = {
5628 .rpc_client = clp->cl_rpcclient,
5629 .rpc_message = &msg,
5630 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005631 .callback_data = &data,
5632 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005633 };
5634 int status;
5635
Trond Myklebust9d12b212012-01-17 22:04:25 -05005636 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005637 dprintk("--> %s\n", __func__);
5638 task = rpc_run_task(&task_setup);
5639
5640 if (IS_ERR(task))
5641 status = PTR_ERR(task);
5642 else {
5643 status = task->tk_status;
5644 rpc_put_task(task);
5645 }
5646 dprintk("<-- %s return %d\n", __func__, status);
5647
5648 return status;
5649}
5650
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005651static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5652{
5653 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5654}
5655
5656static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5657 struct nfs4_slot *new,
5658 u32 max_slots,
5659 u32 ivalue)
5660{
5661 struct nfs4_slot *old = NULL;
5662 u32 i;
5663
5664 spin_lock(&tbl->slot_tbl_lock);
5665 if (new) {
5666 old = tbl->slots;
5667 tbl->slots = new;
5668 tbl->max_slots = max_slots;
5669 }
5670 tbl->highest_used_slotid = -1; /* no slot is currently used */
5671 for (i = 0; i < tbl->max_slots; i++)
5672 tbl->slots[i].seq_nr = ivalue;
5673 spin_unlock(&tbl->slot_tbl_lock);
5674 kfree(old);
5675}
5676
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005677/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005678 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005679 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005680static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5681 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005682{
Andy Adamson104aeba2010-01-14 17:45:10 -05005683 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005684 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005685
Andy Adamson104aeba2010-01-14 17:45:10 -05005686 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5687 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005688
Andy Adamson104aeba2010-01-14 17:45:10 -05005689 /* Does the newly negotiated max_reqs match the existing slot table? */
5690 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005691 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005692 if (!new)
5693 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005694 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005695 ret = 0;
5696
5697 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005698 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5699 tbl, tbl->slots, tbl->max_slots);
5700out:
5701 dprintk("<-- %s: return %d\n", __func__, ret);
5702 return ret;
5703}
5704
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005705/* Destroy the slot table */
5706static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5707{
5708 if (session->fc_slot_table.slots != NULL) {
5709 kfree(session->fc_slot_table.slots);
5710 session->fc_slot_table.slots = NULL;
5711 }
5712 if (session->bc_slot_table.slots != NULL) {
5713 kfree(session->bc_slot_table.slots);
5714 session->bc_slot_table.slots = NULL;
5715 }
5716 return;
5717}
5718
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005719/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005720 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005721 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005722static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005723{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005724 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005725 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005726
Andy Adamsonaacd5532011-11-09 13:58:21 -05005727 dprintk("--> %s\n", __func__);
5728 /* Fore channel */
5729 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005730 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5731 if (status) /* -ENOMEM */
5732 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005733 /* Back channel */
5734 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005735 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5736 if (status && tbl->slots == NULL)
5737 /* Fore and back channel share a connection so get
5738 * both slot tables or neither */
5739 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005740 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005741}
5742
5743struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5744{
5745 struct nfs4_session *session;
5746 struct nfs4_slot_table *tbl;
5747
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005748 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005749 if (!session)
5750 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005751
Andy Adamson557134a2009-04-01 09:21:53 -04005752 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005753 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005754 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005755 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005756 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005757
5758 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005759 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005760 spin_lock_init(&tbl->slot_tbl_lock);
5761 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005762 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005763
Trond Myklebust1055d762010-06-16 09:52:27 -04005764 session->session_state = 1<<NFS4_SESSION_INITING;
5765
Andy Adamson557134a2009-04-01 09:21:53 -04005766 session->clp = clp;
5767 return session;
5768}
5769
5770void nfs4_destroy_session(struct nfs4_session *session)
5771{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005772 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005773 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005774
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005775 cred = nfs4_get_exchange_id_cred(session->clp);
5776 nfs4_proc_destroy_session(session, cred);
5777 if (cred)
5778 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005779
5780 rcu_read_lock();
5781 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5782 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005783 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005784 __func__, xprt);
5785 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005786 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005787 kfree(session);
5788}
5789
Andy Adamsonfc931582009-04-01 09:22:31 -04005790/*
5791 * Initialize the values to be used by the client in CREATE_SESSION
5792 * If nfs4_init_session set the fore channel request and response sizes,
5793 * use them.
5794 *
5795 * Set the back channel max_resp_sz_cached to zero to force the client to
5796 * always set csa_cachethis to FALSE because the current implementation
5797 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5798 */
5799static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5800{
5801 struct nfs4_session *session = args->client->cl_session;
5802 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5803 mxresp_sz = session->fc_attrs.max_resp_sz;
5804
5805 if (mxrqst_sz == 0)
5806 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5807 if (mxresp_sz == 0)
5808 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5809 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005810 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5811 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005812 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005813 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005814
5815 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005816 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005817 __func__,
5818 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005819 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005820
5821 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005822 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5823 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5824 args->bc_attrs.max_resp_sz_cached = 0;
5825 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5826 args->bc_attrs.max_reqs = 1;
5827
5828 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5829 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5830 __func__,
5831 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5832 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5833 args->bc_attrs.max_reqs);
5834}
5835
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005836static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005837{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005838 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5839 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5840
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005841 if (rcvd->max_resp_sz > sent->max_resp_sz)
5842 return -EINVAL;
5843 /*
5844 * Our requested max_ops is the minimum we need; we're not
5845 * prepared to break up compounds into smaller pieces than that.
5846 * So, no point even trying to continue if the server won't
5847 * cooperate:
5848 */
5849 if (rcvd->max_ops < sent->max_ops)
5850 return -EINVAL;
5851 if (rcvd->max_reqs == 0)
5852 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005853 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5854 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005855 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005856}
5857
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005858static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5859{
5860 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5861 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005862
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005863 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5864 return -EINVAL;
5865 if (rcvd->max_resp_sz < sent->max_resp_sz)
5866 return -EINVAL;
5867 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5868 return -EINVAL;
5869 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005870 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005871 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005872 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005873 return -EINVAL;
5874 return 0;
5875}
Andy Adamson8d353012009-04-01 09:22:32 -04005876
Andy Adamson8d353012009-04-01 09:22:32 -04005877static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5878 struct nfs4_session *session)
5879{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005880 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005881
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005882 ret = nfs4_verify_fore_channel_attrs(args, session);
5883 if (ret)
5884 return ret;
5885 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005886}
5887
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005888static int _nfs4_proc_create_session(struct nfs_client *clp,
5889 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005890{
5891 struct nfs4_session *session = clp->cl_session;
5892 struct nfs41_create_session_args args = {
5893 .client = clp,
5894 .cb_program = NFS4_CALLBACK,
5895 };
5896 struct nfs41_create_session_res res = {
5897 .client = clp,
5898 };
5899 struct rpc_message msg = {
5900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5901 .rpc_argp = &args,
5902 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005903 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005904 };
5905 int status;
5906
5907 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005908 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005909
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005910 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005911
Andy Adamson8d353012009-04-01 09:22:32 -04005912 if (!status)
5913 /* Verify the session's negotiated channel_attrs values */
5914 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005915 if (!status) {
5916 /* Increment the clientid slot sequence id */
5917 clp->cl_seqid++;
5918 }
5919
5920 return status;
5921}
5922
5923/*
5924 * Issues a CREATE_SESSION operation to the server.
5925 * It is the responsibility of the caller to verify the session is
5926 * expired before calling this routine.
5927 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005928int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005929{
5930 int status;
5931 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005932 struct nfs4_session *session = clp->cl_session;
5933
5934 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5935
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005936 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005937 if (status)
5938 goto out;
5939
Andy Adamsonaacd5532011-11-09 13:58:21 -05005940 /* Init or reset the session slot tables */
5941 status = nfs4_setup_session_slot_tables(session);
5942 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005943 if (status)
5944 goto out;
5945
5946 ptr = (unsigned *)&session->sess_id.data[0];
5947 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5948 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005949out:
5950 dprintk("<-- %s\n", __func__);
5951 return status;
5952}
5953
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005954/*
5955 * Issue the over-the-wire RPC DESTROY_SESSION.
5956 * The caller must serialize access to this routine.
5957 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005958int nfs4_proc_destroy_session(struct nfs4_session *session,
5959 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005960{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005961 struct rpc_message msg = {
5962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5963 .rpc_argp = session,
5964 .rpc_cred = cred,
5965 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005966 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005967
5968 dprintk("--> nfs4_proc_destroy_session\n");
5969
5970 /* session is still being setup */
5971 if (session->clp->cl_cons_state != NFS_CS_READY)
5972 return status;
5973
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005974 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005975
5976 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005977 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005978 "Session has been destroyed regardless...\n", status);
5979
5980 dprintk("<-- nfs4_proc_destroy_session\n");
5981 return status;
5982}
5983
Trond Myklebust7b38c362012-05-23 13:23:31 -04005984/*
5985 * With sessions, the client is not marked ready until after a
5986 * successful EXCHANGE_ID and CREATE_SESSION.
5987 *
5988 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5989 * other versions of NFS can be tried.
5990 */
5991static int nfs41_check_session_ready(struct nfs_client *clp)
5992{
5993 int ret;
5994
5995 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5996 ret = nfs4_client_recover_expired_lease(clp);
5997 if (ret)
5998 return ret;
5999 }
6000 if (clp->cl_cons_state < NFS_CS_READY)
6001 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006002 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006003 return 0;
6004}
6005
Trond Myklebustfccba802009-07-21 16:48:07 -04006006int nfs4_init_session(struct nfs_server *server)
6007{
6008 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006009 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05006010 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04006011
6012 if (!nfs4_has_session(clp))
6013 return 0;
6014
Trond Myklebust1055d762010-06-16 09:52:27 -04006015 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006016 spin_lock(&clp->cl_lock);
6017 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04006018
Trond Myklebust7b38c362012-05-23 13:23:31 -04006019 rsize = server->rsize;
6020 if (rsize == 0)
6021 rsize = NFS_MAX_FILE_IO_SIZE;
6022 wsize = server->wsize;
6023 if (wsize == 0)
6024 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05006025
Trond Myklebust7b38c362012-05-23 13:23:31 -04006026 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
6027 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
6028 }
6029 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006030
Trond Myklebust7b38c362012-05-23 13:23:31 -04006031 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006032}
6033
Trond Myklebust7b38c362012-05-23 13:23:31 -04006034int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006035{
6036 struct nfs4_session *session = clp->cl_session;
6037 int ret;
6038
Trond Myklebust7b38c362012-05-23 13:23:31 -04006039 spin_lock(&clp->cl_lock);
6040 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6041 /*
6042 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6043 * DS lease to be equal to the MDS lease.
6044 */
6045 clp->cl_lease_time = lease_time;
6046 clp->cl_last_renewal = jiffies;
6047 }
6048 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006049
Trond Myklebust7b38c362012-05-23 13:23:31 -04006050 ret = nfs41_check_session_ready(clp);
6051 if (ret)
6052 return ret;
6053 /* Test for the DS role */
6054 if (!is_ds_client(clp))
6055 return -ENODEV;
6056 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006057}
6058EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6059
6060
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006061/*
6062 * Renew the cl_session lease.
6063 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006064struct nfs4_sequence_data {
6065 struct nfs_client *clp;
6066 struct nfs4_sequence_args args;
6067 struct nfs4_sequence_res res;
6068};
6069
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006070static void nfs41_sequence_release(void *data)
6071{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006072 struct nfs4_sequence_data *calldata = data;
6073 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006074
Alexandros Batsakis71358402010-02-05 03:45:05 -08006075 if (atomic_read(&clp->cl_count) > 1)
6076 nfs4_schedule_state_renewal(clp);
6077 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006078 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006079}
6080
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006081static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6082{
6083 switch(task->tk_status) {
6084 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006085 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6086 return -EAGAIN;
6087 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006088 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006089 }
6090 return 0;
6091}
6092
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006093static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006094{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006095 struct nfs4_sequence_data *calldata = data;
6096 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006097
Trond Myklebust14516c32010-07-31 14:29:06 -04006098 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6099 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006100
6101 if (task->tk_status < 0) {
6102 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006103 if (atomic_read(&clp->cl_count) == 1)
6104 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006105
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006106 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6107 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006108 return;
6109 }
6110 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006111 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006112out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006113 dprintk("<-- %s\n", __func__);
6114}
6115
6116static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6117{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006118 struct nfs4_sequence_data *calldata = data;
6119 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006120 struct nfs4_sequence_args *args;
6121 struct nfs4_sequence_res *res;
6122
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006123 args = task->tk_msg.rpc_argp;
6124 res = task->tk_msg.rpc_resp;
6125
Trond Myklebust9d12b212012-01-17 22:04:25 -05006126 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006127 return;
6128 rpc_call_start(task);
6129}
6130
6131static const struct rpc_call_ops nfs41_sequence_ops = {
6132 .rpc_call_done = nfs41_sequence_call_done,
6133 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006134 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006135};
6136
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006137static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006138{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006139 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006140 struct rpc_message msg = {
6141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6142 .rpc_cred = cred,
6143 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006144 struct rpc_task_setup task_setup_data = {
6145 .rpc_client = clp->cl_rpcclient,
6146 .rpc_message = &msg,
6147 .callback_ops = &nfs41_sequence_ops,
6148 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6149 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006150
Alexandros Batsakis71358402010-02-05 03:45:05 -08006151 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006152 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006153 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006154 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006155 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006156 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006157 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006158 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006159 msg.rpc_argp = &calldata->args;
6160 msg.rpc_resp = &calldata->res;
6161 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006162 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006163
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006164 return rpc_run_task(&task_setup_data);
6165}
6166
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006167static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006168{
6169 struct rpc_task *task;
6170 int ret = 0;
6171
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006172 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6173 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006174 task = _nfs41_proc_sequence(clp, cred);
6175 if (IS_ERR(task))
6176 ret = PTR_ERR(task);
6177 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006178 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006179 dprintk("<-- %s status=%d\n", __func__, ret);
6180 return ret;
6181}
6182
6183static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6184{
6185 struct rpc_task *task;
6186 int ret;
6187
6188 task = _nfs41_proc_sequence(clp, cred);
6189 if (IS_ERR(task)) {
6190 ret = PTR_ERR(task);
6191 goto out;
6192 }
6193 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006194 if (!ret) {
6195 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6196
6197 if (task->tk_status == 0)
6198 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006199 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006200 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006201 rpc_put_task(task);
6202out:
6203 dprintk("<-- %s status=%d\n", __func__, ret);
6204 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006205}
6206
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006207struct nfs4_reclaim_complete_data {
6208 struct nfs_client *clp;
6209 struct nfs41_reclaim_complete_args arg;
6210 struct nfs41_reclaim_complete_res res;
6211};
6212
6213static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6214{
6215 struct nfs4_reclaim_complete_data *calldata = data;
6216
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006217 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006218 if (nfs41_setup_sequence(calldata->clp->cl_session,
6219 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006220 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006221 return;
6222
6223 rpc_call_start(task);
6224}
6225
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006226static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6227{
6228 switch(task->tk_status) {
6229 case 0:
6230 case -NFS4ERR_COMPLETE_ALREADY:
6231 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6232 break;
6233 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006234 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006235 /* fall through */
6236 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006237 return -EAGAIN;
6238 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006239 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006240 }
6241 return 0;
6242}
6243
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006244static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6245{
6246 struct nfs4_reclaim_complete_data *calldata = data;
6247 struct nfs_client *clp = calldata->clp;
6248 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6249
6250 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006251 if (!nfs41_sequence_done(task, res))
6252 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006253
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006254 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6255 rpc_restart_call_prepare(task);
6256 return;
6257 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006258 dprintk("<-- %s\n", __func__);
6259}
6260
6261static void nfs4_free_reclaim_complete_data(void *data)
6262{
6263 struct nfs4_reclaim_complete_data *calldata = data;
6264
6265 kfree(calldata);
6266}
6267
6268static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6269 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6270 .rpc_call_done = nfs4_reclaim_complete_done,
6271 .rpc_release = nfs4_free_reclaim_complete_data,
6272};
6273
6274/*
6275 * Issue a global reclaim complete.
6276 */
6277static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6278{
6279 struct nfs4_reclaim_complete_data *calldata;
6280 struct rpc_task *task;
6281 struct rpc_message msg = {
6282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6283 };
6284 struct rpc_task_setup task_setup_data = {
6285 .rpc_client = clp->cl_rpcclient,
6286 .rpc_message = &msg,
6287 .callback_ops = &nfs4_reclaim_complete_call_ops,
6288 .flags = RPC_TASK_ASYNC,
6289 };
6290 int status = -ENOMEM;
6291
6292 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006293 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006294 if (calldata == NULL)
6295 goto out;
6296 calldata->clp = clp;
6297 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006298
Trond Myklebust9d12b212012-01-17 22:04:25 -05006299 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006300 msg.rpc_argp = &calldata->arg;
6301 msg.rpc_resp = &calldata->res;
6302 task_setup_data.callback_data = calldata;
6303 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006304 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006305 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006306 goto out;
6307 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006308 status = nfs4_wait_for_completion_rpc_task(task);
6309 if (status == 0)
6310 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006311 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006312 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006313out:
6314 dprintk("<-- %s status=%d\n", __func__, status);
6315 return status;
6316}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006317
6318static void
6319nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6320{
6321 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006322 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006323
6324 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006325 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6326 * right now covering the LAYOUTGET we are about to send.
6327 * However, that is not so catastrophic, and there seems
6328 * to be no way to prevent it completely.
6329 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006330 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006331 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006332 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006333 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6334 NFS_I(lgp->args.inode)->layout,
6335 lgp->args.ctx->state)) {
6336 rpc_exit(task, NFS4_OK);
6337 return;
6338 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006339 rpc_call_start(task);
6340}
6341
6342static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6343{
6344 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006345 struct inode *inode = lgp->args.inode;
6346 struct nfs_server *server = NFS_SERVER(inode);
6347 struct pnfs_layout_hdr *lo;
6348 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006349
6350 dprintk("--> %s\n", __func__);
6351
6352 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006353 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006354
6355 switch (task->tk_status) {
6356 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006357 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006358 case -NFS4ERR_LAYOUTTRYLATER:
6359 case -NFS4ERR_RECALLCONFLICT:
6360 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006361 break;
6362 case -NFS4ERR_EXPIRED:
6363 case -NFS4ERR_BAD_STATEID:
6364 spin_lock(&inode->i_lock);
6365 lo = NFS_I(inode)->layout;
6366 if (!lo || list_empty(&lo->plh_segs)) {
6367 spin_unlock(&inode->i_lock);
6368 /* If the open stateid was bad, then recover it. */
6369 state = lgp->args.ctx->state;
6370 } else {
6371 LIST_HEAD(head);
6372
6373 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6374 spin_unlock(&inode->i_lock);
6375 /* Mark the bad layout state as invalid, then
6376 * retry using the open stateid. */
6377 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006378 }
6379 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006380 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6381 rpc_restart_call_prepare(task);
6382out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006383 dprintk("<-- %s\n", __func__);
6384}
6385
Idan Kedar85541162012-08-02 11:47:10 +03006386static size_t max_response_pages(struct nfs_server *server)
6387{
6388 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6389 return nfs_page_array_len(0, max_resp_sz);
6390}
6391
6392static void nfs4_free_pages(struct page **pages, size_t size)
6393{
6394 int i;
6395
6396 if (!pages)
6397 return;
6398
6399 for (i = 0; i < size; i++) {
6400 if (!pages[i])
6401 break;
6402 __free_page(pages[i]);
6403 }
6404 kfree(pages);
6405}
6406
6407static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6408{
6409 struct page **pages;
6410 int i;
6411
6412 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6413 if (!pages) {
6414 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6415 return NULL;
6416 }
6417
6418 for (i = 0; i < size; i++) {
6419 pages[i] = alloc_page(gfp_flags);
6420 if (!pages[i]) {
6421 dprintk("%s: failed to allocate page\n", __func__);
6422 nfs4_free_pages(pages, size);
6423 return NULL;
6424 }
6425 }
6426
6427 return pages;
6428}
6429
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006430static void nfs4_layoutget_release(void *calldata)
6431{
6432 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006433 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6434 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006435
6436 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006437 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006438 put_nfs_open_context(lgp->args.ctx);
6439 kfree(calldata);
6440 dprintk("<-- %s\n", __func__);
6441}
6442
6443static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6444 .rpc_call_prepare = nfs4_layoutget_prepare,
6445 .rpc_call_done = nfs4_layoutget_done,
6446 .rpc_release = nfs4_layoutget_release,
6447};
6448
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006449struct pnfs_layout_segment *
6450nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006451{
6452 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006453 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006454 struct rpc_task *task;
6455 struct rpc_message msg = {
6456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6457 .rpc_argp = &lgp->args,
6458 .rpc_resp = &lgp->res,
6459 };
6460 struct rpc_task_setup task_setup_data = {
6461 .rpc_client = server->client,
6462 .rpc_message = &msg,
6463 .callback_ops = &nfs4_layoutget_call_ops,
6464 .callback_data = lgp,
6465 .flags = RPC_TASK_ASYNC,
6466 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006467 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006468 int status = 0;
6469
6470 dprintk("--> %s\n", __func__);
6471
Idan Kedar85541162012-08-02 11:47:10 +03006472 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6473 if (!lgp->args.layout.pages) {
6474 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006475 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006476 }
6477 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6478
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006479 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006480 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006481 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006482 task = rpc_run_task(&task_setup_data);
6483 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006484 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006485 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006486 if (status == 0)
6487 status = task->tk_status;
6488 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006489 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006490 rpc_put_task(task);
6491 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006492 if (status)
6493 return ERR_PTR(status);
6494 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006495}
6496
Benny Halevycbe82602011-05-22 19:52:37 +03006497static void
6498nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6499{
6500 struct nfs4_layoutreturn *lrp = calldata;
6501
6502 dprintk("--> %s\n", __func__);
6503 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006504 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006505 return;
6506 rpc_call_start(task);
6507}
6508
6509static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6510{
6511 struct nfs4_layoutreturn *lrp = calldata;
6512 struct nfs_server *server;
6513
6514 dprintk("--> %s\n", __func__);
6515
6516 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6517 return;
6518
6519 server = NFS_SERVER(lrp->args.inode);
6520 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006521 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006522 return;
6523 }
Benny Halevycbe82602011-05-22 19:52:37 +03006524 dprintk("<-- %s\n", __func__);
6525}
6526
6527static void nfs4_layoutreturn_release(void *calldata)
6528{
6529 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006530 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006531
6532 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006533 spin_lock(&lo->plh_inode->i_lock);
6534 if (lrp->res.lrs_present)
6535 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6536 lo->plh_block_lgets--;
6537 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006538 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006539 kfree(calldata);
6540 dprintk("<-- %s\n", __func__);
6541}
6542
6543static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6544 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6545 .rpc_call_done = nfs4_layoutreturn_done,
6546 .rpc_release = nfs4_layoutreturn_release,
6547};
6548
6549int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6550{
6551 struct rpc_task *task;
6552 struct rpc_message msg = {
6553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6554 .rpc_argp = &lrp->args,
6555 .rpc_resp = &lrp->res,
6556 };
6557 struct rpc_task_setup task_setup_data = {
6558 .rpc_client = lrp->clp->cl_rpcclient,
6559 .rpc_message = &msg,
6560 .callback_ops = &nfs4_layoutreturn_call_ops,
6561 .callback_data = lrp,
6562 };
6563 int status;
6564
6565 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006566 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006567 task = rpc_run_task(&task_setup_data);
6568 if (IS_ERR(task))
6569 return PTR_ERR(task);
6570 status = task->tk_status;
6571 dprintk("<-- %s status=%d\n", __func__, status);
6572 rpc_put_task(task);
6573 return status;
6574}
6575
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006576/*
6577 * Retrieve the list of Data Server devices from the MDS.
6578 */
6579static int _nfs4_getdevicelist(struct nfs_server *server,
6580 const struct nfs_fh *fh,
6581 struct pnfs_devicelist *devlist)
6582{
6583 struct nfs4_getdevicelist_args args = {
6584 .fh = fh,
6585 .layoutclass = server->pnfs_curr_ld->id,
6586 };
6587 struct nfs4_getdevicelist_res res = {
6588 .devlist = devlist,
6589 };
6590 struct rpc_message msg = {
6591 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6592 .rpc_argp = &args,
6593 .rpc_resp = &res,
6594 };
6595 int status;
6596
6597 dprintk("--> %s\n", __func__);
6598 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6599 &res.seq_res, 0);
6600 dprintk("<-- %s status=%d\n", __func__, status);
6601 return status;
6602}
6603
6604int nfs4_proc_getdevicelist(struct nfs_server *server,
6605 const struct nfs_fh *fh,
6606 struct pnfs_devicelist *devlist)
6607{
6608 struct nfs4_exception exception = { };
6609 int err;
6610
6611 do {
6612 err = nfs4_handle_exception(server,
6613 _nfs4_getdevicelist(server, fh, devlist),
6614 &exception);
6615 } while (exception.retry);
6616
6617 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6618 err, devlist->num_devs);
6619
6620 return err;
6621}
6622EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6623
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006624static int
6625_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6626{
6627 struct nfs4_getdeviceinfo_args args = {
6628 .pdev = pdev,
6629 };
6630 struct nfs4_getdeviceinfo_res res = {
6631 .pdev = pdev,
6632 };
6633 struct rpc_message msg = {
6634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6635 .rpc_argp = &args,
6636 .rpc_resp = &res,
6637 };
6638 int status;
6639
6640 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006641 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006642 dprintk("<-- %s status=%d\n", __func__, status);
6643
6644 return status;
6645}
6646
6647int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6648{
6649 struct nfs4_exception exception = { };
6650 int err;
6651
6652 do {
6653 err = nfs4_handle_exception(server,
6654 _nfs4_proc_getdeviceinfo(server, pdev),
6655 &exception);
6656 } while (exception.retry);
6657 return err;
6658}
6659EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6660
Andy Adamson863a3c62011-03-23 13:27:54 +00006661static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6662{
6663 struct nfs4_layoutcommit_data *data = calldata;
6664 struct nfs_server *server = NFS_SERVER(data->args.inode);
6665
6666 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006667 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006668 return;
6669 rpc_call_start(task);
6670}
6671
6672static void
6673nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6674{
6675 struct nfs4_layoutcommit_data *data = calldata;
6676 struct nfs_server *server = NFS_SERVER(data->args.inode);
6677
6678 if (!nfs4_sequence_done(task, &data->res.seq_res))
6679 return;
6680
6681 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006682 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6683 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6684 case -NFS4ERR_BADLAYOUT: /* no layout */
6685 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006686 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006687 break;
6688 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006689 nfs_post_op_update_inode_force_wcc(data->args.inode,
6690 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006691 break;
6692 default:
6693 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6694 rpc_restart_call_prepare(task);
6695 return;
6696 }
6697 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006698}
6699
6700static void nfs4_layoutcommit_release(void *calldata)
6701{
6702 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006703 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006704 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006705
Andy Adamsondb29c082011-07-30 20:52:38 -04006706 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006707 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006708 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6709 list_del_init(&lseg->pls_lc_list);
6710 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6711 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006712 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006713 }
Peng Tao92407e72011-10-23 20:21:17 -07006714
6715 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6716 smp_mb__after_clear_bit();
6717 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6718
Andy Adamson863a3c62011-03-23 13:27:54 +00006719 put_rpccred(data->cred);
6720 kfree(data);
6721}
6722
6723static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6724 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6725 .rpc_call_done = nfs4_layoutcommit_done,
6726 .rpc_release = nfs4_layoutcommit_release,
6727};
6728
6729int
Andy Adamsonef311532011-03-12 02:58:10 -05006730nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006731{
6732 struct rpc_message msg = {
6733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6734 .rpc_argp = &data->args,
6735 .rpc_resp = &data->res,
6736 .rpc_cred = data->cred,
6737 };
6738 struct rpc_task_setup task_setup_data = {
6739 .task = &data->task,
6740 .rpc_client = NFS_CLIENT(data->args.inode),
6741 .rpc_message = &msg,
6742 .callback_ops = &nfs4_layoutcommit_ops,
6743 .callback_data = data,
6744 .flags = RPC_TASK_ASYNC,
6745 };
6746 struct rpc_task *task;
6747 int status = 0;
6748
6749 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6750 "lbw: %llu inode %lu\n",
6751 data->task.tk_pid, sync,
6752 data->args.lastbytewritten,
6753 data->args.inode->i_ino);
6754
Trond Myklebust9d12b212012-01-17 22:04:25 -05006755 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006756 task = rpc_run_task(&task_setup_data);
6757 if (IS_ERR(task))
6758 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006759 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006760 goto out;
6761 status = nfs4_wait_for_completion_rpc_task(task);
6762 if (status != 0)
6763 goto out;
6764 status = task->tk_status;
6765out:
6766 dprintk("%s: status %d\n", __func__, status);
6767 rpc_put_task(task);
6768 return status;
6769}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006770
6771static int
6772_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6773 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6774{
6775 struct nfs41_secinfo_no_name_args args = {
6776 .style = SECINFO_STYLE_CURRENT_FH,
6777 };
6778 struct nfs4_secinfo_res res = {
6779 .flavors = flavors,
6780 };
6781 struct rpc_message msg = {
6782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6783 .rpc_argp = &args,
6784 .rpc_resp = &res,
6785 };
6786 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6787}
6788
6789static int
6790nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6791 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6792{
6793 struct nfs4_exception exception = { };
6794 int err;
6795 do {
6796 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6797 switch (err) {
6798 case 0:
6799 case -NFS4ERR_WRONGSEC:
6800 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006801 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006802 default:
6803 err = nfs4_handle_exception(server, err, &exception);
6804 }
6805 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006806out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006807 return err;
6808}
6809
6810static int
6811nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6812 struct nfs_fsinfo *info)
6813{
6814 int err;
6815 struct page *page;
6816 rpc_authflavor_t flavor;
6817 struct nfs4_secinfo_flavors *flavors;
6818
6819 page = alloc_page(GFP_KERNEL);
6820 if (!page) {
6821 err = -ENOMEM;
6822 goto out;
6823 }
6824
6825 flavors = page_address(page);
6826 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6827
6828 /*
6829 * Fall back on "guess and check" method if
6830 * the server doesn't support SECINFO_NO_NAME
6831 */
6832 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6833 err = nfs4_find_root_sec(server, fhandle, info);
6834 goto out_freepage;
6835 }
6836 if (err)
6837 goto out_freepage;
6838
6839 flavor = nfs_find_best_sec(flavors);
6840 if (err == 0)
6841 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6842
6843out_freepage:
6844 put_page(page);
6845 if (err == -EACCES)
6846 return -EPERM;
6847out:
6848 return err;
6849}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006850
6851static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006852{
6853 int status;
6854 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006855 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006856 };
6857 struct nfs41_test_stateid_res res;
6858 struct rpc_message msg = {
6859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6860 .rpc_argp = &args,
6861 .rpc_resp = &res,
6862 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006863
Chuck Lever38527b12012-07-11 16:30:23 -04006864 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006865 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006866 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006867 if (status != NFS_OK) {
6868 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006869 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006870 }
6871 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006872 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006873}
6874
Chuck Lever38527b12012-07-11 16:30:23 -04006875/**
6876 * nfs41_test_stateid - perform a TEST_STATEID operation
6877 *
6878 * @server: server / transport on which to perform the operation
6879 * @stateid: state ID to test
6880 *
6881 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6882 * Otherwise a negative NFS4ERR value is returned if the operation
6883 * failed or the state ID is not currently valid.
6884 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006885static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006886{
6887 struct nfs4_exception exception = { };
6888 int err;
6889 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006890 err = _nfs41_test_stateid(server, stateid);
6891 if (err != -NFS4ERR_DELAY)
6892 break;
6893 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006894 } while (exception.retry);
6895 return err;
6896}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006897
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006898static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006899{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006900 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006901 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006902 };
6903 struct nfs41_free_stateid_res res;
6904 struct rpc_message msg = {
6905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6906 .rpc_argp = &args,
6907 .rpc_resp = &res,
6908 };
Chuck Lever38527b12012-07-11 16:30:23 -04006909 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006910
Chuck Lever38527b12012-07-11 16:30:23 -04006911 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006912 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006913 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006914 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006915 dprintk("NFS reply free_stateid: %d\n", status);
6916 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006917}
6918
Chuck Lever38527b12012-07-11 16:30:23 -04006919/**
6920 * nfs41_free_stateid - perform a FREE_STATEID operation
6921 *
6922 * @server: server / transport on which to perform the operation
6923 * @stateid: state ID to release
6924 *
6925 * Returns NFS_OK if the server freed "stateid". Otherwise a
6926 * negative NFS4ERR value is returned.
6927 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006928static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006929{
6930 struct nfs4_exception exception = { };
6931 int err;
6932 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006933 err = _nfs4_free_stateid(server, stateid);
6934 if (err != -NFS4ERR_DELAY)
6935 break;
6936 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006937 } while (exception.retry);
6938 return err;
6939}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006940
6941static bool nfs41_match_stateid(const nfs4_stateid *s1,
6942 const nfs4_stateid *s2)
6943{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006944 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006945 return false;
6946
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006947 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006948 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006949 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006950 return true;
6951
6952 return false;
6953}
6954
Andy Adamson557134a2009-04-01 09:21:53 -04006955#endif /* CONFIG_NFS_V4_1 */
6956
Trond Myklebust36281ca2012-03-04 18:13:56 -05006957static bool nfs4_match_stateid(const nfs4_stateid *s1,
6958 const nfs4_stateid *s2)
6959{
Trond Myklebustf597c532012-03-04 18:13:56 -05006960 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006961}
6962
6963
Trond Myklebust17280172012-03-11 13:11:00 -04006964static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006965 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006966 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 .recover_open = nfs4_open_reclaim,
6968 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006969 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006970 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006971 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972};
6973
Andy Adamson591d71c2009-04-01 09:22:47 -04006974#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006975static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006976 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6977 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6978 .recover_open = nfs4_open_reclaim,
6979 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006980 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006981 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006982 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006983 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006984};
6985#endif /* CONFIG_NFS_V4_1 */
6986
Trond Myklebust17280172012-03-11 13:11:00 -04006987static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006988 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006989 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 .recover_open = nfs4_open_expired,
6991 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006992 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006993 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994};
6995
Andy Adamson591d71c2009-04-01 09:22:47 -04006996#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006997static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006998 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6999 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007000 .recover_open = nfs41_open_expired,
7001 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007002 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007003 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007004};
7005#endif /* CONFIG_NFS_V4_1 */
7006
Trond Myklebust17280172012-03-11 13:11:00 -04007007static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007008 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007009 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007010 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007011};
7012
7013#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007014static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007015 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007016 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007017 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007018};
7019#endif
7020
Trond Myklebust97dc1352010-06-16 09:52:26 -04007021static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7022 .minor_version = 0,
7023 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007024 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007025 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007026 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7027 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7028 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007029};
7030
7031#if defined(CONFIG_NFS_V4_1)
7032static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7033 .minor_version = 1,
7034 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007035 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007036 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007037 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7038 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7039 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007040};
7041#endif
7042
Trond Myklebust97dc1352010-06-16 09:52:26 -04007043const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7044 [0] = &nfs_v4_0_minor_ops,
7045#if defined(CONFIG_NFS_V4_1)
7046 [1] = &nfs_v4_1_minor_ops,
7047#endif
7048};
7049
Bryan Schumaker73a79702012-07-16 16:39:12 -04007050const struct inode_operations nfs4_dir_inode_operations = {
7051 .create = nfs_create,
7052 .lookup = nfs_lookup,
7053 .atomic_open = nfs_atomic_open,
7054 .link = nfs_link,
7055 .unlink = nfs_unlink,
7056 .symlink = nfs_symlink,
7057 .mkdir = nfs_mkdir,
7058 .rmdir = nfs_rmdir,
7059 .mknod = nfs_mknod,
7060 .rename = nfs_rename,
7061 .permission = nfs_permission,
7062 .getattr = nfs_getattr,
7063 .setattr = nfs_setattr,
7064 .getxattr = generic_getxattr,
7065 .setxattr = generic_setxattr,
7066 .listxattr = generic_listxattr,
7067 .removexattr = generic_removexattr,
7068};
7069
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007070static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007071 .permission = nfs_permission,
7072 .getattr = nfs_getattr,
7073 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007074 .getxattr = generic_getxattr,
7075 .setxattr = generic_setxattr,
7076 .listxattr = generic_listxattr,
7077 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007078};
7079
David Howells509de812006-08-22 20:06:11 -04007080const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 .version = 4, /* protocol version */
7082 .dentry_ops = &nfs4_dentry_operations,
7083 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007084 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007085 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007087 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007088 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 .getattr = nfs4_proc_getattr,
7090 .setattr = nfs4_proc_setattr,
7091 .lookup = nfs4_proc_lookup,
7092 .access = nfs4_proc_access,
7093 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094 .create = nfs4_proc_create,
7095 .remove = nfs4_proc_remove,
7096 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007097 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 .unlink_done = nfs4_proc_unlink_done,
7099 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007100 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007101 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007102 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 .link = nfs4_proc_link,
7104 .symlink = nfs4_proc_symlink,
7105 .mkdir = nfs4_proc_mkdir,
7106 .rmdir = nfs4_proc_remove,
7107 .readdir = nfs4_proc_readdir,
7108 .mknod = nfs4_proc_mknod,
7109 .statfs = nfs4_proc_statfs,
7110 .fsinfo = nfs4_proc_fsinfo,
7111 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007112 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 .decode_dirent = nfs4_decode_dirent,
7114 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007115 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007116 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007117 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007119 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007120 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007121 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007123 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007124 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007126 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007127 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007128 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007129 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007130 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007131 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007132 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007133 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007134 .create_server = nfs4_create_server,
7135 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136};
7137
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007138static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7139 .prefix = XATTR_NAME_NFSV4_ACL,
7140 .list = nfs4_xattr_list_nfs4_acl,
7141 .get = nfs4_xattr_get_nfs4_acl,
7142 .set = nfs4_xattr_set_nfs4_acl,
7143};
7144
7145const struct xattr_handler *nfs4_xattr_handlers[] = {
7146 &nfs4_xattr_nfs4_acl_handler,
7147 NULL
7148};
7149
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150/*
7151 * Local variables:
7152 * c-basic-offset: 8
7153 * End:
7154 */