blob: 80bb5055d0b3c3de96aadb897168633f27472cc0 [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 Myklebustef159e92012-02-06 19:50:40 -050075static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050081static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040083static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040084static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040085static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
86 struct nfs_fattr *fattr, struct iattr *sattr,
87 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
90static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050093static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Trond Myklebust52567b02009-10-23 14:46:42 -040095 if (err >= -1000)
96 return err;
97 switch (err) {
98 case -NFS4ERR_RESOURCE:
99 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000100 case -NFS4ERR_WRONGSEC:
101 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800102 case -NFS4ERR_BADOWNER:
103 case -NFS4ERR_BADNAME:
104 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400105 case -NFS4ERR_SHARE_DENIED:
106 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400107 case -NFS4ERR_MINOR_VERS_MISMATCH:
108 return -EPROTONOSUPPORT;
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155const u32 nfs4_statfs_bitmap[2] = {
156 FATTR4_WORD0_FILES_AVAIL
157 | FATTR4_WORD0_FILES_FREE
158 | FATTR4_WORD0_FILES_TOTAL,
159 FATTR4_WORD1_SPACE_AVAIL
160 | FATTR4_WORD1_SPACE_FREE
161 | FATTR4_WORD1_SPACE_TOTAL
162};
163
Trond Myklebust4ce79712005-06-22 17:16:21 +0000164const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 FATTR4_WORD0_MAXLINK
166 | FATTR4_WORD0_MAXNAME,
167 0
168};
169
Fred Isamandae100c2011-07-30 20:52:37 -0400170const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 | FATTR4_WORD0_MAXREAD
172 | FATTR4_WORD0_MAXWRITE
173 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700174 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400175 | FATTR4_WORD1_FS_LAYOUT_TYPES,
176 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Manoj Naik830b8e32006-06-09 09:34:25 -0400179const u32 nfs4_fs_locations_bitmap[2] = {
180 FATTR4_WORD0_TYPE
181 | FATTR4_WORD0_CHANGE
182 | FATTR4_WORD0_SIZE
183 | FATTR4_WORD0_FSID
184 | FATTR4_WORD0_FILEID
185 | FATTR4_WORD0_FS_LOCATIONS,
186 FATTR4_WORD1_MODE
187 | FATTR4_WORD1_NUMLINKS
188 | FATTR4_WORD1_OWNER
189 | FATTR4_WORD1_OWNER_GROUP
190 | FATTR4_WORD1_RAWDEV
191 | FATTR4_WORD1_SPACE_USED
192 | FATTR4_WORD1_TIME_ACCESS
193 | FATTR4_WORD1_TIME_METADATA
194 | FATTR4_WORD1_TIME_MODIFY
195 | FATTR4_WORD1_MOUNTED_ON_FILEID
196};
197
Al Virobc4785c2006-10-19 23:28:51 -0700198static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct nfs4_readdir_arg *readdir)
200{
Al Viro0dbb4c62006-10-19 23:28:49 -0700201 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 BUG_ON(readdir->count < 80);
204 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000205 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
207 return;
208 }
209
210 readdir->cookie = 0;
211 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
212 if (cookie == 2)
213 return;
214
215 /*
216 * NFSv4 servers do not return entries for '.' and '..'
217 * Therefore, we fake these entries here. We let '.'
218 * have cookie 0 and '..' have cookie 1. Note that
219 * when talking to the server, we always send cookie 0
220 * instead of 1 or 2.
221 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800222 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (cookie == 0) {
225 *p++ = xdr_one; /* next */
226 *p++ = xdr_zero; /* cookie, first word */
227 *p++ = xdr_one; /* cookie, second word */
228 *p++ = xdr_one; /* entry len */
229 memcpy(p, ".\0\0\0", 4); /* entry */
230 p++;
231 *p++ = xdr_one; /* bitmap length */
232 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
233 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400234 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
237 *p++ = xdr_one; /* next */
238 *p++ = xdr_zero; /* cookie, first word */
239 *p++ = xdr_two; /* cookie, second word */
240 *p++ = xdr_two; /* entry len */
241 memcpy(p, "..\0\0", 4); /* entry */
242 p++;
243 *p++ = xdr_one; /* bitmap length */
244 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
245 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400246 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 readdir->pgbase = (char *)p - (char *)start;
249 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800250 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Trond Myklebust65de8722008-12-23 15:21:44 -0500253static int nfs4_wait_clnt_recover(struct nfs_client *clp)
254{
255 int res;
256
257 might_sleep();
258
Trond Myklebuste005e802008-12-23 15:21:48 -0500259 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400260 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500261 return res;
262}
263
264static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
265{
266 int res = 0;
267
268 might_sleep();
269
270 if (*timeout <= 0)
271 *timeout = NFS4_POLL_RETRY_MIN;
272 if (*timeout > NFS4_POLL_RETRY_MAX)
273 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100274 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500275 if (fatal_signal_pending(current))
276 res = -ERESTARTSYS;
277 *timeout <<= 1;
278 return res;
279}
280
281/* This is the error handling routine for processes that are allowed
282 * to sleep.
283 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800284static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500285{
286 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500287 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500288 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 int ret = errorcode;
290
291 exception->retry = 0;
292 switch(errorcode) {
293 case 0:
294 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500295 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400296 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400297 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500298 exception->retry = 1;
299 return 0;
300 }
301 if (state == NULL)
302 break;
303 nfs4_schedule_stateid_recovery(server, state);
304 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500305 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500306 case -NFS4ERR_ADMIN_REVOKED:
307 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500308 if (state == NULL)
309 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400310 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500311 nfs4_schedule_stateid_recovery(server, state);
312 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400313 case -NFS4ERR_EXPIRED:
314 if (state != NULL)
315 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500316 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500317 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500318 nfs4_schedule_lease_recovery(clp);
319 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500320#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400321 case -NFS4ERR_BADSESSION:
322 case -NFS4ERR_BADSLOT:
323 case -NFS4ERR_BAD_HIGH_SLOT:
324 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
325 case -NFS4ERR_DEADSESSION:
326 case -NFS4ERR_SEQ_FALSE_RETRY:
327 case -NFS4ERR_SEQ_MISORDERED:
328 dprintk("%s ERROR: %d Reset session\n", __func__,
329 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400330 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400331 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500332 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500333#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500334 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500335 if (exception->timeout > HZ) {
336 /* We have retried a decent amount, time to
337 * fail
338 */
339 ret = -EBUSY;
340 break;
341 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500342 case -NFS4ERR_GRACE:
343 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500344 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 ret = nfs4_delay(server->client, &exception->timeout);
346 if (ret != 0)
347 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400348 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500349 case -NFS4ERR_OLD_STATEID:
350 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800351 break;
352 case -NFS4ERR_BADOWNER:
353 /* The following works around a Linux server bug! */
354 case -NFS4ERR_BADNAME:
355 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
356 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
357 exception->retry = 1;
358 printk(KERN_WARNING "NFS: v4 server %s "
359 "does not accept raw "
360 "uid/gids. "
361 "Reenabling the idmapper.\n",
362 server->nfs_client->cl_hostname);
363 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500364 }
365 /* We failed to handle the error */
366 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500367wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500368 ret = nfs4_wait_clnt_recover(clp);
369 if (ret == 0)
370 exception->retry = 1;
371 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500372}
373
374
Trond Myklebust452e9352010-07-31 14:29:06 -0400375static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 spin_lock(&clp->cl_lock);
378 if (time_before(clp->cl_last_renewal,timestamp))
379 clp->cl_last_renewal = timestamp;
380 spin_unlock(&clp->cl_lock);
381}
382
Trond Myklebust452e9352010-07-31 14:29:06 -0400383static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
384{
385 do_renew_lease(server->nfs_client, timestamp);
386}
387
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400388#if defined(CONFIG_NFS_V4_1)
389
Benny Halevy510b8172009-04-01 09:22:14 -0400390/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400391 * nfs4_free_slot - free a slot and efficiently update slot table.
392 *
393 * freeing a slot is trivially done by clearing its respective bit
394 * in the bitmap.
395 * If the freed slotid equals highest_used_slotid we want to update it
396 * so that the server would be able to size down the slot table if needed,
397 * otherwise we know that the highest_used_slotid is still in use.
398 * When updating highest_used_slotid there may be "holes" in the bitmap
399 * so we need to scan down from highest_used_slotid to 0 looking for the now
400 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500401 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500402 *
403 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400404 */
405static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500406nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400407{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500408 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400409 /* clear used bit in bitmap */
410 __clear_bit(slotid, tbl->used_slots);
411
412 /* update highest_used_slotid when it is freed */
413 if (slotid == tbl->highest_used_slotid) {
414 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500415 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400416 tbl->highest_used_slotid = slotid;
417 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500418 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400419 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500420 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
421 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400422}
423
Trond Myklebust961a8282012-01-17 22:57:37 -0500424bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
425{
426 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
427 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400428}
429
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800430/*
Andy Adamson42acd022011-01-06 02:04:34 +0000431 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800432 */
Andy Adamson42acd022011-01-06 02:04:34 +0000433static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800434{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400435 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500436 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
437 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800438 return;
439 }
440
Trond Myklebust45d43c22012-02-06 19:38:51 -0500441 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800442 return;
443
Andy Adamson42acd022011-01-06 02:04:34 +0000444 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
445 complete(&ses->fc_slot_table.complete);
446}
447
448/*
449 * Signal state manager thread if session back channel is drained
450 */
451void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
452{
453 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500454 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000455 return;
456 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
457 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800458}
459
Trond Myklebustd185a332010-06-16 09:52:25 -0400460static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400461{
462 struct nfs4_slot_table *tbl;
463
Trond Myklebustd185a332010-06-16 09:52:25 -0400464 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400465 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400466 /* just wake up the next guy waiting since
467 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500468 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500469 return;
Andy Adamson13615872009-04-01 09:22:17 -0400470 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500471
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500472 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500473 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000474 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500475 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400476 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400477}
478
Trond Myklebust14516c32010-07-31 14:29:06 -0400479static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400480{
481 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400482 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400483
484 /*
485 * sr_status remains 1 if an RPC level error occurred. The server
486 * may or may not have processed the sequence operation..
487 * Proceed as if the server received and processed the sequence
488 * operation.
489 */
490 if (res->sr_status == 1)
491 res->sr_status = NFS_OK;
492
Bryan Schumaker468f8612011-04-18 15:57:32 -0400493 /* don't increment the sequence number if the task wasn't sent */
494 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400495 goto out;
496
Andy Adamson691daf32009-12-04 15:55:39 -0500497 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400498 switch (res->sr_status) {
499 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400500 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400501 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400502 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400503 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400504 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500505 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500506 if (res->sr_status_flags != 0)
507 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400508 break;
509 case -NFS4ERR_DELAY:
510 /* The server detected a resend of the RPC call and
511 * returned NFS4ERR_DELAY as per Section 2.10.6.2
512 * of RFC5661.
513 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200514 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400515 __func__,
516 res->sr_slot - res->sr_session->fc_slot_table.slots,
517 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400518 goto out_retry;
519 default:
520 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400521 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400522 }
523out:
524 /* The session may be reset by one of the error handlers. */
525 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400526 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400527 return 1;
528out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400529 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400530 goto out;
531 rpc_delay(task, NFS4_POLL_RETRY_MAX);
532 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400533}
534
Trond Myklebust14516c32010-07-31 14:29:06 -0400535static int nfs4_sequence_done(struct rpc_task *task,
536 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400537{
Trond Myklebust14516c32010-07-31 14:29:06 -0400538 if (res->sr_session == NULL)
539 return 1;
540 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400541}
542
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400543/*
Benny Halevy510b8172009-04-01 09:22:14 -0400544 * nfs4_find_slot - efficiently look for a free slot
545 *
546 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
547 * If found, we mark the slot as used, update the highest_used_slotid,
548 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500549 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400550 *
551 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400552 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500553static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800554nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400555{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500556 u32 slotid;
557 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400558
Trond Myklebust45d43c22012-02-06 19:38:51 -0500559 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400560 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
561 tbl->max_slots);
562 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
563 if (slotid >= tbl->max_slots)
564 goto out;
565 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500566 if (slotid > tbl->highest_used_slotid ||
567 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400568 tbl->highest_used_slotid = slotid;
569 ret_id = slotid;
570out:
571 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
572 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400573 return ret_id;
574}
575
Trond Myklebust9d12b212012-01-17 22:04:25 -0500576static void nfs41_init_sequence(struct nfs4_sequence_args *args,
577 struct nfs4_sequence_res *res, int cache_reply)
578{
579 args->sa_session = NULL;
580 args->sa_cache_this = 0;
581 if (cache_reply)
582 args->sa_cache_this = 1;
583 res->sr_session = NULL;
584 res->sr_slot = NULL;
585}
586
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000587int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400588 struct nfs4_sequence_args *args,
589 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400590 struct rpc_task *task)
591{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400592 struct nfs4_slot *slot;
593 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500594 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595
596 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400598 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599 return 0;
600
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400601 tbl = &session->fc_slot_table;
602
603 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400604 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800605 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500606 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500607 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400608 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500609 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500610 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400611 }
612
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800613 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
614 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
615 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
616 spin_unlock(&tbl->slot_tbl_lock);
617 dprintk("%s enforce FIFO order\n", __func__);
618 return -EAGAIN;
619 }
620
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800621 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500622 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400623 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
624 spin_unlock(&tbl->slot_tbl_lock);
625 dprintk("<-- %s: no free slots\n", __func__);
626 return -EAGAIN;
627 }
628 spin_unlock(&tbl->slot_tbl_lock);
629
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800630 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400631 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400632 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400633 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400634
635 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
636
Benny Halevy99fe60d2009-04-01 09:22:29 -0400637 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400638 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400639 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400640 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400641 /*
642 * sr_status is only set in decode_sequence, and so will remain
643 * set to 1 if an rpc level failure occurs.
644 */
645 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400646 return 0;
647}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000648EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649
Trond Myklebust035168a2010-06-16 09:52:26 -0400650int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400651 struct nfs4_sequence_args *args,
652 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653 struct rpc_task *task)
654{
Trond Myklebust035168a2010-06-16 09:52:26 -0400655 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 int ret = 0;
657
Trond Myklebust9d12b212012-01-17 22:04:25 -0500658 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400660
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200661 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400662 __func__, session->clp, session, res->sr_slot ?
663 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400664
Trond Myklebust9d12b212012-01-17 22:04:25 -0500665 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666out:
667 dprintk("<-- %s status=%d\n", __func__, ret);
668 return ret;
669}
670
671struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400672 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673 struct nfs4_sequence_args *seq_args;
674 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675};
676
677static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
678{
679 struct nfs41_call_sync_data *data = calldata;
680
Trond Myklebust035168a2010-06-16 09:52:26 -0400681 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
682
683 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500684 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400685 return;
686 rpc_call_start(task);
687}
688
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800689static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
690{
691 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
692 nfs41_call_sync_prepare(task, calldata);
693}
694
Andy Adamson69ab40c2009-04-01 09:22:19 -0400695static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
696{
697 struct nfs41_call_sync_data *data = calldata;
698
Trond Myklebust14516c32010-07-31 14:29:06 -0400699 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400700}
701
Trond Myklebust17280172012-03-11 13:11:00 -0400702static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400703 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400704 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400705};
706
Trond Myklebust17280172012-03-11 13:11:00 -0400707static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800708 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
709 .rpc_call_done = nfs41_call_sync_done,
710};
711
Bryan Schumaker7c513052011-03-24 17:12:24 +0000712static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
713 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400714 struct rpc_message *msg,
715 struct nfs4_sequence_args *args,
716 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800717 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400718{
719 int ret;
720 struct rpc_task *task;
721 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400722 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400723 .seq_args = args,
724 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400725 };
726 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400728 .rpc_message = msg,
729 .callback_ops = &nfs41_call_sync_ops,
730 .callback_data = &data
731 };
732
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800733 if (privileged)
734 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400735 task = rpc_run_task(&task_setup);
736 if (IS_ERR(task))
737 ret = PTR_ERR(task);
738 else {
739 ret = task->tk_status;
740 rpc_put_task(task);
741 }
742 return ret;
743}
744
Bryan Schumaker7c513052011-03-24 17:12:24 +0000745int _nfs4_call_sync_session(struct rpc_clnt *clnt,
746 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400747 struct rpc_message *msg,
748 struct nfs4_sequence_args *args,
749 struct nfs4_sequence_res *res,
750 int cache_reply)
751{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500752 nfs41_init_sequence(args, res, cache_reply);
753 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400754}
755
Trond Myklebustdf896452010-06-16 09:52:26 -0400756#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500757static inline
758void nfs41_init_sequence(struct nfs4_sequence_args *args,
759 struct nfs4_sequence_res *res, int cache_reply)
760{
761}
762
Trond Myklebust14516c32010-07-31 14:29:06 -0400763static int nfs4_sequence_done(struct rpc_task *task,
764 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400765{
Trond Myklebust14516c32010-07-31 14:29:06 -0400766 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400767}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400768#endif /* CONFIG_NFS_V4_1 */
769
Bryan Schumaker7c513052011-03-24 17:12:24 +0000770int _nfs4_call_sync(struct rpc_clnt *clnt,
771 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400772 struct rpc_message *msg,
773 struct nfs4_sequence_args *args,
774 struct nfs4_sequence_res *res,
775 int cache_reply)
776{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500777 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000778 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400779}
780
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000781static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000782int nfs4_call_sync(struct rpc_clnt *clnt,
783 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000784 struct rpc_message *msg,
785 struct nfs4_sequence_args *args,
786 struct nfs4_sequence_res *res,
787 int cache_reply)
788{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000789 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
790 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000791}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400792
Trond Myklebust38478b22006-05-25 01:40:57 -0400793static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Trond Myklebust38478b22006-05-25 01:40:57 -0400795 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Trond Myklebust38478b22006-05-25 01:40:57 -0400797 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400798 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700799 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400800 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700801 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400802 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100805struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400806 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807 struct nfs_openargs o_arg;
808 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100809 struct nfs_open_confirmargs c_arg;
810 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500811 struct nfs4_string owner_name;
812 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100813 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400815 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400817 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100819 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400820 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100821 int rpc_status;
822 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823};
824
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400825
826static void nfs4_init_opendata_res(struct nfs4_opendata *p)
827{
828 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400829 p->o_res.seqid = p->o_arg.seqid;
830 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400831 p->o_res.server = p->o_arg.server;
832 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500833 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400834}
835
Al Viro82a2c1b2011-06-22 18:30:55 -0400836static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500837 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400838 const struct iattr *attrs,
839 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100840{
Al Viro82a2c1b2011-06-22 18:30:55 -0400841 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100842 struct inode *dir = parent->d_inode;
843 struct nfs_server *server = NFS_SERVER(dir);
844 struct nfs4_opendata *p;
845
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400846 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847 if (p == NULL)
848 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400849 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100850 if (p->o_arg.seqid == NULL)
851 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400852 nfs_sb_active(dentry->d_sb);
853 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 p->dir = parent;
855 p->owner = sp;
856 atomic_inc(&sp->so_count);
857 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500858 p->o_arg.open_flags = flags;
859 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400860 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400861 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
862 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400863 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100864 p->o_arg.server = server;
865 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400866 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100867 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500868 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500869 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400870
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100871 p->o_arg.u.attrs = &p->attrs;
872 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500873
874 verf[0] = jiffies;
875 verf[1] = current->pid;
876 memcpy(p->o_arg.u.verifier.data, verf,
877 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100878 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100879 p->c_arg.fh = &p->o_res.fh;
880 p->c_arg.stateid = &p->o_res.stateid;
881 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400882 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400883 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100884 return p;
885err_free:
886 kfree(p);
887err:
888 dput(parent);
889 return NULL;
890}
891
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400892static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400894 struct nfs4_opendata *p = container_of(kref,
895 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400896 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400897
898 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400899 if (p->state != NULL)
900 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400901 nfs4_put_state_owner(p->owner);
902 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400903 dput(p->dentry);
904 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500905 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400906 kfree(p);
907}
908
909static void nfs4_opendata_put(struct nfs4_opendata *p)
910{
911 if (p != NULL)
912 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100913}
914
Trond Myklebust06f814a2006-01-03 09:55:07 +0100915static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
916{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100917 int ret;
918
Trond Myklebust06f814a2006-01-03 09:55:07 +0100919 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100920 return ret;
921}
922
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400924{
925 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926
Trond Myklebust536e43d2012-01-17 22:04:26 -0500927 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 goto out;
929 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400930 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500931 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
932 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400933 break;
934 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500935 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
936 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400937 break;
938 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500939 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
940 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400941 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400943 return ret;
944}
945
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400947{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500948 if (delegation == NULL)
949 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400951 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500952 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400953 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500954 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400955 return 1;
956}
957
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100959{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100961 case FMODE_WRITE:
962 state->n_wronly++;
963 break;
964 case FMODE_READ:
965 state->n_rdonly++;
966 break;
967 case FMODE_READ|FMODE_WRITE:
968 state->n_rdwr++;
969 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100971}
972
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400974{
975 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500976 nfs4_stateid_copy(&state->stateid, stateid);
977 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400979 case FMODE_READ:
980 set_bit(NFS_O_RDONLY_STATE, &state->flags);
981 break;
982 case FMODE_WRITE:
983 set_bit(NFS_O_WRONLY_STATE, &state->flags);
984 break;
985 case FMODE_READ|FMODE_WRITE:
986 set_bit(NFS_O_RDWR_STATE, &state->flags);
987 }
988}
989
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400991{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400992 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400994 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400995}
996
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997static 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 -0700998{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400999 /*
1000 * Protect the call to nfs4_state_set_mode_locked and
1001 * serialise the stateid update
1002 */
1003 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001004 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001005 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001006 set_bit(NFS_DELEGATED_STATE, &state->flags);
1007 }
1008 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001010 write_sequnlock(&state->seqlock);
1011 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001013 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001016static 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 -05001017{
1018 struct nfs_inode *nfsi = NFS_I(state->inode);
1019 struct nfs_delegation *deleg_cur;
1020 int ret = 0;
1021
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001023
1024 rcu_read_lock();
1025 deleg_cur = rcu_dereference(nfsi->delegation);
1026 if (deleg_cur == NULL)
1027 goto no_delegation;
1028
1029 spin_lock(&deleg_cur->lock);
1030 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001032 goto no_delegation_unlock;
1033
1034 if (delegation == NULL)
1035 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001036 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001037 goto no_delegation_unlock;
1038
Trond Myklebustb7391f42008-12-23 15:21:52 -05001039 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001041 ret = 1;
1042no_delegation_unlock:
1043 spin_unlock(&deleg_cur->lock);
1044no_delegation:
1045 rcu_read_unlock();
1046
1047 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001049 ret = 1;
1050 }
1051
1052 return ret;
1053}
1054
1055
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057{
1058 struct nfs_delegation *delegation;
1059
1060 rcu_read_lock();
1061 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001063 rcu_read_unlock();
1064 return;
1065 }
1066 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001067 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068}
1069
Trond Myklebust6ee41262007-07-08 14:11:36 -04001070static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071{
1072 struct nfs4_state *state = opendata->state;
1073 struct nfs_inode *nfsi = NFS_I(state->inode);
1074 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001075 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 nfs4_stateid stateid;
1078 int ret = -EAGAIN;
1079
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001082 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083 if (can_open_cached(state, fmode, open_mode)) {
1084 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001085 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001086 goto out_return_state;
1087 }
1088 spin_unlock(&state->owner->so_lock);
1089 }
Trond Myklebust34310432008-12-23 15:21:38 -05001090 rcu_read_lock();
1091 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001092 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001093 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001094 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001095 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001096 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001097 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001098 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001099 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100 if (ret != 0)
1101 goto out;
1102 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001103
1104 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001106 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108out:
1109 return ERR_PTR(ret);
1110out_return_state:
1111 atomic_inc(&state->count);
1112 return state;
1113}
1114
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001115static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1116{
1117 struct inode *inode;
1118 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001119 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001120 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001121
Trond Myklebustaac00a82007-07-05 19:02:21 -04001122 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001123 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001124 goto out;
1125 }
1126
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001127 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001128 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001129 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001130 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001131 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001132 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001133 goto err;
1134 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001135 state = nfs4_get_open_state(inode, data->owner);
1136 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001137 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001138 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001139 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001140 int delegation_flags = 0;
1141
Trond Myklebust003707c2007-07-05 18:07:55 -04001142 rcu_read_lock();
1143 delegation = rcu_dereference(NFS_I(inode)->delegation);
1144 if (delegation)
1145 delegation_flags = delegation->flags;
1146 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001147 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",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001151 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001152 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001153 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 }
Trond Myklebust34310432008-12-23 15:21:38 -05001161
1162 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001163 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001164 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001165out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001166 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001167err_put_inode:
1168 iput(inode);
1169err:
1170 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001171}
1172
Trond Myklebust864472e2006-01-03 09:55:15 +01001173static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1174{
1175 struct nfs_inode *nfsi = NFS_I(state->inode);
1176 struct nfs_open_context *ctx;
1177
1178 spin_lock(&state->inode->i_lock);
1179 list_for_each_entry(ctx, &nfsi->open_files, list) {
1180 if (ctx->state != state)
1181 continue;
1182 get_nfs_open_context(ctx);
1183 spin_unlock(&state->inode->i_lock);
1184 return ctx;
1185 }
1186 spin_unlock(&state->inode->i_lock);
1187 return ERR_PTR(-ENOENT);
1188}
1189
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001190static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1191{
1192 struct nfs4_opendata *opendata;
1193
Al Viro3d4ff432011-06-22 18:40:12 -04001194 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001195 if (opendata == NULL)
1196 return ERR_PTR(-ENOMEM);
1197 opendata->state = state;
1198 atomic_inc(&state->count);
1199 return opendata;
1200}
1201
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001202static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001203{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 int ret;
1206
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001207 opendata->o_arg.open_flags = 0;
1208 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1210 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1211 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001212 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 if (ret != 0)
1214 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001215 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001216 if (IS_ERR(newstate))
1217 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001218 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 return 0;
1221}
1222
1223static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1224{
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 int ret;
1227
1228 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 smp_rmb();
1231 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001232 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001233 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 if (ret != 0)
1235 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001236 if (newstate != state)
1237 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001238 }
1239 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001240 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001241 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001242 if (ret != 0)
1243 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001244 if (newstate != state)
1245 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 }
1247 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001248 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001249 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001250 if (ret != 0)
1251 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001252 if (newstate != state)
1253 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001254 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001255 /*
1256 * We may have performed cached opens for all three recoveries.
1257 * Check if we need to update the current stateid.
1258 */
1259 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001260 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001261 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001263 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001264 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001266 return 0;
1267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/*
1270 * OPEN_RECLAIM:
1271 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001273static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001275 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001276 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001277 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int status;
1279
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001280 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1281 if (IS_ERR(opendata))
1282 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1284 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001285 rcu_read_lock();
1286 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001287 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001288 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001289 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001290 opendata->o_arg.u.delegation_type = delegation_type;
1291 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001292 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return status;
1294}
1295
Trond Myklebust539cd032007-06-05 11:46:42 -04001296static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 struct nfs_server *server = NFS_SERVER(state->inode);
1299 struct nfs4_exception exception = { };
1300 int err;
1301 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001302 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001303 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001304 break;
1305 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 } while (exception.retry);
1307 return err;
1308}
1309
Trond Myklebust864472e2006-01-03 09:55:15 +01001310static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1311{
1312 struct nfs_open_context *ctx;
1313 int ret;
1314
1315 ctx = nfs4_state_find_open_context(state);
1316 if (IS_ERR(ctx))
1317 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001318 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001319 put_nfs_open_context(ctx);
1320 return ret;
1321}
1322
Trond Myklebust13437e12007-07-06 15:10:43 -04001323static 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 -07001324{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001325 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001326 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001328 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1329 if (IS_ERR(opendata))
1330 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001331 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001332 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001333 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001334 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001335 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Trond Myklebust13437e12007-07-06 15:10:43 -04001338int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001341 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int err;
1343 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001344 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 switch (err) {
1346 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001347 case -ENOENT:
1348 case -ESTALE:
1349 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001350 case -NFS4ERR_BADSESSION:
1351 case -NFS4ERR_BADSLOT:
1352 case -NFS4ERR_BAD_HIGH_SLOT:
1353 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1354 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001355 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001356 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 case -NFS4ERR_STALE_CLIENTID:
1358 case -NFS4ERR_STALE_STATEID:
1359 case -NFS4ERR_EXPIRED:
1360 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001361 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001362 goto out;
1363 case -ERESTARTSYS:
1364 /*
1365 * The show must go on: exit, but mark the
1366 * stateid as needing recovery.
1367 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001368 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001369 case -NFS4ERR_ADMIN_REVOKED:
1370 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001371 nfs_inode_find_state_and_recover(state->inode,
1372 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001373 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001374 case -EKEYEXPIRED:
1375 /*
1376 * User RPCSEC_GSS context has expired.
1377 * We cannot recover this stateid now, so
1378 * skip it and allow recovery thread to
1379 * proceed.
1380 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001381 case -ENOMEM:
1382 err = 0;
1383 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385 err = nfs4_handle_exception(server, err, &exception);
1386 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001387out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return err;
1389}
1390
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001391static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1392{
1393 struct nfs4_opendata *data = calldata;
1394
1395 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001396 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001397 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001398 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001399 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001400 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001401 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001402}
1403
1404static void nfs4_open_confirm_release(void *calldata)
1405{
1406 struct nfs4_opendata *data = calldata;
1407 struct nfs4_state *state = NULL;
1408
1409 /* If this request hasn't been cancelled, do nothing */
1410 if (data->cancelled == 0)
1411 goto out_free;
1412 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001413 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001414 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001415 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001416 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001417 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001418out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001419 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001420}
1421
1422static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001423 .rpc_call_done = nfs4_open_confirm_done,
1424 .rpc_release = nfs4_open_confirm_release,
1425};
1426
1427/*
1428 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1429 */
1430static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1431{
1432 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1433 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001434 struct rpc_message msg = {
1435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1436 .rpc_argp = &data->c_arg,
1437 .rpc_resp = &data->c_res,
1438 .rpc_cred = data->owner->so_cred,
1439 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001440 struct rpc_task_setup task_setup_data = {
1441 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001442 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001443 .callback_ops = &nfs4_open_confirm_ops,
1444 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001445 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001446 .flags = RPC_TASK_ASYNC,
1447 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 int status;
1449
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001450 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001451 data->rpc_done = 0;
1452 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001453 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001454 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001455 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001456 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001457 status = nfs4_wait_for_completion_rpc_task(task);
1458 if (status != 0) {
1459 data->cancelled = 1;
1460 smp_wmb();
1461 } else
1462 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001463 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return status;
1465}
1466
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001467static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469 struct nfs4_opendata *data = calldata;
1470 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001471
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1473 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001474 /*
1475 * Check if we still need to send an OPEN call, or if we can use
1476 * a delegation instead.
1477 */
1478 if (data->state != NULL) {
1479 struct nfs_delegation *delegation;
1480
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001481 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001482 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001483 rcu_read_lock();
1484 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001485 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1486 can_open_delegated(delegation, data->o_arg.fmode))
1487 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001488 rcu_read_unlock();
1489 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001490 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001491 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001492 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001493 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001494 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1495 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001496 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001497 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001498 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001499 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001500 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001501 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001502 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001503unlock_no_action:
1504 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001505out_no_action:
1506 task->tk_action = NULL;
1507
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001510static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1511{
1512 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1513 nfs4_open_prepare(task, calldata);
1514}
1515
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516static void nfs4_open_done(struct rpc_task *task, void *calldata)
1517{
1518 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001521
Trond Myklebust14516c32010-07-31 14:29:06 -04001522 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1523 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001524
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 if (task->tk_status == 0) {
1526 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001527 case S_IFREG:
1528 break;
1529 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001531 break;
1532 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001534 break;
1535 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001537 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001538 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001539 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1540 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001541 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001542 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001544
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545static void nfs4_open_release(void *calldata)
1546{
1547 struct nfs4_opendata *data = calldata;
1548 struct nfs4_state *state = NULL;
1549
1550 /* If this request hasn't been cancelled, do nothing */
1551 if (data->cancelled == 0)
1552 goto out_free;
1553 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001554 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001555 goto out_free;
1556 /* In case we need an open_confirm, no cleanup! */
1557 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1558 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001560 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001561 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001563 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001564}
1565
1566static const struct rpc_call_ops nfs4_open_ops = {
1567 .rpc_call_prepare = nfs4_open_prepare,
1568 .rpc_call_done = nfs4_open_done,
1569 .rpc_release = nfs4_open_release,
1570};
1571
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001572static const struct rpc_call_ops nfs4_recover_open_ops = {
1573 .rpc_call_prepare = nfs4_recover_open_prepare,
1574 .rpc_call_done = nfs4_open_done,
1575 .rpc_release = nfs4_open_release,
1576};
1577
1578static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001579{
1580 struct inode *dir = data->dir->d_inode;
1581 struct nfs_server *server = NFS_SERVER(dir);
1582 struct nfs_openargs *o_arg = &data->o_arg;
1583 struct nfs_openres *o_res = &data->o_res;
1584 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001585 struct rpc_message msg = {
1586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1587 .rpc_argp = o_arg,
1588 .rpc_resp = o_res,
1589 .rpc_cred = data->owner->so_cred,
1590 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001591 struct rpc_task_setup task_setup_data = {
1592 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001593 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001594 .callback_ops = &nfs4_open_ops,
1595 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001596 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001597 .flags = RPC_TASK_ASYNC,
1598 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001599 int status;
1600
Trond Myklebust9d12b212012-01-17 22:04:25 -05001601 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001602 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001603 data->rpc_done = 0;
1604 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001605 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001606 if (isrecover)
1607 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001608 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001609 if (IS_ERR(task))
1610 return PTR_ERR(task);
1611 status = nfs4_wait_for_completion_rpc_task(task);
1612 if (status != 0) {
1613 data->cancelled = 1;
1614 smp_wmb();
1615 } else
1616 status = data->rpc_status;
1617 rpc_put_task(task);
1618
1619 return status;
1620}
1621
1622static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1623{
1624 struct inode *dir = data->dir->d_inode;
1625 struct nfs_openres *o_res = &data->o_res;
1626 int status;
1627
1628 status = nfs4_run_open_task(data, 1);
1629 if (status != 0 || !data->rpc_done)
1630 return status;
1631
Trond Myklebust6926afd2012-01-07 13:22:46 -05001632 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1633
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001634 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1635 status = _nfs4_proc_open_confirm(data);
1636 if (status != 0)
1637 return status;
1638 }
1639
1640 return status;
1641}
1642
1643/*
1644 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1645 */
1646static int _nfs4_proc_open(struct nfs4_opendata *data)
1647{
1648 struct inode *dir = data->dir->d_inode;
1649 struct nfs_server *server = NFS_SERVER(dir);
1650 struct nfs_openargs *o_arg = &data->o_arg;
1651 struct nfs_openres *o_res = &data->o_res;
1652 int status;
1653
1654 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001655 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001656 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001657 if (status != 0) {
1658 if (status == -NFS4ERR_BADNAME &&
1659 !(o_arg->open_flags & O_CREAT))
1660 return -ENOENT;
1661 return status;
1662 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001663
Trond Myklebust6926afd2012-01-07 13:22:46 -05001664 nfs_fattr_map_and_free_names(server, &data->f_attr);
1665
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001666 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001667 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001668 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1669 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001671 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001673 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001676 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001677 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Andy Adamsond83217c2011-03-01 01:34:17 +00001680static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001681{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001682 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001683 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001684
Trond Myklebusta78cb572009-08-09 15:06:19 -04001685 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001686 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001687 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001688 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001689 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1690 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001691 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001692 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001693 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001694 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001695 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001696}
1697
Andy Adamsond83217c2011-03-01 01:34:17 +00001698static int nfs4_recover_expired_lease(struct nfs_server *server)
1699{
1700 return nfs4_client_recover_expired_lease(server->nfs_client);
1701}
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703/*
1704 * OPEN_EXPIRED:
1705 * reclaim state on the server after a network partition.
1706 * Assumes caller holds the appropriate lock
1707 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001708static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001710 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001711 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001713 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1714 if (IS_ERR(opendata))
1715 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001716 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001717 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001718 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001719 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001720 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001723static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001724{
Trond Myklebust539cd032007-06-05 11:46:42 -04001725 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001726 struct nfs4_exception exception = { };
1727 int err;
1728
1729 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001730 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001731 switch (err) {
1732 default:
1733 goto out;
1734 case -NFS4ERR_GRACE:
1735 case -NFS4ERR_DELAY:
1736 nfs4_handle_exception(server, err, &exception);
1737 err = 0;
1738 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001739 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001740out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001741 return err;
1742}
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1745{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001747 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Trond Myklebust864472e2006-01-03 09:55:15 +01001749 ctx = nfs4_state_find_open_context(state);
1750 if (IS_ERR(ctx))
1751 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001752 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001753 put_nfs_open_context(ctx);
1754 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001757#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001758static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001759{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001761 nfs4_stateid *stateid = &state->stateid;
1762 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001763
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001764 /* If a state reset has been done, test_stateid is unneeded */
1765 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1766 return;
1767
1768 status = nfs41_test_stateid(server, stateid);
1769 if (status != NFS_OK) {
1770 /* Free the stateid unless the server explicitly
1771 * informs us the stateid is unrecognized. */
1772 if (status != -NFS4ERR_BAD_STATEID)
1773 nfs41_free_stateid(server, stateid);
1774
1775 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1776 }
1777}
1778
1779/**
1780 * nfs41_check_open_stateid - possibly free an open stateid
1781 *
1782 * @state: NFSv4 state for an inode
1783 *
1784 * Returns NFS_OK if recovery for this stateid is now finished.
1785 * Otherwise a negative NFS4ERR value is returned.
1786 */
1787static int nfs41_check_open_stateid(struct nfs4_state *state)
1788{
1789 struct nfs_server *server = NFS_SERVER(state->inode);
1790 nfs4_stateid *stateid = &state->stateid;
1791 int status;
1792
1793 /* If a state reset has been done, test_stateid is unneeded */
1794 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1795 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1796 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1797 return -NFS4ERR_BAD_STATEID;
1798
1799 status = nfs41_test_stateid(server, stateid);
1800 if (status != NFS_OK) {
1801 /* Free the stateid unless the server explicitly
1802 * informs us the stateid is unrecognized. */
1803 if (status != -NFS4ERR_BAD_STATEID)
1804 nfs41_free_stateid(server, stateid);
1805
1806 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1807 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1808 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001809 }
1810 return status;
1811}
1812
1813static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1814{
Chuck Levereb64cf92012-07-11 16:30:05 -04001815 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001816
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001817 nfs41_clear_delegation_stateid(state);
1818 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001819 if (status != NFS_OK)
1820 status = nfs4_open_expired(sp, state);
1821 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001822}
1823#endif
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001826 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1827 * fields corresponding to attributes that were used to store the verifier.
1828 * Make sure we clobber those fields in the later setattr call
1829 */
1830static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1831{
1832 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1833 !(sattr->ia_valid & ATTR_ATIME_SET))
1834 sattr->ia_valid |= ATTR_ATIME;
1835
1836 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1837 !(sattr->ia_valid & ATTR_MTIME_SET))
1838 sattr->ia_valid |= ATTR_MTIME;
1839}
1840
1841/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001842 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 */
Andy Adamson82be4172012-05-23 05:02:35 -04001844static int _nfs4_do_open(struct inode *dir,
1845 struct dentry *dentry,
1846 fmode_t fmode,
1847 int flags,
1848 struct iattr *sattr,
1849 struct rpc_cred *cred,
1850 struct nfs4_state **res,
1851 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 struct nfs4_state_owner *sp;
1854 struct nfs4_state *state = NULL;
1855 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001856 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001857 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001861 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1862 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1864 goto out_err;
1865 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001866 status = nfs4_recover_expired_lease(server);
1867 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001868 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001869 if (dentry->d_inode != NULL)
1870 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001871 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001872 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001873 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001874 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Andy Adamson82be4172012-05-23 05:02:35 -04001876 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1877 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1878 if (!opendata->f_attr.mdsthreshold)
1879 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001880 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001881 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001882 if (dentry->d_inode != NULL)
1883 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001884
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001885 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001887 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001889 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001890 status = PTR_ERR(state);
1891 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001892 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001893 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001894 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001895
1896 if (opendata->o_arg.open_flags & O_EXCL) {
1897 nfs4_exclusive_attrset(opendata, sattr);
1898
1899 nfs_fattr_init(opendata->o_res.f_attr);
1900 status = nfs4_do_setattr(state->inode, cred,
1901 opendata->o_res.f_attr, sattr,
1902 state);
1903 if (status == 0)
1904 nfs_setattr_update_inode(state->inode, sattr);
1905 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1906 }
Andy Adamson82be4172012-05-23 05:02:35 -04001907
1908 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1909 *ctx_th = opendata->f_attr.mdsthreshold;
1910 else
1911 kfree(opendata->f_attr.mdsthreshold);
1912 opendata->f_attr.mdsthreshold = NULL;
1913
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001914 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 *res = state;
1917 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001918err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001919 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001920 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001921err_put_state_owner:
1922 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 *res = NULL;
1925 return status;
1926}
1927
1928
Andy Adamson82be4172012-05-23 05:02:35 -04001929static struct nfs4_state *nfs4_do_open(struct inode *dir,
1930 struct dentry *dentry,
1931 fmode_t fmode,
1932 int flags,
1933 struct iattr *sattr,
1934 struct rpc_cred *cred,
1935 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 struct nfs4_exception exception = { };
1938 struct nfs4_state *res;
1939 int status;
1940
Trond Myklebust2d0dbc62012-06-08 10:58:09 -04001941 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001943 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1944 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (status == 0)
1946 break;
1947 /* NOTE: BAD_SEQID means the server and client disagree about the
1948 * book-keeping w.r.t. state-changing operations
1949 * (OPEN/CLOSE/LOCK/LOCKU...)
1950 * It is actually a sign of a bug on the client or on the server.
1951 *
1952 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001953 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 * have unhashed the old state_owner for us, and that we can
1955 * therefore safely retry using a new one. We should still warn
1956 * the user though...
1957 */
1958 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001959 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001960 " returned a bad sequence-id error!\n",
1961 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 exception.retry = 1;
1963 continue;
1964 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001965 /*
1966 * BAD_STATEID on OPEN means that the server cancelled our
1967 * state before it received the OPEN_CONFIRM.
1968 * Recover by retrying the request as per the discussion
1969 * on Page 181 of RFC3530.
1970 */
1971 if (status == -NFS4ERR_BAD_STATEID) {
1972 exception.retry = 1;
1973 continue;
1974 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001975 if (status == -EAGAIN) {
1976 /* We must have found a delegation */
1977 exception.retry = 1;
1978 continue;
1979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1981 status, &exception));
1982 } while (exception.retry);
1983 return res;
1984}
1985
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001986static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1987 struct nfs_fattr *fattr, struct iattr *sattr,
1988 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001990 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001992 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 .iap = sattr,
1994 .server = server,
1995 .bitmask = server->attr_bitmask,
1996 };
1997 struct nfs_setattrres res = {
1998 .fattr = fattr,
1999 .server = server,
2000 };
2001 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2003 .rpc_argp = &arg,
2004 .rpc_resp = &res,
2005 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002007 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002008 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Trond Myklebust0e574af2005-10-27 22:12:38 -04002010 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Trond Myklebust4fc87962012-03-08 17:42:01 -05002012 if (state != NULL) {
2013 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2014 current->files, current->tgid);
2015 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2016 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002017 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002018 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002019 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Bryan Schumaker7c513052011-03-24 17:12:24 +00002021 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002022 if (status == 0 && state != NULL)
2023 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002024 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002027static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2028 struct nfs_fattr *fattr, struct iattr *sattr,
2029 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002031 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002032 struct nfs4_exception exception = {
2033 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002034 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002035 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int err;
2037 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002038 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2039 switch (err) {
2040 case -NFS4ERR_OPENMODE:
2041 if (state && !(state->state & FMODE_WRITE)) {
2042 err = -EBADF;
2043 if (sattr->ia_valid & ATTR_OPEN)
2044 err = -EACCES;
2045 goto out;
2046 }
2047 }
2048 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002050out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return err;
2052}
2053
2054struct nfs4_closedata {
2055 struct inode *inode;
2056 struct nfs4_state *state;
2057 struct nfs_closeargs arg;
2058 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002059 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002060 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002061 bool roc;
2062 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063};
2064
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002065static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002066{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002067 struct nfs4_closedata *calldata = data;
2068 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002069 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002070
Fred Isamanf7e89172011-01-06 11:36:32 +00002071 if (calldata->roc)
2072 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002073 nfs4_put_open_state(calldata->state);
2074 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002075 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002076 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002077 kfree(calldata);
2078}
2079
Trond Myklebust88069f72009-12-08 08:33:16 -05002080static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2081 fmode_t fmode)
2082{
2083 spin_lock(&state->owner->so_lock);
2084 if (!(fmode & FMODE_READ))
2085 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2086 if (!(fmode & FMODE_WRITE))
2087 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2088 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2089 spin_unlock(&state->owner->so_lock);
2090}
2091
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002092static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002094 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct nfs_server *server = NFS_SERVER(calldata->inode);
2097
Chuck Levera3ca5652012-03-01 17:00:40 -05002098 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002099 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2100 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /* hmm. we are done with the inode, and in the process of freeing
2102 * the state_owner. we keep this around to process errors
2103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 switch (task->tk_status) {
2105 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002106 if (calldata->roc)
2107 pnfs_roc_set_barrier(state->inode,
2108 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002109 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002110 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002111 nfs4_close_clear_stateid_flags(state,
2112 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 break;
2114 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002115 case -NFS4ERR_OLD_STATEID:
2116 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002118 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002121 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2122 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002124 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002125 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002126 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002129static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002131 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002132 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002133 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002134
Chuck Levera3ca5652012-03-01 17:00:40 -05002135 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002136 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002137 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002138
Trond Myklebust88069f72009-12-08 08:33:16 -05002139 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2140 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002141 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002142 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002143 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002144 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002145 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2146 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2147 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002148 }
2149 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002150 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2151 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2152 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002153 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002154 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002155 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002156
2157 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002158 /* Note: exit _without_ calling nfs4_close_done */
2159 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002160 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002161 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002162
Fred Isamanf7e89172011-01-06 11:36:32 +00002163 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002164 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002165 if (calldata->roc &&
2166 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2167 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2168 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002169 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002170 }
2171 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002172
Trond Myklebust516a6af2005-10-27 22:12:41 -04002173 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002174 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002175 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002176 &calldata->arg.seq_args,
2177 &calldata->res.seq_res,
2178 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002179 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002180 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002181out:
2182 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002185static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002186 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002187 .rpc_call_done = nfs4_close_done,
2188 .rpc_release = nfs4_free_closedata,
2189};
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191/*
2192 * It is possible for data to be read/written from a mem-mapped file
2193 * after the sys_close call (which hits the vfs layer as a flush).
2194 * This means that we can't safely call nfsv4 close on a file until
2195 * the inode is cleared. This in turn means that we are not good
2196 * NFSv4 citizens - we do not indicate to the server to update the file's
2197 * share state even when we are done with one of the three share
2198 * stateid's in the inode.
2199 *
2200 * NOTE: Caller must be holding the sp->so_owner semaphore!
2201 */
Al Viro643168c2011-06-22 18:20:23 -04002202int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002204 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002206 struct nfs4_state_owner *sp = state->owner;
2207 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002208 struct rpc_message msg = {
2209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2210 .rpc_cred = state->owner->so_cred,
2211 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002212 struct rpc_task_setup task_setup_data = {
2213 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002214 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002215 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002216 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002217 .flags = RPC_TASK_ASYNC,
2218 };
Trond Myklebust95121352005-10-18 14:20:12 -07002219 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002221 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002223 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002224 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002225 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002227 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002228 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002230 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002231 if (calldata->arg.seqid == NULL)
2232 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002233 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002234 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002235 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002236 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002237 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002238 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002239 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002240
Trond Myklebust1174dd12010-12-21 10:52:24 -05002241 msg.rpc_argp = &calldata->arg;
2242 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002243 task_setup_data.callback_data = calldata;
2244 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002245 if (IS_ERR(task))
2246 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002247 status = 0;
2248 if (wait)
2249 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002250 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002251 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002252out_free_calldata:
2253 kfree(calldata);
2254out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002255 if (roc)
2256 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002257 nfs4_put_open_state(state);
2258 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002259 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
Trond Myklebust2b484292010-09-17 10:56:51 -04002262static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002263nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 struct nfs4_state *state;
2266
Trond Myklebust565277f2007-10-15 18:17:53 -04002267 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002268 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2269 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002270 if (IS_ERR(state))
2271 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002272 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002273 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
Trond Myklebust1185a552009-12-03 15:54:02 -05002276static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002277{
2278 if (ctx->state == NULL)
2279 return;
2280 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002281 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002282 else
Al Viro643168c2011-06-22 18:20:23 -04002283 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002284}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2287{
Benny Halevy43652ad2009-04-01 09:21:54 -04002288 struct nfs4_server_caps_arg args = {
2289 .fhandle = fhandle,
2290 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 struct nfs4_server_caps_res res = {};
2292 struct rpc_message msg = {
2293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002294 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 .rpc_resp = &res,
2296 };
2297 int status;
2298
Bryan Schumaker7c513052011-03-24 17:12:24 +00002299 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (status == 0) {
2301 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002302 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2303 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2304 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2305 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2306 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2308 server->caps |= NFS_CAP_ACLS;
2309 if (res.has_links != 0)
2310 server->caps |= NFS_CAP_HARDLINKS;
2311 if (res.has_symlinks != 0)
2312 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002313 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2314 server->caps |= NFS_CAP_FILEID;
2315 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2316 server->caps |= NFS_CAP_MODE;
2317 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2318 server->caps |= NFS_CAP_NLINK;
2319 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2320 server->caps |= NFS_CAP_OWNER;
2321 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2322 server->caps |= NFS_CAP_OWNER_GROUP;
2323 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2324 server->caps |= NFS_CAP_ATIME;
2325 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2326 server->caps |= NFS_CAP_CTIME;
2327 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2328 server->caps |= NFS_CAP_MTIME;
2329
Trond Myklebusta65318b2009-03-11 14:10:28 -04002330 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2331 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2332 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002334 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return status;
2338}
2339
Trond Myklebust55a97592006-06-09 09:34:19 -04002340int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 struct nfs4_exception exception = { };
2343 int err;
2344 do {
2345 err = nfs4_handle_exception(server,
2346 _nfs4_server_capabilities(server, fhandle),
2347 &exception);
2348 } while (exception.retry);
2349 return err;
2350}
2351
2352static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2353 struct nfs_fsinfo *info)
2354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 struct nfs4_lookup_root_arg args = {
2356 .bitmask = nfs4_fattr_bitmap,
2357 };
2358 struct nfs4_lookup_res res = {
2359 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002360 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 .fh = fhandle,
2362 };
2363 struct rpc_message msg = {
2364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2365 .rpc_argp = &args,
2366 .rpc_resp = &res,
2367 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002368
Trond Myklebust0e574af2005-10-27 22:12:38 -04002369 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002370 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
2373static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2374 struct nfs_fsinfo *info)
2375{
2376 struct nfs4_exception exception = { };
2377 int err;
2378 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002379 err = _nfs4_lookup_root(server, fhandle, info);
2380 switch (err) {
2381 case 0:
2382 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002383 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002384 default:
2385 err = nfs4_handle_exception(server, err, &exception);
2386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002388out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return err;
2390}
2391
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002392static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2393 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2394{
2395 struct rpc_auth *auth;
2396 int ret;
2397
2398 auth = rpcauth_create(flavor, server->client);
2399 if (!auth) {
2400 ret = -EIO;
2401 goto out;
2402 }
2403 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002404out:
2405 return ret;
2406}
2407
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002408static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002409 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002411 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002412 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002414 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2415 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002416
2417 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002418 /* AUTH_UNIX is the default flavor if none was specified,
2419 * thus has already been tried. */
2420 if (flav_array[i] == RPC_AUTH_UNIX)
2421 continue;
2422
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002423 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002424 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002425 continue;
2426 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002427 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002428 /*
2429 * -EACCESS could mean that the user doesn't have correct permissions
2430 * to access the mount. It could also mean that we tried to mount
2431 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2432 * existing mount programs don't handle -EACCES very well so it should
2433 * be mapped to -EPERM instead.
2434 */
2435 if (status == -EACCES)
2436 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002437 return status;
2438}
2439
2440/*
2441 * get the file handle for the "/" directory on the server
2442 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002443int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2444 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002445{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002446 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002447 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002448 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2449 /*
2450 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2451 * by nfs4_map_errors() as this function exits.
2452 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002453 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (status == 0)
2455 status = nfs4_server_capabilities(server, fhandle);
2456 if (status == 0)
2457 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002458 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
Bryan Schumakerbae36242012-05-10 15:07:31 -04002461static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2462 struct nfs_fsinfo *info)
2463{
2464 int error;
2465 struct nfs_fattr *fattr = info->fattr;
2466
2467 error = nfs4_server_capabilities(server, mntfh);
2468 if (error < 0) {
2469 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2470 return error;
2471 }
2472
2473 error = nfs4_proc_getattr(server, mntfh, fattr);
2474 if (error < 0) {
2475 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2476 return error;
2477 }
2478
2479 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2480 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2481 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2482
2483 return error;
2484}
2485
Manoj Naik6b97fd32006-06-09 09:34:29 -04002486/*
2487 * Get locations and (maybe) other attributes of a referral.
2488 * Note that we'll actually follow the referral later when
2489 * we detect fsid mismatch in inode revalidation
2490 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002491static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2492 const struct qstr *name, struct nfs_fattr *fattr,
2493 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002494{
2495 int status = -ENOMEM;
2496 struct page *page = NULL;
2497 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002498
2499 page = alloc_page(GFP_KERNEL);
2500 if (page == NULL)
2501 goto out;
2502 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2503 if (locations == NULL)
2504 goto out;
2505
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002506 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002507 if (status != 0)
2508 goto out;
2509 /* Make sure server returned a different fsid for the referral */
2510 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002511 dprintk("%s: server did not return a different fsid for"
2512 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002513 status = -EIO;
2514 goto out;
2515 }
Andy Adamson533eb462011-06-13 18:25:56 -04002516 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2517 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002518
Andy Adamson533eb462011-06-13 18:25:56 -04002519 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002520 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002521 memset(fhandle, 0, sizeof(struct nfs_fh));
2522out:
2523 if (page)
2524 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002525 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002526 return status;
2527}
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2530{
2531 struct nfs4_getattr_arg args = {
2532 .fh = fhandle,
2533 .bitmask = server->attr_bitmask,
2534 };
2535 struct nfs4_getattr_res res = {
2536 .fattr = fattr,
2537 .server = server,
2538 };
2539 struct rpc_message msg = {
2540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2541 .rpc_argp = &args,
2542 .rpc_resp = &res,
2543 };
2544
Trond Myklebust0e574af2005-10-27 22:12:38 -04002545 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002546 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547}
2548
2549static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2550{
2551 struct nfs4_exception exception = { };
2552 int err;
2553 do {
2554 err = nfs4_handle_exception(server,
2555 _nfs4_proc_getattr(server, fhandle, fattr),
2556 &exception);
2557 } while (exception.retry);
2558 return err;
2559}
2560
2561/*
2562 * The file is not closed if it is opened due to the a request to change
2563 * the size of the file. The open call will not be needed once the
2564 * VFS layer lookup-intents are implemented.
2565 *
2566 * Close is called when the inode is destroyed.
2567 * If we haven't opened the file for O_WRONLY, we
2568 * need to in the size_change case to obtain a stateid.
2569 *
2570 * Got race?
2571 * Because OPEN is always done by name in nfsv4, it is
2572 * possible that we opened a different file by the same
2573 * name. We can recognize this race condition, but we
2574 * can't do anything about it besides returning an error.
2575 *
2576 * This will be fixed with VFS changes (lookup-intent).
2577 */
2578static int
2579nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2580 struct iattr *sattr)
2581{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002582 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002583 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002584 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 int status;
2586
Benny Halevy8a1636c2010-07-14 15:43:57 -04002587 if (pnfs_ld_layoutret_on_setattr(inode))
2588 pnfs_return_layout(inode);
2589
Trond Myklebust0e574af2005-10-27 22:12:38 -04002590 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Andy Adamson26699402012-05-30 16:12:24 -04002592 /* Deal with open(O_TRUNC) */
2593 if (sattr->ia_valid & ATTR_OPEN)
2594 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2595
2596 /* Optimization: if the end result is no change, don't RPC */
2597 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2598 return 0;
2599
Trond Myklebustd5308382005-11-04 15:33:38 -05002600 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002601 if (sattr->ia_valid & ATTR_FILE) {
2602 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002603
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002604 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002605 if (ctx) {
2606 cred = ctx->cred;
2607 state = ctx->state;
2608 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002609 }
2610
2611 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002612 if (status == 0)
2613 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return status;
2615}
2616
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002617static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2618 const struct qstr *name, struct nfs_fh *fhandle,
2619 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002620{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002621 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002622 int status;
2623 struct nfs4_lookup_arg args = {
2624 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002625 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002626 .name = name,
2627 };
2628 struct nfs4_lookup_res res = {
2629 .server = server,
2630 .fattr = fattr,
2631 .fh = fhandle,
2632 };
2633 struct rpc_message msg = {
2634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2635 .rpc_argp = &args,
2636 .rpc_resp = &res,
2637 };
2638
2639 nfs_fattr_init(fattr);
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002642 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 dprintk("NFS reply lookup: %d\n", status);
2644 return status;
2645}
2646
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002647static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002648{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002649 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002650 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002651 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2652 fattr->nlink = 2;
2653}
2654
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002655static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2656 struct qstr *name, struct nfs_fh *fhandle,
2657 struct nfs_fattr *fattr)
2658{
2659 struct nfs4_exception exception = { };
2660 struct rpc_clnt *client = *clnt;
2661 int err;
2662 do {
2663 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2664 switch (err) {
2665 case -NFS4ERR_BADNAME:
2666 err = -ENOENT;
2667 goto out;
2668 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002669 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002670 goto out;
2671 case -NFS4ERR_WRONGSEC:
2672 err = -EPERM;
2673 if (client != *clnt)
2674 goto out;
2675
2676 client = nfs4_create_sec_client(client, dir, name);
2677 if (IS_ERR(client))
2678 return PTR_ERR(client);
2679
2680 exception.retry = 1;
2681 break;
2682 default:
2683 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2684 }
2685 } while (exception.retry);
2686
2687out:
2688 if (err == 0)
2689 *clnt = client;
2690 else if (client != *clnt)
2691 rpc_shutdown_client(client);
2692
2693 return err;
2694}
2695
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002696static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002697 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002699 int status;
2700 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002701
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002702 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2703 if (client != NFS_CLIENT(dir)) {
2704 rpc_shutdown_client(client);
2705 nfs_fixup_secinfo_attributes(fattr);
2706 }
2707 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002710struct rpc_clnt *
2711nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2712 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2713{
2714 int status;
2715 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2716
2717 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2718 if (status < 0) {
2719 rpc_shutdown_client(client);
2720 return ERR_PTR(status);
2721 }
2722 return client;
2723}
2724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2726{
Trond Myklebust76b32992007-08-10 17:45:11 -04002727 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 struct nfs4_accessargs args = {
2729 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002730 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002732 struct nfs4_accessres res = {
2733 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002734 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 struct rpc_message msg = {
2736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2737 .rpc_argp = &args,
2738 .rpc_resp = &res,
2739 .rpc_cred = entry->cred,
2740 };
2741 int mode = entry->mask;
2742 int status;
2743
2744 /*
2745 * Determine which access bits we want to ask for...
2746 */
2747 if (mode & MAY_READ)
2748 args.access |= NFS4_ACCESS_READ;
2749 if (S_ISDIR(inode->i_mode)) {
2750 if (mode & MAY_WRITE)
2751 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2752 if (mode & MAY_EXEC)
2753 args.access |= NFS4_ACCESS_LOOKUP;
2754 } else {
2755 if (mode & MAY_WRITE)
2756 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2757 if (mode & MAY_EXEC)
2758 args.access |= NFS4_ACCESS_EXECUTE;
2759 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002760
2761 res.fattr = nfs_alloc_fattr();
2762 if (res.fattr == NULL)
2763 return -ENOMEM;
2764
Bryan Schumaker7c513052011-03-24 17:12:24 +00002765 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (!status) {
2767 entry->mask = 0;
2768 if (res.access & NFS4_ACCESS_READ)
2769 entry->mask |= MAY_READ;
2770 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2771 entry->mask |= MAY_WRITE;
2772 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2773 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002774 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002776 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 return status;
2778}
2779
2780static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2781{
2782 struct nfs4_exception exception = { };
2783 int err;
2784 do {
2785 err = nfs4_handle_exception(NFS_SERVER(inode),
2786 _nfs4_proc_access(inode, entry),
2787 &exception);
2788 } while (exception.retry);
2789 return err;
2790}
2791
2792/*
2793 * TODO: For the time being, we don't try to get any attributes
2794 * along with any of the zero-copy operations READ, READDIR,
2795 * READLINK, WRITE.
2796 *
2797 * In the case of the first three, we want to put the GETATTR
2798 * after the read-type operation -- this is because it is hard
2799 * to predict the length of a GETATTR response in v4, and thus
2800 * align the READ data correctly. This means that the GETATTR
2801 * may end up partially falling into the page cache, and we should
2802 * shift it into the 'tail' of the xdr_buf before processing.
2803 * To do this efficiently, we need to know the total length
2804 * of data received, which doesn't seem to be available outside
2805 * of the RPC layer.
2806 *
2807 * In the case of WRITE, we also want to put the GETATTR after
2808 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002809 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 *
2811 * Both of these changes to the XDR layer would in fact be quite
2812 * minor, but I decided to leave them for a subsequent patch.
2813 */
2814static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2815 unsigned int pgbase, unsigned int pglen)
2816{
2817 struct nfs4_readlink args = {
2818 .fh = NFS_FH(inode),
2819 .pgbase = pgbase,
2820 .pglen = pglen,
2821 .pages = &page,
2822 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002823 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 struct rpc_message msg = {
2825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2826 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002827 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 };
2829
Bryan Schumaker7c513052011-03-24 17:12:24 +00002830 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 -07002831}
2832
2833static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2834 unsigned int pgbase, unsigned int pglen)
2835{
2836 struct nfs4_exception exception = { };
2837 int err;
2838 do {
2839 err = nfs4_handle_exception(NFS_SERVER(inode),
2840 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2841 &exception);
2842 } while (exception.retry);
2843 return err;
2844}
2845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002847 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849static int
2850nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002851 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002853 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 int status = 0;
2856
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002857 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2858 if (IS_ERR(ctx))
2859 return PTR_ERR(ctx);
2860
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002861 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002862 state = nfs4_do_open(dir, dentry, ctx->mode,
2863 flags, sattr, ctx->cred,
2864 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002865 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (IS_ERR(state)) {
2867 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002868 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002870 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002871 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002872 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002874 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return status;
2876}
2877
2878static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2879{
Trond Myklebust16e42952005-10-27 22:12:44 -04002880 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002881 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002883 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002885 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002886 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002887 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2890 .rpc_argp = &args,
2891 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002893 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002894
Bryan Schumaker7c513052011-03-24 17:12:24 +00002895 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002896 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002897 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 return status;
2899}
2900
2901static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2902{
2903 struct nfs4_exception exception = { };
2904 int err;
2905 do {
2906 err = nfs4_handle_exception(NFS_SERVER(dir),
2907 _nfs4_proc_remove(dir, name),
2908 &exception);
2909 } while (exception.retry);
2910 return err;
2911}
2912
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002913static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002915 struct nfs_server *server = NFS_SERVER(dir);
2916 struct nfs_removeargs *args = msg->rpc_argp;
2917 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002919 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002921 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002924static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2925{
2926 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2927 &data->args.seq_args,
2928 &data->res.seq_res,
2929 task))
2930 return;
2931 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002934static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002936 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2937
Trond Myklebust14516c32010-07-31 14:29:06 -04002938 if (!nfs4_sequence_done(task, &res->seq_res))
2939 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002940 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002941 return 0;
2942 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002943 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
2945
Jeff Laytond3d41522010-09-17 17:31:57 -04002946static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2947{
2948 struct nfs_server *server = NFS_SERVER(dir);
2949 struct nfs_renameargs *arg = msg->rpc_argp;
2950 struct nfs_renameres *res = msg->rpc_resp;
2951
2952 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002953 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002954 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002955}
2956
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002957static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2958{
2959 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2960 &data->args.seq_args,
2961 &data->res.seq_res,
2962 task))
2963 return;
2964 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002965}
2966
2967static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2968 struct inode *new_dir)
2969{
2970 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2971
2972 if (!nfs4_sequence_done(task, &res->seq_res))
2973 return 0;
2974 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2975 return 0;
2976
2977 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002978 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002979 return 1;
2980}
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2983 struct inode *new_dir, struct qstr *new_name)
2984{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002985 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002986 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 .old_dir = NFS_FH(old_dir),
2988 .new_dir = NFS_FH(new_dir),
2989 .old_name = old_name,
2990 .new_name = new_name,
2991 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002992 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002993 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002994 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 struct rpc_message msg = {
2996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2997 .rpc_argp = &arg,
2998 .rpc_resp = &res,
2999 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003000 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Bryan Schumaker7c513052011-03-24 17:12:24 +00003002 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 if (!status) {
3004 update_changeattr(old_dir, &res.old_cinfo);
3005 update_changeattr(new_dir, &res.new_cinfo);
3006 }
3007 return status;
3008}
3009
3010static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3011 struct inode *new_dir, struct qstr *new_name)
3012{
3013 struct nfs4_exception exception = { };
3014 int err;
3015 do {
3016 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3017 _nfs4_proc_rename(old_dir, old_name,
3018 new_dir, new_name),
3019 &exception);
3020 } while (exception.retry);
3021 return err;
3022}
3023
3024static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3025{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003026 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 struct nfs4_link_arg arg = {
3028 .fh = NFS_FH(inode),
3029 .dir_fh = NFS_FH(dir),
3030 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003031 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003033 struct nfs4_link_res res = {
3034 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003035 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 struct rpc_message msg = {
3037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3038 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003039 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003041 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Trond Myklebust136f2622010-04-16 16:22:49 -04003043 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003044 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003045 goto out;
3046
Bryan Schumaker7c513052011-03-24 17:12:24 +00003047 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003048 if (!status) {
3049 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003050 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003051 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003052out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003053 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return status;
3055}
3056
3057static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3058{
3059 struct nfs4_exception exception = { };
3060 int err;
3061 do {
3062 err = nfs4_handle_exception(NFS_SERVER(inode),
3063 _nfs4_proc_link(inode, dir, name),
3064 &exception);
3065 } while (exception.retry);
3066 return err;
3067}
3068
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003069struct nfs4_createdata {
3070 struct rpc_message msg;
3071 struct nfs4_create_arg arg;
3072 struct nfs4_create_res res;
3073 struct nfs_fh fh;
3074 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003075};
3076
3077static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3078 struct qstr *name, struct iattr *sattr, u32 ftype)
3079{
3080 struct nfs4_createdata *data;
3081
3082 data = kzalloc(sizeof(*data), GFP_KERNEL);
3083 if (data != NULL) {
3084 struct nfs_server *server = NFS_SERVER(dir);
3085
3086 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3087 data->msg.rpc_argp = &data->arg;
3088 data->msg.rpc_resp = &data->res;
3089 data->arg.dir_fh = NFS_FH(dir);
3090 data->arg.server = server;
3091 data->arg.name = name;
3092 data->arg.attrs = sattr;
3093 data->arg.ftype = ftype;
3094 data->arg.bitmask = server->attr_bitmask;
3095 data->res.server = server;
3096 data->res.fh = &data->fh;
3097 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003098 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003099 }
3100 return data;
3101}
3102
3103static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3104{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003105 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003106 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003107 if (status == 0) {
3108 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003109 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3110 }
3111 return status;
3112}
3113
3114static void nfs4_free_createdata(struct nfs4_createdata *data)
3115{
3116 kfree(data);
3117}
3118
Chuck Lever4f390c12006-08-22 20:06:22 -04003119static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003120 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003122 struct nfs4_createdata *data;
3123 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Chuck Lever94a6d752006-08-22 20:06:23 -04003125 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003126 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003127
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003128 status = -ENOMEM;
3129 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3130 if (data == NULL)
3131 goto out;
3132
3133 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3134 data->arg.u.symlink.pages = &page;
3135 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003137 status = nfs4_do_create(dir, dentry, data);
3138
3139 nfs4_free_createdata(data);
3140out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return status;
3142}
3143
Chuck Lever4f390c12006-08-22 20:06:22 -04003144static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003145 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
3147 struct nfs4_exception exception = { };
3148 int err;
3149 do {
3150 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003151 _nfs4_proc_symlink(dir, dentry, page,
3152 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 &exception);
3154 } while (exception.retry);
3155 return err;
3156}
3157
3158static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3159 struct iattr *sattr)
3160{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003161 struct nfs4_createdata *data;
3162 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003164 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3165 if (data == NULL)
3166 goto out;
3167
3168 status = nfs4_do_create(dir, dentry, data);
3169
3170 nfs4_free_createdata(data);
3171out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return status;
3173}
3174
3175static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3176 struct iattr *sattr)
3177{
3178 struct nfs4_exception exception = { };
3179 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003180
3181 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 do {
3183 err = nfs4_handle_exception(NFS_SERVER(dir),
3184 _nfs4_proc_mkdir(dir, dentry, sattr),
3185 &exception);
3186 } while (exception.retry);
3187 return err;
3188}
3189
3190static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003191 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
3193 struct inode *dir = dentry->d_inode;
3194 struct nfs4_readdir_arg args = {
3195 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003196 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 .pgbase = 0,
3198 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003199 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003200 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 };
3202 struct nfs4_readdir_res res;
3203 struct rpc_message msg = {
3204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3205 .rpc_argp = &args,
3206 .rpc_resp = &res,
3207 .rpc_cred = cred,
3208 };
3209 int status;
3210
Harvey Harrison3110ff82008-05-02 13:42:44 -07003211 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003212 dentry->d_parent->d_name.name,
3213 dentry->d_name.name,
3214 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3216 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003217 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 -05003218 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003220 status += args.pgbase;
3221 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003222
3223 nfs_invalidate_atime(dir);
3224
Harvey Harrison3110ff82008-05-02 13:42:44 -07003225 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return status;
3227}
3228
3229static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003230 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
3232 struct nfs4_exception exception = { };
3233 int err;
3234 do {
3235 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3236 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003237 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 &exception);
3239 } while (exception.retry);
3240 return err;
3241}
3242
3243static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3244 struct iattr *sattr, dev_t rdev)
3245{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003246 struct nfs4_createdata *data;
3247 int mode = sattr->ia_mode;
3248 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
3250 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3251 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003252
3253 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3254 if (data == NULL)
3255 goto out;
3256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003258 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003260 data->arg.ftype = NF4BLK;
3261 data->arg.u.device.specdata1 = MAJOR(rdev);
3262 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 }
3264 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003265 data->arg.ftype = NF4CHR;
3266 data->arg.u.device.specdata1 = MAJOR(rdev);
3267 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003270 status = nfs4_do_create(dir, dentry, data);
3271
3272 nfs4_free_createdata(data);
3273out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 return status;
3275}
3276
3277static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3278 struct iattr *sattr, dev_t rdev)
3279{
3280 struct nfs4_exception exception = { };
3281 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003282
3283 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 do {
3285 err = nfs4_handle_exception(NFS_SERVER(dir),
3286 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3287 &exception);
3288 } while (exception.retry);
3289 return err;
3290}
3291
3292static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3293 struct nfs_fsstat *fsstat)
3294{
3295 struct nfs4_statfs_arg args = {
3296 .fh = fhandle,
3297 .bitmask = server->attr_bitmask,
3298 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003299 struct nfs4_statfs_res res = {
3300 .fsstat = fsstat,
3301 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 struct rpc_message msg = {
3303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3304 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003305 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 };
3307
Trond Myklebust0e574af2005-10-27 22:12:38 -04003308 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003309 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
3312static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3313{
3314 struct nfs4_exception exception = { };
3315 int err;
3316 do {
3317 err = nfs4_handle_exception(server,
3318 _nfs4_proc_statfs(server, fhandle, fsstat),
3319 &exception);
3320 } while (exception.retry);
3321 return err;
3322}
3323
3324static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3325 struct nfs_fsinfo *fsinfo)
3326{
3327 struct nfs4_fsinfo_arg args = {
3328 .fh = fhandle,
3329 .bitmask = server->attr_bitmask,
3330 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003331 struct nfs4_fsinfo_res res = {
3332 .fsinfo = fsinfo,
3333 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 struct rpc_message msg = {
3335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3336 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003337 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 };
3339
Bryan Schumaker7c513052011-03-24 17:12:24 +00003340 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
3343static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3344{
3345 struct nfs4_exception exception = { };
3346 int err;
3347
3348 do {
3349 err = nfs4_handle_exception(server,
3350 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3351 &exception);
3352 } while (exception.retry);
3353 return err;
3354}
3355
3356static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3357{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003358 int error;
3359
Trond Myklebust0e574af2005-10-27 22:12:38 -04003360 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003361 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3362 if (error == 0)
3363 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3364
3365 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
3368static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3369 struct nfs_pathconf *pathconf)
3370{
3371 struct nfs4_pathconf_arg args = {
3372 .fh = fhandle,
3373 .bitmask = server->attr_bitmask,
3374 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003375 struct nfs4_pathconf_res res = {
3376 .pathconf = pathconf,
3377 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 struct rpc_message msg = {
3379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3380 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003381 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 };
3383
3384 /* None of the pathconf attributes are mandatory to implement */
3385 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3386 memset(pathconf, 0, sizeof(*pathconf));
3387 return 0;
3388 }
3389
Trond Myklebust0e574af2005-10-27 22:12:38 -04003390 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003391 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
3393
3394static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3395 struct nfs_pathconf *pathconf)
3396{
3397 struct nfs4_exception exception = { };
3398 int err;
3399
3400 do {
3401 err = nfs4_handle_exception(server,
3402 _nfs4_proc_pathconf(server, fhandle, pathconf),
3403 &exception);
3404 } while (exception.retry);
3405 return err;
3406}
3407
Benny Halevyd20581a2011-05-22 19:52:03 +03003408void __nfs4_read_done_cb(struct nfs_read_data *data)
3409{
Fred Isamancd841602012-04-20 14:47:44 -04003410 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003411}
3412
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003413static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Fred Isamancd841602012-04-20 14:47:44 -04003415 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003417 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003418 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003419 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003421
Benny Halevyd20581a2011-05-22 19:52:03 +03003422 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003424 renew_lease(server, data->timestamp);
3425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003428static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3429{
3430
3431 dprintk("--> %s\n", __func__);
3432
3433 if (!nfs4_sequence_done(task, &data->res.seq_res))
3434 return -EAGAIN;
3435
Benny Halevyd20581a2011-05-22 19:52:03 +03003436 return data->read_done_cb ? data->read_done_cb(task, data) :
3437 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003438}
3439
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003440static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003443 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003444 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003445 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446}
3447
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003448static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3449{
Fred Isamancd841602012-04-20 14:47:44 -04003450 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003451 &data->args.seq_args,
3452 &data->res.seq_res,
3453 task))
3454 return;
3455 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456}
3457
Fred Isamanb029bc92011-03-03 15:13:42 +00003458static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
Fred Isamancd841602012-04-20 14:47:44 -04003460 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003462 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003463 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003464 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003466 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003468 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003469 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
Fred Isamanb029bc92011-03-03 15:13:42 +00003473static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3474{
3475 if (!nfs4_sequence_done(task, &data->res.seq_res))
3476 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003477 return data->write_done_cb ? data->write_done_cb(task, data) :
3478 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003479}
3480
Trond Myklebust5a37f852012-04-28 14:55:16 -04003481static
3482bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003483{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003484 const struct nfs_pgio_header *hdr = data->header;
3485
3486 /* Don't request attributes for pNFS or O_DIRECT writes */
3487 if (data->ds_clp != NULL || hdr->dreq != NULL)
3488 return false;
3489 /* Otherwise, request attributes if and only if we don't hold
3490 * a delegation
3491 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003492 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003493}
Fred Isamana69aef12011-03-03 15:13:47 +00003494
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003495static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
Fred Isamancd841602012-04-20 14:47:44 -04003497 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003498
Trond Myklebust5a37f852012-04-28 14:55:16 -04003499 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003500 data->args.bitmask = NULL;
3501 data->res.fattr = NULL;
3502 } else
3503 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003504
Fred Isamanb029bc92011-03-03 15:13:42 +00003505 if (!data->write_done_cb)
3506 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003507 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 data->timestamp = jiffies;
3509
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003510 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003511 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003514static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3515{
Fred Isamancd841602012-04-20 14:47:44 -04003516 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003517 &data->args.seq_args,
3518 &data->res.seq_res,
3519 task))
3520 return;
3521 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522}
3523
Fred Isaman0b7c0152012-04-20 14:47:39 -04003524static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3525{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3527 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003528 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return;
3531 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003532}
3533
Fred Isaman0b7c0152012-04-20 14:47:39 -04003534static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
3536 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003537
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003538 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003539 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003540 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543}
3544
Fred Isaman0b7c0152012-04-20 14:47:39 -04003545static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003546{
3547 if (!nfs4_sequence_done(task, &data->res.seq_res))
3548 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003549 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003550}
3551
Fred Isaman0b7c0152012-04-20 14:47:39 -04003552static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003554 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003555
Fred Isaman0b7c0152012-04-20 14:47:39 -04003556 if (data->commit_done_cb == NULL)
3557 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003558 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003559 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003560 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003563struct nfs4_renewdata {
3564 struct nfs_client *client;
3565 unsigned long timestamp;
3566};
3567
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568/*
3569 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3570 * standalone procedure for queueing an asynchronous RENEW.
3571 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003572static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003573{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003574 struct nfs4_renewdata *data = calldata;
3575 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003576
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003577 if (atomic_read(&clp->cl_count) > 1)
3578 nfs4_schedule_state_renewal(clp);
3579 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003580 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003581}
3582
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003583static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003585 struct nfs4_renewdata *data = calldata;
3586 struct nfs_client *clp = data->client;
3587 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
3589 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003590 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003591 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3592 return;
3593 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003594 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003595 return;
3596 }
3597 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003599 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600}
3601
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003602static const struct rpc_call_ops nfs4_renew_ops = {
3603 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003604 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003605};
3606
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003607static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
3609 struct rpc_message msg = {
3610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3611 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003612 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003614 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003616 if (renew_flags == 0)
3617 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003618 if (!atomic_inc_not_zero(&clp->cl_count))
3619 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003620 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003621 if (data == NULL)
3622 return -ENOMEM;
3623 data->client = clp;
3624 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003626 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627}
3628
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003629static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
3631 struct rpc_message msg = {
3632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3633 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003634 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 };
3636 unsigned long now = jiffies;
3637 int status;
3638
3639 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3640 if (status < 0)
3641 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003642 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 return 0;
3644}
3645
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003646static inline int nfs4_server_supports_acls(struct nfs_server *server)
3647{
3648 return (server->caps & NFS_CAP_ACLS)
3649 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3650 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3651}
3652
3653/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3654 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3655 * the stack.
3656 */
3657#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3658
Neil Hormane9e3d722011-03-04 19:26:03 -05003659static int buf_to_pages_noslab(const void *buf, size_t buflen,
3660 struct page **pages, unsigned int *pgbase)
3661{
3662 struct page *newpage, **spages;
3663 int rc = 0;
3664 size_t len;
3665 spages = pages;
3666
3667 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003668 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003669 newpage = alloc_page(GFP_KERNEL);
3670
3671 if (newpage == NULL)
3672 goto unwind;
3673 memcpy(page_address(newpage), buf, len);
3674 buf += len;
3675 buflen -= len;
3676 *pages++ = newpage;
3677 rc++;
3678 } while (buflen != 0);
3679
3680 return rc;
3681
3682unwind:
3683 for(; rc > 0; rc--)
3684 __free_page(spages[rc-1]);
3685 return -ENOMEM;
3686}
3687
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003688struct nfs4_cached_acl {
3689 int cached;
3690 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003691 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003692};
3693
3694static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003695{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003696 struct nfs_inode *nfsi = NFS_I(inode);
3697
3698 spin_lock(&inode->i_lock);
3699 kfree(nfsi->nfs4_acl);
3700 nfsi->nfs4_acl = acl;
3701 spin_unlock(&inode->i_lock);
3702}
3703
3704static void nfs4_zap_acl_attr(struct inode *inode)
3705{
3706 nfs4_set_cached_acl(inode, NULL);
3707}
3708
3709static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3710{
3711 struct nfs_inode *nfsi = NFS_I(inode);
3712 struct nfs4_cached_acl *acl;
3713 int ret = -ENOENT;
3714
3715 spin_lock(&inode->i_lock);
3716 acl = nfsi->nfs4_acl;
3717 if (acl == NULL)
3718 goto out;
3719 if (buf == NULL) /* user is just asking for length */
3720 goto out_len;
3721 if (acl->cached == 0)
3722 goto out;
3723 ret = -ERANGE; /* see getxattr(2) man page */
3724 if (acl->len > buflen)
3725 goto out;
3726 memcpy(buf, acl->data, acl->len);
3727out_len:
3728 ret = acl->len;
3729out:
3730 spin_unlock(&inode->i_lock);
3731 return ret;
3732}
3733
Sachin Prabhu5794d212012-04-17 14:36:40 +01003734static 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 +00003735{
3736 struct nfs4_cached_acl *acl;
3737
Sachin Prabhu5794d212012-04-17 14:36:40 +01003738 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003739 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3740 if (acl == NULL)
3741 goto out;
3742 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003743 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003744 } else {
3745 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3746 if (acl == NULL)
3747 goto out;
3748 acl->cached = 0;
3749 }
3750 acl->len = acl_len;
3751out:
3752 nfs4_set_cached_acl(inode, acl);
3753}
3754
Andy Adamsonbf118a32011-12-07 11:55:27 -05003755/*
3756 * The getxattr API returns the required buffer length when called with a
3757 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3758 * the required buf. On a NULL buf, we send a page of data to the server
3759 * guessing that the ACL request can be serviced by a page. If so, we cache
3760 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3761 * the cache. If not so, we throw away the page, and cache the required
3762 * length. The next getxattr call will then produce another round trip to
3763 * the server, this time with the input buf of the required size.
3764 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003765static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003766{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003767 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003768 struct nfs_getaclargs args = {
3769 .fh = NFS_FH(inode),
3770 .acl_pages = pages,
3771 .acl_len = buflen,
3772 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003773 struct nfs_getaclres res = {
3774 .acl_len = buflen,
3775 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003776 struct rpc_message msg = {
3777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3778 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003779 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003780 };
Chuck Lever56d08fe2012-07-11 16:30:32 -04003781 int ret = -ENOMEM, npages, i;
3782 size_t acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003783
Andy Adamsonbf118a32011-12-07 11:55:27 -05003784 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3785 /* As long as we're doing a round trip to the server anyway,
3786 * let's be prepared for a page of acl data. */
3787 if (npages == 0)
3788 npages = 1;
3789
Sachin Prabhu5a006892012-04-17 14:35:39 +01003790 /* Add an extra page to handle the bitmap returned */
3791 npages++;
3792
Andy Adamsonbf118a32011-12-07 11:55:27 -05003793 for (i = 0; i < npages; i++) {
3794 pages[i] = alloc_page(GFP_KERNEL);
3795 if (!pages[i])
3796 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003797 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003798
3799 /* for decoding across pages */
3800 res.acl_scratch = alloc_page(GFP_KERNEL);
3801 if (!res.acl_scratch)
3802 goto out_free;
3803
Andy Adamsonbf118a32011-12-07 11:55:27 -05003804 args.acl_len = npages * PAGE_SIZE;
3805 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003806
Andy Adamsonbf118a32011-12-07 11:55:27 -05003807 /* Let decode_getfacl know not to fail if the ACL data is larger than
3808 * the page we send as a guess */
3809 if (buf == NULL)
3810 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003811
Peng Taode040be2012-01-10 22:42:47 +08003812 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003813 __func__, buf, buflen, npages, args.acl_len);
3814 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3815 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003816 if (ret)
3817 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003818
3819 acl_len = res.acl_len - res.acl_data_offset;
3820 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003821 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003822 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003823 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003824 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003825 if (buf) {
3826 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003827 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003828 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003829 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003830 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003831 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003832 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003833out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003834 for (i = 0; i < npages; i++)
3835 if (pages[i])
3836 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003837 if (res.acl_scratch)
3838 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003839 return ret;
3840}
3841
Trond Myklebust16b42892006-08-24 12:27:15 -04003842static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3843{
3844 struct nfs4_exception exception = { };
3845 ssize_t ret;
3846 do {
3847 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3848 if (ret >= 0)
3849 break;
3850 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3851 } while (exception.retry);
3852 return ret;
3853}
3854
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003855static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3856{
3857 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003858 int ret;
3859
3860 if (!nfs4_server_supports_acls(server))
3861 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003862 ret = nfs_revalidate_inode(server, inode);
3863 if (ret < 0)
3864 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003865 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3866 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003867 ret = nfs4_read_cached_acl(inode, buf, buflen);
3868 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003869 /* -ENOENT is returned if there is no ACL or if there is an ACL
3870 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003871 return ret;
3872 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003873}
3874
Trond Myklebust16b42892006-08-24 12:27:15 -04003875static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003876{
3877 struct nfs_server *server = NFS_SERVER(inode);
3878 struct page *pages[NFS4ACL_MAXPAGES];
3879 struct nfs_setaclargs arg = {
3880 .fh = NFS_FH(inode),
3881 .acl_pages = pages,
3882 .acl_len = buflen,
3883 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003884 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003885 struct rpc_message msg = {
3886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3887 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003888 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003889 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003890 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003891
3892 if (!nfs4_server_supports_acls(server))
3893 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003894 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3895 if (i < 0)
3896 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003897 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003898 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003899
3900 /*
3901 * Free each page after tx, so the only ref left is
3902 * held by the network stack
3903 */
3904 for (; i > 0; i--)
3905 put_page(pages[i-1]);
3906
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003907 /*
3908 * Acl update can result in inode attribute update.
3909 * so mark the attribute cache invalid.
3910 */
3911 spin_lock(&inode->i_lock);
3912 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3913 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003914 nfs_access_zap_cache(inode);
3915 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003916 return ret;
3917}
3918
Trond Myklebust16b42892006-08-24 12:27:15 -04003919static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3920{
3921 struct nfs4_exception exception = { };
3922 int err;
3923 do {
3924 err = nfs4_handle_exception(NFS_SERVER(inode),
3925 __nfs4_proc_set_acl(inode, buf, buflen),
3926 &exception);
3927 } while (exception.retry);
3928 return err;
3929}
3930
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003932nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003934 struct nfs_client *clp = server->nfs_client;
3935
3936 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 return 0;
3938 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003939 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003940 case -NFS4ERR_ADMIN_REVOKED:
3941 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003942 if (state == NULL)
3943 break;
3944 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003945 case -NFS4ERR_OPENMODE:
3946 if (state == NULL)
3947 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003948 nfs4_schedule_stateid_recovery(server, state);
3949 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003950 case -NFS4ERR_EXPIRED:
3951 if (state != NULL)
3952 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003954 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003955 nfs4_schedule_lease_recovery(clp);
3956 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003957#if defined(CONFIG_NFS_V4_1)
3958 case -NFS4ERR_BADSESSION:
3959 case -NFS4ERR_BADSLOT:
3960 case -NFS4ERR_BAD_HIGH_SLOT:
3961 case -NFS4ERR_DEADSESSION:
3962 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3963 case -NFS4ERR_SEQ_FALSE_RETRY:
3964 case -NFS4ERR_SEQ_MISORDERED:
3965 dprintk("%s ERROR %d, Reset session\n", __func__,
3966 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003967 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003968 task->tk_status = 0;
3969 return -EAGAIN;
3970#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003972 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003973 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003974 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3976 task->tk_status = 0;
3977 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003978 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 case -NFS4ERR_OLD_STATEID:
3980 task->tk_status = 0;
3981 return -EAGAIN;
3982 }
3983 task->tk_status = nfs4_map_errors(task->tk_status);
3984 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003985wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003986 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003987 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3988 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3989 task->tk_status = 0;
3990 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
Chuck Leverf0920752012-05-21 22:45:41 -04003993static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3994 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003995{
3996 __be32 verf[2];
3997
Chuck Lever2c820d92012-05-21 22:45:33 -04003998 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3999 /* An impossible timestamp guarantees this value
4000 * will never match a generated boot time. */
4001 verf[0] = 0;
4002 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4003 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004004 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4005 verf[0] = (__be32)nn->boot_time.tv_sec;
4006 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004007 }
Chuck Levercd937102012-03-02 17:14:31 -05004008 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4009}
4010
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004011int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4012 unsigned short port, struct rpc_cred *cred,
4013 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
4015 nfs4_verifier sc_verifier;
4016 struct nfs4_setclientid setclientid = {
4017 .sc_verifier = &sc_verifier,
4018 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004019 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 };
4021 struct rpc_message msg = {
4022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4023 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004024 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004025 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 int loop = 0;
4028 int status;
4029
Chuck Leverf0920752012-05-21 22:45:41 -04004030 nfs4_init_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
4032 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05004033 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05004035 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004036 clp->cl_ipaddr,
4037 rpc_peeraddr2str(clp->cl_rpcclient,
4038 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05004039 rpc_peeraddr2str(clp->cl_rpcclient,
4040 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04004041 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 clp->cl_id_uniquifier);
4043 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004044 sizeof(setclientid.sc_netid),
4045 rpc_peeraddr2str(clp->cl_rpcclient,
4046 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004048 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05004050 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004052 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 if (status != -NFS4ERR_CLID_INUSE)
4054 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004055 if (loop != 0) {
4056 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004058 }
4059 ++loop;
4060 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 }
4062 return status;
4063}
4064
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004065int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004066 struct nfs4_setclientid_res *arg,
4067 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068{
4069 struct nfs_fsinfo fsinfo;
4070 struct rpc_message msg = {
4071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004072 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004074 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 };
4076 unsigned long now;
4077 int status;
4078
4079 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004080 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 if (status == 0) {
4082 spin_lock(&clp->cl_lock);
4083 clp->cl_lease_time = fsinfo.lease_time * HZ;
4084 clp->cl_last_renewal = now;
4085 spin_unlock(&clp->cl_lock);
4086 }
4087 return status;
4088}
4089
Trond Myklebustfe650402006-01-03 09:55:18 +01004090struct nfs4_delegreturndata {
4091 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004092 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004093 struct nfs_fh fh;
4094 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004095 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004096 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004097 int rpc_status;
4098};
4099
Trond Myklebustfe650402006-01-03 09:55:18 +01004100static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4101{
4102 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004103
Trond Myklebust14516c32010-07-31 14:29:06 -04004104 if (!nfs4_sequence_done(task, &data->res.seq_res))
4105 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004106
Ricardo Labiaga79708862009-12-07 09:23:21 -05004107 switch (task->tk_status) {
4108 case -NFS4ERR_STALE_STATEID:
4109 case -NFS4ERR_EXPIRED:
4110 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004111 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004112 break;
4113 default:
4114 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4115 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004116 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004117 return;
4118 }
4119 }
4120 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004121}
4122
4123static void nfs4_delegreturn_release(void *calldata)
4124{
Trond Myklebustfe650402006-01-03 09:55:18 +01004125 kfree(calldata);
4126}
4127
Andy Adamson938e1012009-04-01 09:22:28 -04004128#if defined(CONFIG_NFS_V4_1)
4129static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4130{
4131 struct nfs4_delegreturndata *d_data;
4132
4133 d_data = (struct nfs4_delegreturndata *)data;
4134
Trond Myklebust035168a2010-06-16 09:52:26 -04004135 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004136 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004137 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004138 return;
4139 rpc_call_start(task);
4140}
4141#endif /* CONFIG_NFS_V4_1 */
4142
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004143static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004144#if defined(CONFIG_NFS_V4_1)
4145 .rpc_call_prepare = nfs4_delegreturn_prepare,
4146#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004147 .rpc_call_done = nfs4_delegreturn_done,
4148 .rpc_release = nfs4_delegreturn_release,
4149};
4150
Trond Myklebuste6f81072008-01-24 18:14:34 -05004151static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004152{
4153 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004154 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004155 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004156 struct rpc_message msg = {
4157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4158 .rpc_cred = cred,
4159 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004160 struct rpc_task_setup task_setup_data = {
4161 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004162 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004163 .callback_ops = &nfs4_delegreturn_ops,
4164 .flags = RPC_TASK_ASYNC,
4165 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004166 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004167
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004168 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004169 if (data == NULL)
4170 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004171 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004172 data->args.fhandle = &data->fh;
4173 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004174 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004175 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004176 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004177 data->res.fattr = &data->fattr;
4178 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004179 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004180 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004181 data->rpc_status = 0;
4182
Trond Myklebustc970aa82007-07-14 15:39:59 -04004183 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004184 msg.rpc_argp = &data->args;
4185 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004186 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004187 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004188 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004189 if (!issync)
4190 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004191 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004192 if (status != 0)
4193 goto out;
4194 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004195 if (status == 0)
4196 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4197 else
4198 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004199out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004200 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004201 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202}
4203
Trond Myklebuste6f81072008-01-24 18:14:34 -05004204int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
4206 struct nfs_server *server = NFS_SERVER(inode);
4207 struct nfs4_exception exception = { };
4208 int err;
4209 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004210 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 switch (err) {
4212 case -NFS4ERR_STALE_STATEID:
4213 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 case 0:
4215 return 0;
4216 }
4217 err = nfs4_handle_exception(server, err, &exception);
4218 } while (exception.retry);
4219 return err;
4220}
4221
4222#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4223#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4224
4225/*
4226 * sleep, with exponential backoff, and retry the LOCK operation.
4227 */
4228static unsigned long
4229nfs4_set_lock_task_retry(unsigned long timeout)
4230{
Jeff Laytond3103102011-12-01 22:44:39 +01004231 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 timeout <<= 1;
4233 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4234 return NFS4_LOCK_MAXTIMEOUT;
4235 return timeout;
4236}
4237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4239{
4240 struct inode *inode = state->inode;
4241 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004242 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004243 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004245 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004247 struct nfs_lockt_res res = {
4248 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 };
4250 struct rpc_message msg = {
4251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4252 .rpc_argp = &arg,
4253 .rpc_resp = &res,
4254 .rpc_cred = state->owner->so_cred,
4255 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 struct nfs4_lock_state *lsp;
4257 int status;
4258
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004259 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004260 status = nfs4_set_lock_state(state, request);
4261 if (status != 0)
4262 goto out;
4263 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004264 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004265 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004266 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004267 switch (status) {
4268 case 0:
4269 request->fl_type = F_UNLCK;
4270 break;
4271 case -NFS4ERR_DENIED:
4272 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004274 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004275out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 return status;
4277}
4278
4279static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4280{
4281 struct nfs4_exception exception = { };
4282 int err;
4283
4284 do {
4285 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4286 _nfs4_proc_getlk(state, cmd, request),
4287 &exception);
4288 } while (exception.retry);
4289 return err;
4290}
4291
4292static int do_vfs_lock(struct file *file, struct file_lock *fl)
4293{
4294 int res = 0;
4295 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4296 case FL_POSIX:
4297 res = posix_lock_file_wait(file, fl);
4298 break;
4299 case FL_FLOCK:
4300 res = flock_lock_file_wait(file, fl);
4301 break;
4302 default:
4303 BUG();
4304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 return res;
4306}
4307
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004308struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004309 struct nfs_locku_args arg;
4310 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004311 struct nfs4_lock_state *lsp;
4312 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004313 struct file_lock fl;
4314 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004315 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004316};
4317
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004318static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4319 struct nfs_open_context *ctx,
4320 struct nfs4_lock_state *lsp,
4321 struct nfs_seqid *seqid)
4322{
4323 struct nfs4_unlockdata *p;
4324 struct inode *inode = lsp->ls_state->inode;
4325
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004326 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004327 if (p == NULL)
4328 return NULL;
4329 p->arg.fh = NFS_FH(inode);
4330 p->arg.fl = &p->fl;
4331 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004332 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004333 p->arg.stateid = &lsp->ls_stateid;
4334 p->lsp = lsp;
4335 atomic_inc(&lsp->ls_count);
4336 /* Ensure we don't close file until we're done freeing locks! */
4337 p->ctx = get_nfs_open_context(ctx);
4338 memcpy(&p->fl, fl, sizeof(p->fl));
4339 p->server = NFS_SERVER(inode);
4340 return p;
4341}
4342
Trond Myklebust06f814a2006-01-03 09:55:07 +01004343static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004344{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004345 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004346 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004347 nfs4_put_lock_state(calldata->lsp);
4348 put_nfs_open_context(calldata->ctx);
4349 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004350}
4351
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004352static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004353{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004354 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004355
Trond Myklebust14516c32010-07-31 14:29:06 -04004356 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4357 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004358 switch (task->tk_status) {
4359 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004360 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4361 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004362 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004363 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004364 case -NFS4ERR_BAD_STATEID:
4365 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004366 case -NFS4ERR_STALE_STATEID:
4367 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004368 break;
4369 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004370 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004371 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004372 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004373}
4374
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004375static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004376{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004377 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004379 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004380 return;
4381 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004382 /* Note: exit _without_ running nfs4_locku_done */
4383 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004384 return;
4385 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004386 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004387 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004388 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004389 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004390 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004391 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392}
4393
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004394static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004395 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004396 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004397 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004398};
4399
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004400static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4401 struct nfs_open_context *ctx,
4402 struct nfs4_lock_state *lsp,
4403 struct nfs_seqid *seqid)
4404{
4405 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004406 struct rpc_message msg = {
4407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4408 .rpc_cred = ctx->cred,
4409 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004410 struct rpc_task_setup task_setup_data = {
4411 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004412 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004413 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004414 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004415 .flags = RPC_TASK_ASYNC,
4416 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004417
Frank Filz137d6ac2007-07-09 15:32:29 -07004418 /* Ensure this is an unlock - when canceling a lock, the
4419 * canceled lock is passed in, and it won't be an unlock.
4420 */
4421 fl->fl_type = F_UNLCK;
4422
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004423 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4424 if (data == NULL) {
4425 nfs_free_seqid(seqid);
4426 return ERR_PTR(-ENOMEM);
4427 }
4428
Trond Myklebust9d12b212012-01-17 22:04:25 -05004429 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004430 msg.rpc_argp = &data->arg;
4431 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004432 task_setup_data.callback_data = data;
4433 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004434}
4435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4437{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004438 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004439 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004440 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004441 struct rpc_task *task;
4442 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004443 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Trond Myklebust9b073572006-06-29 16:38:34 -04004445 status = nfs4_set_lock_state(state, request);
4446 /* Unlock _before_ we do the RPC call */
4447 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004448 down_read(&nfsi->rwsem);
4449 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4450 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004451 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004452 }
4453 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004454 if (status != 0)
4455 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004456 /* Is this a delegated lock? */
4457 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004458 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004459 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004460 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004461 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004462 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004463 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004464 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004465 status = PTR_ERR(task);
4466 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004467 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004468 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004469 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004470out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004471 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004472 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473}
4474
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004475struct nfs4_lockdata {
4476 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004477 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004478 struct nfs4_lock_state *lsp;
4479 struct nfs_open_context *ctx;
4480 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004481 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004482 int rpc_status;
4483 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004484 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004485};
4486
4487static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004488 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4489 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004490{
4491 struct nfs4_lockdata *p;
4492 struct inode *inode = lsp->ls_state->inode;
4493 struct nfs_server *server = NFS_SERVER(inode);
4494
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004495 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004496 if (p == NULL)
4497 return NULL;
4498
4499 p->arg.fh = NFS_FH(inode);
4500 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004501 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004502 if (p->arg.open_seqid == NULL)
4503 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004504 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004505 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004506 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004507 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004508 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004509 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004510 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004511 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004512 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004513 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004514 atomic_inc(&lsp->ls_count);
4515 p->ctx = get_nfs_open_context(ctx);
4516 memcpy(&p->fl, fl, sizeof(p->fl));
4517 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004518out_free_seqid:
4519 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520out_free:
4521 kfree(p);
4522 return NULL;
4523}
4524
4525static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4526{
4527 struct nfs4_lockdata *data = calldata;
4528 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Harvey Harrison3110ff82008-05-02 13:42:44 -07004530 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004531 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4532 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533 /* Do we need to do an open_to_lock_owner? */
4534 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004535 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4536 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004537 data->arg.open_stateid = &state->stateid;
4538 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004539 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004540 } else
4541 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004542 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004543 if (nfs4_setup_sequence(data->server,
4544 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004545 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004546 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004547 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004548 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004549}
4550
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004551static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4552{
4553 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4554 nfs4_lock_prepare(task, calldata);
4555}
4556
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004557static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4558{
4559 struct nfs4_lockdata *data = calldata;
4560
Harvey Harrison3110ff82008-05-02 13:42:44 -07004561 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562
Trond Myklebust14516c32010-07-31 14:29:06 -04004563 if (!nfs4_sequence_done(task, &data->res.seq_res))
4564 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004565
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004566 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004568 if (data->rpc_status == 0)
4569 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4570 else
4571 goto out;
4572 }
4573 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004574 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004575 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004576 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004578out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004579 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004580}
4581
4582static void nfs4_lock_release(void *calldata)
4583{
4584 struct nfs4_lockdata *data = calldata;
4585
Harvey Harrison3110ff82008-05-02 13:42:44 -07004586 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004587 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004588 if (data->cancelled != 0) {
4589 struct rpc_task *task;
4590 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4591 data->arg.lock_seqid);
4592 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004593 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004594 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004595 } else
4596 nfs_free_seqid(data->arg.lock_seqid);
4597 nfs4_put_lock_state(data->lsp);
4598 put_nfs_open_context(data->ctx);
4599 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004600 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004601}
4602
4603static const struct rpc_call_ops nfs4_lock_ops = {
4604 .rpc_call_prepare = nfs4_lock_prepare,
4605 .rpc_call_done = nfs4_lock_done,
4606 .rpc_release = nfs4_lock_release,
4607};
4608
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004609static const struct rpc_call_ops nfs4_recover_lock_ops = {
4610 .rpc_call_prepare = nfs4_recover_lock_prepare,
4611 .rpc_call_done = nfs4_lock_done,
4612 .rpc_release = nfs4_lock_release,
4613};
4614
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004615static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4616{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004617 switch (error) {
4618 case -NFS4ERR_ADMIN_REVOKED:
4619 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004620 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004621 if (new_lock_owner != 0 ||
4622 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004623 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004624 break;
4625 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004626 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004627 case -NFS4ERR_EXPIRED:
4628 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004629 };
4630}
4631
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004632static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004633{
4634 struct nfs4_lockdata *data;
4635 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004636 struct rpc_message msg = {
4637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4638 .rpc_cred = state->owner->so_cred,
4639 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004640 struct rpc_task_setup task_setup_data = {
4641 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004642 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004643 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004644 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004645 .flags = RPC_TASK_ASYNC,
4646 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004647 int ret;
4648
Harvey Harrison3110ff82008-05-02 13:42:44 -07004649 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004650 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004651 fl->fl_u.nfs4_fl.owner,
4652 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004653 if (data == NULL)
4654 return -ENOMEM;
4655 if (IS_SETLKW(cmd))
4656 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004657 if (recovery_type > NFS_LOCK_NEW) {
4658 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004659 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004660 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4661 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004662 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004663 msg.rpc_argp = &data->arg;
4664 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004665 task_setup_data.callback_data = data;
4666 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004667 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004668 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004669 ret = nfs4_wait_for_completion_rpc_task(task);
4670 if (ret == 0) {
4671 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004672 if (ret)
4673 nfs4_handle_setlk_error(data->server, data->lsp,
4674 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004675 } else
4676 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004677 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004678 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004679 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680}
4681
4682static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4683{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004684 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004685 struct nfs4_exception exception = {
4686 .inode = state->inode,
4687 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004688 int err;
4689
4690 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004691 /* Cache the lock if possible... */
4692 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4693 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004694 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004695 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004696 break;
4697 nfs4_handle_exception(server, err, &exception);
4698 } while (exception.retry);
4699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700}
4701
4702static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4703{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004704 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004705 struct nfs4_exception exception = {
4706 .inode = state->inode,
4707 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004708 int err;
4709
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004710 err = nfs4_set_lock_state(state, request);
4711 if (err != 0)
4712 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004713 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004714 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4715 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004716 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004717 switch (err) {
4718 default:
4719 goto out;
4720 case -NFS4ERR_GRACE:
4721 case -NFS4ERR_DELAY:
4722 nfs4_handle_exception(server, err, &exception);
4723 err = 0;
4724 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004725 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004726out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004727 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728}
4729
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004730#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004731/**
4732 * nfs41_check_expired_locks - possibly free a lock stateid
4733 *
4734 * @state: NFSv4 state for an inode
4735 *
4736 * Returns NFS_OK if recovery for this stateid is now finished.
4737 * Otherwise a negative NFS4ERR value is returned.
4738 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004739static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004740{
Chuck Levereb64cf92012-07-11 16:30:05 -04004741 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004742 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004743 struct nfs_server *server = NFS_SERVER(state->inode);
4744
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004745 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4746 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4747 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4748 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004749 /* Free the stateid unless the server
4750 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004751 if (status != -NFS4ERR_BAD_STATEID)
4752 nfs41_free_stateid(server,
4753 &lsp->ls_stateid);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004754 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4755 ret = status;
4756 }
4757 }
4758 };
4759
4760 return ret;
4761}
4762
4763static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4764{
4765 int status = NFS_OK;
4766
4767 if (test_bit(LK_STATE_IN_USE, &state->flags))
4768 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004769 if (status != NFS_OK)
4770 status = nfs4_lock_expired(state, request);
4771 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004772}
4773#endif
4774
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4776{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004777 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004778 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004779 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004781 if ((fl_flags & FL_POSIX) &&
4782 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4783 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004784 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004785 status = nfs4_set_lock_state(state, request);
4786 if (status != 0)
4787 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004788 request->fl_flags |= FL_ACCESS;
4789 status = do_vfs_lock(request->fl_file, request);
4790 if (status < 0)
4791 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004792 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004793 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004794 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004795 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004796 request->fl_flags = fl_flags & ~FL_SLEEP;
4797 status = do_vfs_lock(request->fl_file, request);
4798 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004799 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004800 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004801 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004802 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004803 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004804 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004805 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004806 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4807 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004808out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004809 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004810out:
4811 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 return status;
4813}
4814
4815static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4816{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004817 struct nfs4_exception exception = {
4818 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004819 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004820 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 int err;
4822
4823 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004824 err = _nfs4_proc_setlk(state, cmd, request);
4825 if (err == -NFS4ERR_DENIED)
4826 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004828 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 } while (exception.retry);
4830 return err;
4831}
4832
4833static int
4834nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4835{
4836 struct nfs_open_context *ctx;
4837 struct nfs4_state *state;
4838 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4839 int status;
4840
4841 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004842 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 state = ctx->state;
4844
4845 if (request->fl_start < 0 || request->fl_end < 0)
4846 return -EINVAL;
4847
Trond Myklebustd9531262009-07-21 19:22:38 -04004848 if (IS_GETLK(cmd)) {
4849 if (state != NULL)
4850 return nfs4_proc_getlk(state, F_GETLK, request);
4851 return 0;
4852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
4854 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4855 return -EINVAL;
4856
Trond Myklebustd9531262009-07-21 19:22:38 -04004857 if (request->fl_type == F_UNLCK) {
4858 if (state != NULL)
4859 return nfs4_proc_unlck(state, cmd, request);
4860 return 0;
4861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Trond Myklebustd9531262009-07-21 19:22:38 -04004863 if (state == NULL)
4864 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004865 /*
4866 * Don't rely on the VFS having checked the file open mode,
4867 * since it won't do this for flock() locks.
4868 */
4869 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4870 case F_RDLCK:
4871 if (!(filp->f_mode & FMODE_READ))
4872 return -EBADF;
4873 break;
4874 case F_WRLCK:
4875 if (!(filp->f_mode & FMODE_WRITE))
4876 return -EBADF;
4877 }
4878
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 do {
4880 status = nfs4_proc_setlk(state, cmd, request);
4881 if ((status != -EAGAIN) || IS_SETLK(cmd))
4882 break;
4883 timeout = nfs4_set_lock_task_retry(timeout);
4884 status = -ERESTARTSYS;
4885 if (signalled())
4886 break;
4887 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 return status;
4889}
4890
Trond Myklebust888e6942005-11-04 15:38:11 -05004891int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4892{
4893 struct nfs_server *server = NFS_SERVER(state->inode);
4894 struct nfs4_exception exception = { };
4895 int err;
4896
4897 err = nfs4_set_lock_state(state, fl);
4898 if (err != 0)
4899 goto out;
4900 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004901 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004902 switch (err) {
4903 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004904 printk(KERN_ERR "NFS: %s: unhandled error "
4905 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004906 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004907 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004908 goto out;
4909 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004910 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004911 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004912 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004913 nfs4_schedule_lease_recovery(server->nfs_client);
4914 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004915 case -NFS4ERR_BADSESSION:
4916 case -NFS4ERR_BADSLOT:
4917 case -NFS4ERR_BAD_HIGH_SLOT:
4918 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4919 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004920 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004921 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004922 case -ERESTARTSYS:
4923 /*
4924 * The show must go on: exit, but mark the
4925 * stateid as needing recovery.
4926 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004927 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004928 case -NFS4ERR_ADMIN_REVOKED:
4929 case -NFS4ERR_BAD_STATEID:
4930 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004931 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004932 err = 0;
4933 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004934 case -EKEYEXPIRED:
4935 /*
4936 * User RPCSEC_GSS context has expired.
4937 * We cannot recover this stateid now, so
4938 * skip it and allow recovery thread to
4939 * proceed.
4940 */
4941 err = 0;
4942 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004943 case -ENOMEM:
4944 case -NFS4ERR_DENIED:
4945 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4946 err = 0;
4947 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004948 case -NFS4ERR_DELAY:
4949 break;
4950 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004951 err = nfs4_handle_exception(server, err, &exception);
4952 } while (exception.retry);
4953out:
4954 return err;
4955}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004956
Trond Myklebustcf470c32012-03-07 13:49:12 -05004957struct nfs_release_lockowner_data {
4958 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004959 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004960 struct nfs_release_lockowner_args args;
4961};
4962
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004963static void nfs4_release_lockowner_release(void *calldata)
4964{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004965 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004966 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004967 kfree(calldata);
4968}
4969
Trond Myklebust17280172012-03-11 13:11:00 -04004970static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004971 .rpc_release = nfs4_release_lockowner_release,
4972};
4973
Trond Myklebustcf470c32012-03-07 13:49:12 -05004974int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004975{
4976 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004977 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004978 struct rpc_message msg = {
4979 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4980 };
4981
4982 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004983 return -EINVAL;
4984 data = kmalloc(sizeof(*data), GFP_NOFS);
4985 if (!data)
4986 return -ENOMEM;
4987 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004988 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004989 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4990 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4991 data->args.lock_owner.s_dev = server->s_dev;
4992 msg.rpc_argp = &data->args;
4993 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4994 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004995}
4996
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004997#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4998
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004999static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5000 const void *buf, size_t buflen,
5001 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005002{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005003 if (strcmp(key, "") != 0)
5004 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005005
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005006 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005007}
5008
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005009static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5010 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005011{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005012 if (strcmp(key, "") != 0)
5013 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005014
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005015 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005016}
5017
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005018static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5019 size_t list_len, const char *name,
5020 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005021{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005022 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005023
J. Bruce Fields096455a2006-03-20 23:23:42 -05005024 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5025 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005026
5027 if (list && len <= list_len)
5028 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005029 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005030}
5031
Andy Adamson533eb462011-06-13 18:25:56 -04005032/*
5033 * nfs_fhget will use either the mounted_on_fileid or the fileid
5034 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005035static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5036{
Andy Adamson533eb462011-06-13 18:25:56 -04005037 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5038 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5039 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005040 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005041 return;
5042
5043 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005044 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005045 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5046 fattr->nlink = 2;
5047}
5048
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005049static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5050 const struct qstr *name,
5051 struct nfs4_fs_locations *fs_locations,
5052 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005053{
5054 struct nfs_server *server = NFS_SERVER(dir);
5055 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005056 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005057 };
5058 struct nfs4_fs_locations_arg args = {
5059 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005060 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005061 .page = page,
5062 .bitmask = bitmask,
5063 };
Benny Halevy22958462009-04-01 09:22:02 -04005064 struct nfs4_fs_locations_res res = {
5065 .fs_locations = fs_locations,
5066 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005067 struct rpc_message msg = {
5068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5069 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005070 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005071 };
5072 int status;
5073
Harvey Harrison3110ff82008-05-02 13:42:44 -07005074 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005075
5076 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5077 * is not supported */
5078 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5079 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5080 else
5081 bitmask[0] |= FATTR4_WORD0_FILEID;
5082
Trond Myklebustc228fd32007-01-13 02:28:11 -05005083 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005084 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005085 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005086 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005087 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005088 return status;
5089}
5090
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005091int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5092 const struct qstr *name,
5093 struct nfs4_fs_locations *fs_locations,
5094 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005095{
5096 struct nfs4_exception exception = { };
5097 int err;
5098 do {
5099 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005100 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005101 &exception);
5102 } while (exception.retry);
5103 return err;
5104}
5105
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005106static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5107{
5108 int status;
5109 struct nfs4_secinfo_arg args = {
5110 .dir_fh = NFS_FH(dir),
5111 .name = name,
5112 };
5113 struct nfs4_secinfo_res res = {
5114 .flavors = flavors,
5115 };
5116 struct rpc_message msg = {
5117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5118 .rpc_argp = &args,
5119 .rpc_resp = &res,
5120 };
5121
5122 dprintk("NFS call secinfo %s\n", name->name);
5123 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5124 dprintk("NFS reply secinfo: %d\n", status);
5125 return status;
5126}
5127
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005128int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5129 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005130{
5131 struct nfs4_exception exception = { };
5132 int err;
5133 do {
5134 err = nfs4_handle_exception(NFS_SERVER(dir),
5135 _nfs4_proc_secinfo(dir, name, flavors),
5136 &exception);
5137 } while (exception.retry);
5138 return err;
5139}
5140
Andy Adamson557134a2009-04-01 09:21:53 -04005141#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005142/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005143 * Check the exchange flags returned by the server for invalid flags, having
5144 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5145 * DS flags set.
5146 */
5147static int nfs4_check_cl_exchange_flags(u32 flags)
5148{
5149 if (flags & ~EXCHGID4_FLAG_MASK_R)
5150 goto out_inval;
5151 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5152 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5153 goto out_inval;
5154 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5155 goto out_inval;
5156 return NFS_OK;
5157out_inval:
5158 return -NFS4ERR_INVAL;
5159}
5160
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005161static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005162nfs41_same_server_scope(struct nfs41_server_scope *a,
5163 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005164{
5165 if (a->server_scope_sz == b->server_scope_sz &&
5166 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5167 return true;
5168
5169 return false;
5170}
5171
Andy Adamson357f54d2010-12-14 10:11:57 -05005172/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005173 * nfs4_proc_bind_conn_to_session()
5174 *
5175 * The 4.1 client currently uses the same TCP connection for the
5176 * fore and backchannel.
5177 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005178int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005179{
5180 int status;
5181 struct nfs41_bind_conn_to_session_res res;
5182 struct rpc_message msg = {
5183 .rpc_proc =
5184 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5185 .rpc_argp = clp,
5186 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005187 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005188 };
5189
5190 dprintk("--> %s\n", __func__);
5191 BUG_ON(clp == NULL);
5192
5193 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5194 if (unlikely(res.session == NULL)) {
5195 status = -ENOMEM;
5196 goto out;
5197 }
5198
5199 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5200 if (status == 0) {
5201 if (memcmp(res.session->sess_id.data,
5202 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5203 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5204 status = -EIO;
5205 goto out_session;
5206 }
5207 if (res.dir != NFS4_CDFS4_BOTH) {
5208 dprintk("NFS: %s: Unexpected direction from server\n",
5209 __func__);
5210 status = -EIO;
5211 goto out_session;
5212 }
5213 if (res.use_conn_in_rdma_mode) {
5214 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5215 __func__);
5216 status = -EIO;
5217 goto out_session;
5218 }
5219 }
5220out_session:
5221 kfree(res.session);
5222out:
5223 dprintk("<-- %s status= %d\n", __func__, status);
5224 return status;
5225}
5226
5227/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005228 * nfs4_proc_exchange_id()
5229 *
5230 * Since the clientid has expired, all compounds using sessions
5231 * associated with the stale clientid will be returning
5232 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5233 * be in some phase of session reset.
5234 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005235int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005236{
5237 nfs4_verifier verifier;
5238 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005239 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005240 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005241 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005242 };
5243 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005244 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005245 };
5246 int status;
5247 struct rpc_message msg = {
5248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5249 .rpc_argp = &args,
5250 .rpc_resp = &res,
5251 .rpc_cred = cred,
5252 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005253
5254 dprintk("--> %s\n", __func__);
5255 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005256
Chuck Leverf0920752012-05-21 22:45:41 -04005257 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005258
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005259 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005260 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005261 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005262 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005263 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005264
Chuck Leveracdeb692012-05-21 22:46:16 -04005265 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005266 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005267 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005268 status = -ENOMEM;
5269 goto out;
5270 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005271
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005272 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005273 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005274 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005275 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005276 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005277 }
5278
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005279 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005280 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005281 status = -ENOMEM;
5282 goto out_server_scope;
5283 }
5284
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005285 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005286 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005287 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005288
Chuck Lever177313f2012-05-21 22:44:58 -04005289 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005290 clp->cl_clientid = res.clientid;
5291 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5292 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5293 clp->cl_seqid = res.seqid;
5294
Chuck Leveracdeb692012-05-21 22:46:16 -04005295 kfree(clp->cl_serverowner);
5296 clp->cl_serverowner = res.server_owner;
5297 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005298
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005299 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005300 kfree(clp->cl_implid);
5301 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005302
Chuck Lever177313f2012-05-21 22:44:58 -04005303 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005304 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005305 res.server_scope)) {
5306 dprintk("%s: server_scope mismatch detected\n",
5307 __func__);
5308 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005309 kfree(clp->cl_serverscope);
5310 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005311 }
5312
Chuck Lever177313f2012-05-21 22:44:58 -04005313 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005314 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005315 goto out;
5316 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005317 } else
5318 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005319
Chuck Leveracdeb692012-05-21 22:46:16 -04005320out_server_owner:
5321 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005322out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005323 kfree(res.server_scope);
5324out:
Chuck Lever177313f2012-05-21 22:44:58 -04005325 if (clp->cl_implid != NULL)
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005326 dprintk("%s: Server Implementation ID: "
5327 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever59155542012-05-21 22:44:41 -04005328 __func__, clp->cl_implid->domain, clp->cl_implid->name,
5329 clp->cl_implid->date.seconds,
5330 clp->cl_implid->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005331 dprintk("<-- %s status= %d\n", __func__, status);
5332 return status;
5333}
5334
Trond Myklebust66245532012-05-25 17:18:09 -04005335static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5336 struct rpc_cred *cred)
5337{
5338 struct rpc_message msg = {
5339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5340 .rpc_argp = clp,
5341 .rpc_cred = cred,
5342 };
5343 int status;
5344
5345 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5346 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005347 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005348 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5349 return status;
5350}
5351
5352static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5353 struct rpc_cred *cred)
5354{
5355 unsigned int loop;
5356 int ret;
5357
5358 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5359 ret = _nfs4_proc_destroy_clientid(clp, cred);
5360 switch (ret) {
5361 case -NFS4ERR_DELAY:
5362 case -NFS4ERR_CLIENTID_BUSY:
5363 ssleep(1);
5364 break;
5365 default:
5366 return ret;
5367 }
5368 }
5369 return 0;
5370}
5371
5372int nfs4_destroy_clientid(struct nfs_client *clp)
5373{
5374 struct rpc_cred *cred;
5375 int ret = 0;
5376
5377 if (clp->cl_mvops->minor_version < 1)
5378 goto out;
5379 if (clp->cl_exchange_flags == 0)
5380 goto out;
5381 cred = nfs4_get_exchange_id_cred(clp);
5382 ret = nfs4_proc_destroy_clientid(clp, cred);
5383 if (cred)
5384 put_rpccred(cred);
5385 switch (ret) {
5386 case 0:
5387 case -NFS4ERR_STALE_CLIENTID:
5388 clp->cl_exchange_flags = 0;
5389 }
5390out:
5391 return ret;
5392}
5393
Andy Adamson2050f0c2009-04-01 09:22:30 -04005394struct nfs4_get_lease_time_data {
5395 struct nfs4_get_lease_time_args *args;
5396 struct nfs4_get_lease_time_res *res;
5397 struct nfs_client *clp;
5398};
5399
5400static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5401 void *calldata)
5402{
5403 int ret;
5404 struct nfs4_get_lease_time_data *data =
5405 (struct nfs4_get_lease_time_data *)calldata;
5406
5407 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005408 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005409 /* just setup sequence, do not trigger session recovery
5410 since we're invoked within one */
5411 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005412 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005413 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005414
5415 BUG_ON(ret == -EAGAIN);
5416 rpc_call_start(task);
5417 dprintk("<-- %s\n", __func__);
5418}
5419
5420/*
5421 * Called from nfs4_state_manager thread for session setup, so don't recover
5422 * from sequence operation or clientid errors.
5423 */
5424static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5425{
5426 struct nfs4_get_lease_time_data *data =
5427 (struct nfs4_get_lease_time_data *)calldata;
5428
5429 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005430 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5431 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005432 switch (task->tk_status) {
5433 case -NFS4ERR_DELAY:
5434 case -NFS4ERR_GRACE:
5435 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5436 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5437 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005438 /* fall through */
5439 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005440 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005441 return;
5442 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005443 dprintk("<-- %s\n", __func__);
5444}
5445
Trond Myklebust17280172012-03-11 13:11:00 -04005446static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005447 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5448 .rpc_call_done = nfs4_get_lease_time_done,
5449};
5450
5451int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5452{
5453 struct rpc_task *task;
5454 struct nfs4_get_lease_time_args args;
5455 struct nfs4_get_lease_time_res res = {
5456 .lr_fsinfo = fsinfo,
5457 };
5458 struct nfs4_get_lease_time_data data = {
5459 .args = &args,
5460 .res = &res,
5461 .clp = clp,
5462 };
5463 struct rpc_message msg = {
5464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5465 .rpc_argp = &args,
5466 .rpc_resp = &res,
5467 };
5468 struct rpc_task_setup task_setup = {
5469 .rpc_client = clp->cl_rpcclient,
5470 .rpc_message = &msg,
5471 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005472 .callback_data = &data,
5473 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005474 };
5475 int status;
5476
Trond Myklebust9d12b212012-01-17 22:04:25 -05005477 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005478 dprintk("--> %s\n", __func__);
5479 task = rpc_run_task(&task_setup);
5480
5481 if (IS_ERR(task))
5482 status = PTR_ERR(task);
5483 else {
5484 status = task->tk_status;
5485 rpc_put_task(task);
5486 }
5487 dprintk("<-- %s return %d\n", __func__, status);
5488
5489 return status;
5490}
5491
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005492static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5493{
5494 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5495}
5496
5497static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5498 struct nfs4_slot *new,
5499 u32 max_slots,
5500 u32 ivalue)
5501{
5502 struct nfs4_slot *old = NULL;
5503 u32 i;
5504
5505 spin_lock(&tbl->slot_tbl_lock);
5506 if (new) {
5507 old = tbl->slots;
5508 tbl->slots = new;
5509 tbl->max_slots = max_slots;
5510 }
5511 tbl->highest_used_slotid = -1; /* no slot is currently used */
5512 for (i = 0; i < tbl->max_slots; i++)
5513 tbl->slots[i].seq_nr = ivalue;
5514 spin_unlock(&tbl->slot_tbl_lock);
5515 kfree(old);
5516}
5517
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005518/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005519 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005520 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005521static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5522 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005523{
Andy Adamson104aeba2010-01-14 17:45:10 -05005524 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005525 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005526
Andy Adamson104aeba2010-01-14 17:45:10 -05005527 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5528 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005529
Andy Adamson104aeba2010-01-14 17:45:10 -05005530 /* Does the newly negotiated max_reqs match the existing slot table? */
5531 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005532 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005533 if (!new)
5534 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005535 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005536 ret = 0;
5537
5538 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005539 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5540 tbl, tbl->slots, tbl->max_slots);
5541out:
5542 dprintk("<-- %s: return %d\n", __func__, ret);
5543 return ret;
5544}
5545
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005546/* Destroy the slot table */
5547static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5548{
5549 if (session->fc_slot_table.slots != NULL) {
5550 kfree(session->fc_slot_table.slots);
5551 session->fc_slot_table.slots = NULL;
5552 }
5553 if (session->bc_slot_table.slots != NULL) {
5554 kfree(session->bc_slot_table.slots);
5555 session->bc_slot_table.slots = NULL;
5556 }
5557 return;
5558}
5559
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005560/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005561 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005562 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005563static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005564{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005565 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005566 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005567
Andy Adamsonaacd5532011-11-09 13:58:21 -05005568 dprintk("--> %s\n", __func__);
5569 /* Fore channel */
5570 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005571 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5572 if (status) /* -ENOMEM */
5573 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005574 /* Back channel */
5575 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005576 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5577 if (status && tbl->slots == NULL)
5578 /* Fore and back channel share a connection so get
5579 * both slot tables or neither */
5580 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005581 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005582}
5583
5584struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5585{
5586 struct nfs4_session *session;
5587 struct nfs4_slot_table *tbl;
5588
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005589 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005590 if (!session)
5591 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005592
Andy Adamson557134a2009-04-01 09:21:53 -04005593 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005594 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005595 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005596 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005597 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005598
5599 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005600 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005601 spin_lock_init(&tbl->slot_tbl_lock);
5602 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005603 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005604
Trond Myklebust1055d762010-06-16 09:52:27 -04005605 session->session_state = 1<<NFS4_SESSION_INITING;
5606
Andy Adamson557134a2009-04-01 09:21:53 -04005607 session->clp = clp;
5608 return session;
5609}
5610
5611void nfs4_destroy_session(struct nfs4_session *session)
5612{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005613 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005614 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005615
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005616 cred = nfs4_get_exchange_id_cred(session->clp);
5617 nfs4_proc_destroy_session(session, cred);
5618 if (cred)
5619 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005620
5621 rcu_read_lock();
5622 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5623 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005624 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005625 __func__, xprt);
5626 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005627 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005628 kfree(session);
5629}
5630
Andy Adamsonfc931582009-04-01 09:22:31 -04005631/*
5632 * Initialize the values to be used by the client in CREATE_SESSION
5633 * If nfs4_init_session set the fore channel request and response sizes,
5634 * use them.
5635 *
5636 * Set the back channel max_resp_sz_cached to zero to force the client to
5637 * always set csa_cachethis to FALSE because the current implementation
5638 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5639 */
5640static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5641{
5642 struct nfs4_session *session = args->client->cl_session;
5643 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5644 mxresp_sz = session->fc_attrs.max_resp_sz;
5645
5646 if (mxrqst_sz == 0)
5647 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5648 if (mxresp_sz == 0)
5649 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5650 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005651 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5652 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005653 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005654 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005655
5656 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005657 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005658 __func__,
5659 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005660 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005661
5662 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005663 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5664 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5665 args->bc_attrs.max_resp_sz_cached = 0;
5666 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5667 args->bc_attrs.max_reqs = 1;
5668
5669 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5670 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5671 __func__,
5672 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5673 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5674 args->bc_attrs.max_reqs);
5675}
5676
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005677static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005678{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005679 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5680 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5681
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005682 if (rcvd->max_resp_sz > sent->max_resp_sz)
5683 return -EINVAL;
5684 /*
5685 * Our requested max_ops is the minimum we need; we're not
5686 * prepared to break up compounds into smaller pieces than that.
5687 * So, no point even trying to continue if the server won't
5688 * cooperate:
5689 */
5690 if (rcvd->max_ops < sent->max_ops)
5691 return -EINVAL;
5692 if (rcvd->max_reqs == 0)
5693 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005694 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5695 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005696 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005697}
5698
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005699static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5700{
5701 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5702 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005703
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005704 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5705 return -EINVAL;
5706 if (rcvd->max_resp_sz < sent->max_resp_sz)
5707 return -EINVAL;
5708 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5709 return -EINVAL;
5710 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005711 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005712 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005713 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005714 return -EINVAL;
5715 return 0;
5716}
Andy Adamson8d353012009-04-01 09:22:32 -04005717
Andy Adamson8d353012009-04-01 09:22:32 -04005718static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5719 struct nfs4_session *session)
5720{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005721 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005722
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005723 ret = nfs4_verify_fore_channel_attrs(args, session);
5724 if (ret)
5725 return ret;
5726 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005727}
5728
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005729static int _nfs4_proc_create_session(struct nfs_client *clp,
5730 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005731{
5732 struct nfs4_session *session = clp->cl_session;
5733 struct nfs41_create_session_args args = {
5734 .client = clp,
5735 .cb_program = NFS4_CALLBACK,
5736 };
5737 struct nfs41_create_session_res res = {
5738 .client = clp,
5739 };
5740 struct rpc_message msg = {
5741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5742 .rpc_argp = &args,
5743 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005744 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005745 };
5746 int status;
5747
5748 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005749 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005750
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005751 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005752
Andy Adamson8d353012009-04-01 09:22:32 -04005753 if (!status)
5754 /* Verify the session's negotiated channel_attrs values */
5755 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005756 if (!status) {
5757 /* Increment the clientid slot sequence id */
5758 clp->cl_seqid++;
5759 }
5760
5761 return status;
5762}
5763
5764/*
5765 * Issues a CREATE_SESSION operation to the server.
5766 * It is the responsibility of the caller to verify the session is
5767 * expired before calling this routine.
5768 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005769int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005770{
5771 int status;
5772 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005773 struct nfs4_session *session = clp->cl_session;
5774
5775 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5776
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005777 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005778 if (status)
5779 goto out;
5780
Andy Adamsonaacd5532011-11-09 13:58:21 -05005781 /* Init or reset the session slot tables */
5782 status = nfs4_setup_session_slot_tables(session);
5783 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005784 if (status)
5785 goto out;
5786
5787 ptr = (unsigned *)&session->sess_id.data[0];
5788 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5789 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005790out:
5791 dprintk("<-- %s\n", __func__);
5792 return status;
5793}
5794
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005795/*
5796 * Issue the over-the-wire RPC DESTROY_SESSION.
5797 * The caller must serialize access to this routine.
5798 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005799int nfs4_proc_destroy_session(struct nfs4_session *session,
5800 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005801{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005802 struct rpc_message msg = {
5803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5804 .rpc_argp = session,
5805 .rpc_cred = cred,
5806 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005807 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005808
5809 dprintk("--> nfs4_proc_destroy_session\n");
5810
5811 /* session is still being setup */
5812 if (session->clp->cl_cons_state != NFS_CS_READY)
5813 return status;
5814
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005815 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005816
5817 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005818 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005819 "Session has been destroyed regardless...\n", status);
5820
5821 dprintk("<-- nfs4_proc_destroy_session\n");
5822 return status;
5823}
5824
Trond Myklebust7b38c362012-05-23 13:23:31 -04005825/*
5826 * With sessions, the client is not marked ready until after a
5827 * successful EXCHANGE_ID and CREATE_SESSION.
5828 *
5829 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5830 * other versions of NFS can be tried.
5831 */
5832static int nfs41_check_session_ready(struct nfs_client *clp)
5833{
5834 int ret;
5835
5836 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5837 ret = nfs4_client_recover_expired_lease(clp);
5838 if (ret)
5839 return ret;
5840 }
5841 if (clp->cl_cons_state < NFS_CS_READY)
5842 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005843 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005844 return 0;
5845}
5846
Trond Myklebustfccba802009-07-21 16:48:07 -04005847int nfs4_init_session(struct nfs_server *server)
5848{
5849 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005850 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005851 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005852
5853 if (!nfs4_has_session(clp))
5854 return 0;
5855
Trond Myklebust1055d762010-06-16 09:52:27 -04005856 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005857 spin_lock(&clp->cl_lock);
5858 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005859
Trond Myklebust7b38c362012-05-23 13:23:31 -04005860 rsize = server->rsize;
5861 if (rsize == 0)
5862 rsize = NFS_MAX_FILE_IO_SIZE;
5863 wsize = server->wsize;
5864 if (wsize == 0)
5865 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005866
Trond Myklebust7b38c362012-05-23 13:23:31 -04005867 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5868 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5869 }
5870 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005871
Trond Myklebust7b38c362012-05-23 13:23:31 -04005872 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005873}
5874
Trond Myklebust7b38c362012-05-23 13:23:31 -04005875int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005876{
5877 struct nfs4_session *session = clp->cl_session;
5878 int ret;
5879
Trond Myklebust7b38c362012-05-23 13:23:31 -04005880 spin_lock(&clp->cl_lock);
5881 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5882 /*
5883 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5884 * DS lease to be equal to the MDS lease.
5885 */
5886 clp->cl_lease_time = lease_time;
5887 clp->cl_last_renewal = jiffies;
5888 }
5889 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005890
Trond Myklebust7b38c362012-05-23 13:23:31 -04005891 ret = nfs41_check_session_ready(clp);
5892 if (ret)
5893 return ret;
5894 /* Test for the DS role */
5895 if (!is_ds_client(clp))
5896 return -ENODEV;
5897 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005898}
5899EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5900
5901
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005902/*
5903 * Renew the cl_session lease.
5904 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005905struct nfs4_sequence_data {
5906 struct nfs_client *clp;
5907 struct nfs4_sequence_args args;
5908 struct nfs4_sequence_res res;
5909};
5910
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005911static void nfs41_sequence_release(void *data)
5912{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005913 struct nfs4_sequence_data *calldata = data;
5914 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005915
Alexandros Batsakis71358402010-02-05 03:45:05 -08005916 if (atomic_read(&clp->cl_count) > 1)
5917 nfs4_schedule_state_renewal(clp);
5918 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005919 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005920}
5921
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005922static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5923{
5924 switch(task->tk_status) {
5925 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005926 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5927 return -EAGAIN;
5928 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005929 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005930 }
5931 return 0;
5932}
5933
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005934static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005935{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005936 struct nfs4_sequence_data *calldata = data;
5937 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005938
Trond Myklebust14516c32010-07-31 14:29:06 -04005939 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5940 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005941
5942 if (task->tk_status < 0) {
5943 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005944 if (atomic_read(&clp->cl_count) == 1)
5945 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005946
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005947 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5948 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005949 return;
5950 }
5951 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005952 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005953out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005954 dprintk("<-- %s\n", __func__);
5955}
5956
5957static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5958{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005959 struct nfs4_sequence_data *calldata = data;
5960 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005961 struct nfs4_sequence_args *args;
5962 struct nfs4_sequence_res *res;
5963
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005964 args = task->tk_msg.rpc_argp;
5965 res = task->tk_msg.rpc_resp;
5966
Trond Myklebust9d12b212012-01-17 22:04:25 -05005967 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005968 return;
5969 rpc_call_start(task);
5970}
5971
5972static const struct rpc_call_ops nfs41_sequence_ops = {
5973 .rpc_call_done = nfs41_sequence_call_done,
5974 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005975 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005976};
5977
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005978static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005979{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005980 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005981 struct rpc_message msg = {
5982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5983 .rpc_cred = cred,
5984 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005985 struct rpc_task_setup task_setup_data = {
5986 .rpc_client = clp->cl_rpcclient,
5987 .rpc_message = &msg,
5988 .callback_ops = &nfs41_sequence_ops,
5989 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5990 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005991
Alexandros Batsakis71358402010-02-05 03:45:05 -08005992 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005993 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005994 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005995 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005996 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005997 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005998 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005999 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006000 msg.rpc_argp = &calldata->args;
6001 msg.rpc_resp = &calldata->res;
6002 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006003 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006004
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006005 return rpc_run_task(&task_setup_data);
6006}
6007
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006008static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006009{
6010 struct rpc_task *task;
6011 int ret = 0;
6012
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006013 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6014 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006015 task = _nfs41_proc_sequence(clp, cred);
6016 if (IS_ERR(task))
6017 ret = PTR_ERR(task);
6018 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006019 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006020 dprintk("<-- %s status=%d\n", __func__, ret);
6021 return ret;
6022}
6023
6024static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6025{
6026 struct rpc_task *task;
6027 int ret;
6028
6029 task = _nfs41_proc_sequence(clp, cred);
6030 if (IS_ERR(task)) {
6031 ret = PTR_ERR(task);
6032 goto out;
6033 }
6034 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006035 if (!ret) {
6036 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6037
6038 if (task->tk_status == 0)
6039 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006040 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006041 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006042 rpc_put_task(task);
6043out:
6044 dprintk("<-- %s status=%d\n", __func__, ret);
6045 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006046}
6047
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006048struct nfs4_reclaim_complete_data {
6049 struct nfs_client *clp;
6050 struct nfs41_reclaim_complete_args arg;
6051 struct nfs41_reclaim_complete_res res;
6052};
6053
6054static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6055{
6056 struct nfs4_reclaim_complete_data *calldata = data;
6057
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006058 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006059 if (nfs41_setup_sequence(calldata->clp->cl_session,
6060 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006061 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006062 return;
6063
6064 rpc_call_start(task);
6065}
6066
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006067static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6068{
6069 switch(task->tk_status) {
6070 case 0:
6071 case -NFS4ERR_COMPLETE_ALREADY:
6072 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6073 break;
6074 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006075 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006076 /* fall through */
6077 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006078 return -EAGAIN;
6079 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006080 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006081 }
6082 return 0;
6083}
6084
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006085static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6086{
6087 struct nfs4_reclaim_complete_data *calldata = data;
6088 struct nfs_client *clp = calldata->clp;
6089 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6090
6091 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006092 if (!nfs41_sequence_done(task, res))
6093 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006094
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006095 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6096 rpc_restart_call_prepare(task);
6097 return;
6098 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006099 dprintk("<-- %s\n", __func__);
6100}
6101
6102static void nfs4_free_reclaim_complete_data(void *data)
6103{
6104 struct nfs4_reclaim_complete_data *calldata = data;
6105
6106 kfree(calldata);
6107}
6108
6109static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6110 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6111 .rpc_call_done = nfs4_reclaim_complete_done,
6112 .rpc_release = nfs4_free_reclaim_complete_data,
6113};
6114
6115/*
6116 * Issue a global reclaim complete.
6117 */
6118static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6119{
6120 struct nfs4_reclaim_complete_data *calldata;
6121 struct rpc_task *task;
6122 struct rpc_message msg = {
6123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6124 };
6125 struct rpc_task_setup task_setup_data = {
6126 .rpc_client = clp->cl_rpcclient,
6127 .rpc_message = &msg,
6128 .callback_ops = &nfs4_reclaim_complete_call_ops,
6129 .flags = RPC_TASK_ASYNC,
6130 };
6131 int status = -ENOMEM;
6132
6133 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006134 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006135 if (calldata == NULL)
6136 goto out;
6137 calldata->clp = clp;
6138 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006139
Trond Myklebust9d12b212012-01-17 22:04:25 -05006140 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006141 msg.rpc_argp = &calldata->arg;
6142 msg.rpc_resp = &calldata->res;
6143 task_setup_data.callback_data = calldata;
6144 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006145 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006146 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006147 goto out;
6148 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006149 status = nfs4_wait_for_completion_rpc_task(task);
6150 if (status == 0)
6151 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006152 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006153 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006154out:
6155 dprintk("<-- %s status=%d\n", __func__, status);
6156 return status;
6157}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006158
6159static void
6160nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6161{
6162 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006163 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006164
6165 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006166 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6167 * right now covering the LAYOUTGET we are about to send.
6168 * However, that is not so catastrophic, and there seems
6169 * to be no way to prevent it completely.
6170 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006171 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006172 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006173 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006174 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6175 NFS_I(lgp->args.inode)->layout,
6176 lgp->args.ctx->state)) {
6177 rpc_exit(task, NFS4_OK);
6178 return;
6179 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006180 rpc_call_start(task);
6181}
6182
6183static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6184{
6185 struct nfs4_layoutget *lgp = calldata;
6186 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6187
6188 dprintk("--> %s\n", __func__);
6189
6190 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6191 return;
6192
6193 switch (task->tk_status) {
6194 case 0:
6195 break;
6196 case -NFS4ERR_LAYOUTTRYLATER:
6197 case -NFS4ERR_RECALLCONFLICT:
6198 task->tk_status = -NFS4ERR_DELAY;
6199 /* Fall through */
6200 default:
6201 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6202 rpc_restart_call_prepare(task);
6203 return;
6204 }
6205 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006206 dprintk("<-- %s\n", __func__);
6207}
6208
6209static void nfs4_layoutget_release(void *calldata)
6210{
6211 struct nfs4_layoutget *lgp = calldata;
6212
6213 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006214 put_nfs_open_context(lgp->args.ctx);
6215 kfree(calldata);
6216 dprintk("<-- %s\n", __func__);
6217}
6218
6219static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6220 .rpc_call_prepare = nfs4_layoutget_prepare,
6221 .rpc_call_done = nfs4_layoutget_done,
6222 .rpc_release = nfs4_layoutget_release,
6223};
6224
6225int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6226{
6227 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6228 struct rpc_task *task;
6229 struct rpc_message msg = {
6230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6231 .rpc_argp = &lgp->args,
6232 .rpc_resp = &lgp->res,
6233 };
6234 struct rpc_task_setup task_setup_data = {
6235 .rpc_client = server->client,
6236 .rpc_message = &msg,
6237 .callback_ops = &nfs4_layoutget_call_ops,
6238 .callback_data = lgp,
6239 .flags = RPC_TASK_ASYNC,
6240 };
6241 int status = 0;
6242
6243 dprintk("--> %s\n", __func__);
6244
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006245 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006246 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006247 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006248 task = rpc_run_task(&task_setup_data);
6249 if (IS_ERR(task))
6250 return PTR_ERR(task);
6251 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006252 if (status == 0)
6253 status = task->tk_status;
6254 if (status == 0)
6255 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006256 rpc_put_task(task);
6257 dprintk("<-- %s status=%d\n", __func__, status);
6258 return status;
6259}
6260
Benny Halevycbe82602011-05-22 19:52:37 +03006261static void
6262nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6263{
6264 struct nfs4_layoutreturn *lrp = calldata;
6265
6266 dprintk("--> %s\n", __func__);
6267 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006268 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006269 return;
6270 rpc_call_start(task);
6271}
6272
6273static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6274{
6275 struct nfs4_layoutreturn *lrp = calldata;
6276 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006277 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006278
6279 dprintk("--> %s\n", __func__);
6280
6281 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6282 return;
6283
6284 server = NFS_SERVER(lrp->args.inode);
6285 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006286 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006287 return;
6288 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006289 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006290 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006291 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006292 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006293 } else
6294 BUG_ON(!list_empty(&lo->plh_segs));
6295 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006296 lo->plh_block_lgets--;
6297 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006298 dprintk("<-- %s\n", __func__);
6299}
6300
6301static void nfs4_layoutreturn_release(void *calldata)
6302{
6303 struct nfs4_layoutreturn *lrp = calldata;
6304
6305 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006306 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006307 kfree(calldata);
6308 dprintk("<-- %s\n", __func__);
6309}
6310
6311static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6312 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6313 .rpc_call_done = nfs4_layoutreturn_done,
6314 .rpc_release = nfs4_layoutreturn_release,
6315};
6316
6317int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6318{
6319 struct rpc_task *task;
6320 struct rpc_message msg = {
6321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6322 .rpc_argp = &lrp->args,
6323 .rpc_resp = &lrp->res,
6324 };
6325 struct rpc_task_setup task_setup_data = {
6326 .rpc_client = lrp->clp->cl_rpcclient,
6327 .rpc_message = &msg,
6328 .callback_ops = &nfs4_layoutreturn_call_ops,
6329 .callback_data = lrp,
6330 };
6331 int status;
6332
6333 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006334 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006335 task = rpc_run_task(&task_setup_data);
6336 if (IS_ERR(task))
6337 return PTR_ERR(task);
6338 status = task->tk_status;
6339 dprintk("<-- %s status=%d\n", __func__, status);
6340 rpc_put_task(task);
6341 return status;
6342}
6343
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006344/*
6345 * Retrieve the list of Data Server devices from the MDS.
6346 */
6347static int _nfs4_getdevicelist(struct nfs_server *server,
6348 const struct nfs_fh *fh,
6349 struct pnfs_devicelist *devlist)
6350{
6351 struct nfs4_getdevicelist_args args = {
6352 .fh = fh,
6353 .layoutclass = server->pnfs_curr_ld->id,
6354 };
6355 struct nfs4_getdevicelist_res res = {
6356 .devlist = devlist,
6357 };
6358 struct rpc_message msg = {
6359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6360 .rpc_argp = &args,
6361 .rpc_resp = &res,
6362 };
6363 int status;
6364
6365 dprintk("--> %s\n", __func__);
6366 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6367 &res.seq_res, 0);
6368 dprintk("<-- %s status=%d\n", __func__, status);
6369 return status;
6370}
6371
6372int nfs4_proc_getdevicelist(struct nfs_server *server,
6373 const struct nfs_fh *fh,
6374 struct pnfs_devicelist *devlist)
6375{
6376 struct nfs4_exception exception = { };
6377 int err;
6378
6379 do {
6380 err = nfs4_handle_exception(server,
6381 _nfs4_getdevicelist(server, fh, devlist),
6382 &exception);
6383 } while (exception.retry);
6384
6385 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6386 err, devlist->num_devs);
6387
6388 return err;
6389}
6390EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6391
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006392static int
6393_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6394{
6395 struct nfs4_getdeviceinfo_args args = {
6396 .pdev = pdev,
6397 };
6398 struct nfs4_getdeviceinfo_res res = {
6399 .pdev = pdev,
6400 };
6401 struct rpc_message msg = {
6402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6403 .rpc_argp = &args,
6404 .rpc_resp = &res,
6405 };
6406 int status;
6407
6408 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006409 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006410 dprintk("<-- %s status=%d\n", __func__, status);
6411
6412 return status;
6413}
6414
6415int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6416{
6417 struct nfs4_exception exception = { };
6418 int err;
6419
6420 do {
6421 err = nfs4_handle_exception(server,
6422 _nfs4_proc_getdeviceinfo(server, pdev),
6423 &exception);
6424 } while (exception.retry);
6425 return err;
6426}
6427EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6428
Andy Adamson863a3c62011-03-23 13:27:54 +00006429static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6430{
6431 struct nfs4_layoutcommit_data *data = calldata;
6432 struct nfs_server *server = NFS_SERVER(data->args.inode);
6433
6434 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006435 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006436 return;
6437 rpc_call_start(task);
6438}
6439
6440static void
6441nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6442{
6443 struct nfs4_layoutcommit_data *data = calldata;
6444 struct nfs_server *server = NFS_SERVER(data->args.inode);
6445
6446 if (!nfs4_sequence_done(task, &data->res.seq_res))
6447 return;
6448
6449 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006450 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6451 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6452 case -NFS4ERR_BADLAYOUT: /* no layout */
6453 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006454 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006455 break;
6456 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006457 nfs_post_op_update_inode_force_wcc(data->args.inode,
6458 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006459 break;
6460 default:
6461 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6462 rpc_restart_call_prepare(task);
6463 return;
6464 }
6465 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006466}
6467
6468static void nfs4_layoutcommit_release(void *calldata)
6469{
6470 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006471 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006472 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006473
Andy Adamsondb29c082011-07-30 20:52:38 -04006474 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006475 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006476 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6477 list_del_init(&lseg->pls_lc_list);
6478 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6479 &lseg->pls_flags))
6480 put_lseg(lseg);
6481 }
Peng Tao92407e72011-10-23 20:21:17 -07006482
6483 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6484 smp_mb__after_clear_bit();
6485 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6486
Andy Adamson863a3c62011-03-23 13:27:54 +00006487 put_rpccred(data->cred);
6488 kfree(data);
6489}
6490
6491static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6492 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6493 .rpc_call_done = nfs4_layoutcommit_done,
6494 .rpc_release = nfs4_layoutcommit_release,
6495};
6496
6497int
Andy Adamsonef311532011-03-12 02:58:10 -05006498nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006499{
6500 struct rpc_message msg = {
6501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6502 .rpc_argp = &data->args,
6503 .rpc_resp = &data->res,
6504 .rpc_cred = data->cred,
6505 };
6506 struct rpc_task_setup task_setup_data = {
6507 .task = &data->task,
6508 .rpc_client = NFS_CLIENT(data->args.inode),
6509 .rpc_message = &msg,
6510 .callback_ops = &nfs4_layoutcommit_ops,
6511 .callback_data = data,
6512 .flags = RPC_TASK_ASYNC,
6513 };
6514 struct rpc_task *task;
6515 int status = 0;
6516
6517 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6518 "lbw: %llu inode %lu\n",
6519 data->task.tk_pid, sync,
6520 data->args.lastbytewritten,
6521 data->args.inode->i_ino);
6522
Trond Myklebust9d12b212012-01-17 22:04:25 -05006523 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006524 task = rpc_run_task(&task_setup_data);
6525 if (IS_ERR(task))
6526 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006527 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006528 goto out;
6529 status = nfs4_wait_for_completion_rpc_task(task);
6530 if (status != 0)
6531 goto out;
6532 status = task->tk_status;
6533out:
6534 dprintk("%s: status %d\n", __func__, status);
6535 rpc_put_task(task);
6536 return status;
6537}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006538
6539static int
6540_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6541 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6542{
6543 struct nfs41_secinfo_no_name_args args = {
6544 .style = SECINFO_STYLE_CURRENT_FH,
6545 };
6546 struct nfs4_secinfo_res res = {
6547 .flavors = flavors,
6548 };
6549 struct rpc_message msg = {
6550 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6551 .rpc_argp = &args,
6552 .rpc_resp = &res,
6553 };
6554 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6555}
6556
6557static int
6558nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6559 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6560{
6561 struct nfs4_exception exception = { };
6562 int err;
6563 do {
6564 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6565 switch (err) {
6566 case 0:
6567 case -NFS4ERR_WRONGSEC:
6568 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006569 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006570 default:
6571 err = nfs4_handle_exception(server, err, &exception);
6572 }
6573 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006574out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006575 return err;
6576}
6577
6578static int
6579nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6580 struct nfs_fsinfo *info)
6581{
6582 int err;
6583 struct page *page;
6584 rpc_authflavor_t flavor;
6585 struct nfs4_secinfo_flavors *flavors;
6586
6587 page = alloc_page(GFP_KERNEL);
6588 if (!page) {
6589 err = -ENOMEM;
6590 goto out;
6591 }
6592
6593 flavors = page_address(page);
6594 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6595
6596 /*
6597 * Fall back on "guess and check" method if
6598 * the server doesn't support SECINFO_NO_NAME
6599 */
6600 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6601 err = nfs4_find_root_sec(server, fhandle, info);
6602 goto out_freepage;
6603 }
6604 if (err)
6605 goto out_freepage;
6606
6607 flavor = nfs_find_best_sec(flavors);
6608 if (err == 0)
6609 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6610
6611out_freepage:
6612 put_page(page);
6613 if (err == -EACCES)
6614 return -EPERM;
6615out:
6616 return err;
6617}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006618
6619static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006620{
6621 int status;
6622 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006623 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006624 };
6625 struct nfs41_test_stateid_res res;
6626 struct rpc_message msg = {
6627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6628 .rpc_argp = &args,
6629 .rpc_resp = &res,
6630 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006631
Chuck Lever38527b12012-07-11 16:30:23 -04006632 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006633 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006634 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006635 if (status != NFS_OK) {
6636 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006637 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006638 }
6639 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006640 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006641}
6642
Chuck Lever38527b12012-07-11 16:30:23 -04006643/**
6644 * nfs41_test_stateid - perform a TEST_STATEID operation
6645 *
6646 * @server: server / transport on which to perform the operation
6647 * @stateid: state ID to test
6648 *
6649 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6650 * Otherwise a negative NFS4ERR value is returned if the operation
6651 * failed or the state ID is not currently valid.
6652 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006653static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006654{
6655 struct nfs4_exception exception = { };
6656 int err;
6657 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006658 err = _nfs41_test_stateid(server, stateid);
6659 if (err != -NFS4ERR_DELAY)
6660 break;
6661 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006662 } while (exception.retry);
6663 return err;
6664}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006665
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006666static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006667{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006668 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006669 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006670 };
6671 struct nfs41_free_stateid_res res;
6672 struct rpc_message msg = {
6673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6674 .rpc_argp = &args,
6675 .rpc_resp = &res,
6676 };
Chuck Lever38527b12012-07-11 16:30:23 -04006677 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006678
Chuck Lever38527b12012-07-11 16:30:23 -04006679 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006680 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006681 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006682 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006683 dprintk("NFS reply free_stateid: %d\n", status);
6684 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006685}
6686
Chuck Lever38527b12012-07-11 16:30:23 -04006687/**
6688 * nfs41_free_stateid - perform a FREE_STATEID operation
6689 *
6690 * @server: server / transport on which to perform the operation
6691 * @stateid: state ID to release
6692 *
6693 * Returns NFS_OK if the server freed "stateid". Otherwise a
6694 * negative NFS4ERR value is returned.
6695 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006696static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006697{
6698 struct nfs4_exception exception = { };
6699 int err;
6700 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006701 err = _nfs4_free_stateid(server, stateid);
6702 if (err != -NFS4ERR_DELAY)
6703 break;
6704 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006705 } while (exception.retry);
6706 return err;
6707}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006708
6709static bool nfs41_match_stateid(const nfs4_stateid *s1,
6710 const nfs4_stateid *s2)
6711{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006712 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006713 return false;
6714
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006715 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006716 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006717 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006718 return true;
6719
6720 return false;
6721}
6722
Andy Adamson557134a2009-04-01 09:21:53 -04006723#endif /* CONFIG_NFS_V4_1 */
6724
Trond Myklebust36281ca2012-03-04 18:13:56 -05006725static bool nfs4_match_stateid(const nfs4_stateid *s1,
6726 const nfs4_stateid *s2)
6727{
Trond Myklebustf597c532012-03-04 18:13:56 -05006728 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006729}
6730
6731
Trond Myklebust17280172012-03-11 13:11:00 -04006732static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006733 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006734 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 .recover_open = nfs4_open_reclaim,
6736 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006737 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006738 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739};
6740
Andy Adamson591d71c2009-04-01 09:22:47 -04006741#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006742static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006743 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6744 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6745 .recover_open = nfs4_open_reclaim,
6746 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006747 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006748 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006749 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006750};
6751#endif /* CONFIG_NFS_V4_1 */
6752
Trond Myklebust17280172012-03-11 13:11:00 -04006753static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006754 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006755 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 .recover_open = nfs4_open_expired,
6757 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006758 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006759 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760};
6761
Andy Adamson591d71c2009-04-01 09:22:47 -04006762#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006763static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006764 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6765 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006766 .recover_open = nfs41_open_expired,
6767 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006768 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006769 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006770};
6771#endif /* CONFIG_NFS_V4_1 */
6772
Trond Myklebust17280172012-03-11 13:11:00 -04006773static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006774 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006775 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006776 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006777};
6778
6779#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006780static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006781 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006782 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006783 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006784};
6785#endif
6786
Trond Myklebust97dc1352010-06-16 09:52:26 -04006787static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6788 .minor_version = 0,
6789 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006790 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006791 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006792 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6793 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6794 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006795};
6796
6797#if defined(CONFIG_NFS_V4_1)
6798static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6799 .minor_version = 1,
6800 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006801 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006802 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006803 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6804 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6805 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006806};
6807#endif
6808
Trond Myklebust97dc1352010-06-16 09:52:26 -04006809const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6810 [0] = &nfs_v4_0_minor_ops,
6811#if defined(CONFIG_NFS_V4_1)
6812 [1] = &nfs_v4_1_minor_ops,
6813#endif
6814};
6815
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006816static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006817 .permission = nfs_permission,
6818 .getattr = nfs_getattr,
6819 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006820 .getxattr = generic_getxattr,
6821 .setxattr = generic_setxattr,
6822 .listxattr = generic_listxattr,
6823 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006824};
6825
David Howells509de812006-08-22 20:06:11 -04006826const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 .version = 4, /* protocol version */
6828 .dentry_ops = &nfs4_dentry_operations,
6829 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006830 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006831 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006833 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 .getattr = nfs4_proc_getattr,
6835 .setattr = nfs4_proc_setattr,
6836 .lookup = nfs4_proc_lookup,
6837 .access = nfs4_proc_access,
6838 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 .create = nfs4_proc_create,
6840 .remove = nfs4_proc_remove,
6841 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006842 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 .unlink_done = nfs4_proc_unlink_done,
6844 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006845 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006846 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006847 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 .link = nfs4_proc_link,
6849 .symlink = nfs4_proc_symlink,
6850 .mkdir = nfs4_proc_mkdir,
6851 .rmdir = nfs4_proc_remove,
6852 .readdir = nfs4_proc_readdir,
6853 .mknod = nfs4_proc_mknod,
6854 .statfs = nfs4_proc_statfs,
6855 .fsinfo = nfs4_proc_fsinfo,
6856 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006857 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858 .decode_dirent = nfs4_decode_dirent,
6859 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006860 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006861 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006862 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006864 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006865 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006866 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006868 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006869 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006871 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006872 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006873 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006874 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006875 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006876 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006877 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006878 .free_client = nfs4_free_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879};
6880
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006881static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6882 .prefix = XATTR_NAME_NFSV4_ACL,
6883 .list = nfs4_xattr_list_nfs4_acl,
6884 .get = nfs4_xattr_get_nfs4_acl,
6885 .set = nfs4_xattr_set_nfs4_acl,
6886};
6887
6888const struct xattr_handler *nfs4_xattr_handlers[] = {
6889 &nfs4_xattr_nfs4_acl_handler,
6890 NULL
6891};
6892
Trond Myklebustef159e92012-02-06 19:50:40 -05006893module_param(max_session_slots, ushort, 0644);
6894MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6895 "requests the client will negotiate");
6896
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897/*
6898 * Local variables:
6899 * c-basic-offset: 8
6900 * End:
6901 */