blob: 461411171966e417cf8b54a4b9d4ff3f0f4c3981 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010058#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebust4ce79712005-06-22 17:16:21 +000060#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050062#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050063#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040064#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040065#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040066#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustcdd4e682006-01-03 09:55:12 +010075struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010076static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080077static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050079static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050080static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040081static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040082static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040083static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050091static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Trond Myklebust52567b02009-10-23 14:46:42 -040093 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
97 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000098 case -NFS4ERR_WRONGSEC:
99 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
102 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400103 case -NFS4ERR_SHARE_DENIED:
104 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400107 case -NFS4ERR_ACCESS:
108 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700111 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400114 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/*
118 * This is our standard bitmap for GETATTR requests.
119 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400120const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 FATTR4_WORD0_TYPE
122 | FATTR4_WORD0_CHANGE
123 | FATTR4_WORD0_SIZE
124 | FATTR4_WORD0_FSID
125 | FATTR4_WORD0_FILEID,
126 FATTR4_WORD1_MODE
127 | FATTR4_WORD1_NUMLINKS
128 | FATTR4_WORD1_OWNER
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
135};
136
Trond Myklebust1549210f2012-06-05 09:16:47 -0400137static const u32 nfs4_pnfs_open_bitmap[3] = {
138 FATTR4_WORD0_TYPE
139 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_SIZE
141 | FATTR4_WORD0_FSID
142 | FATTR4_WORD0_FILEID,
143 FATTR4_WORD1_MODE
144 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
153};
154
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);
Chuck Lever46a87b82012-07-11 16:30:41 -0400261 if (res)
262 return res;
263
264 if (clp->cl_cons_state < 0)
265 return clp->cl_cons_state;
266 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500267}
268
269static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
270{
271 int res = 0;
272
273 might_sleep();
274
275 if (*timeout <= 0)
276 *timeout = NFS4_POLL_RETRY_MIN;
277 if (*timeout > NFS4_POLL_RETRY_MAX)
278 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100279 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500280 if (fatal_signal_pending(current))
281 res = -ERESTARTSYS;
282 *timeout <<= 1;
283 return res;
284}
285
286/* This is the error handling routine for processes that are allowed
287 * to sleep.
288 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800289static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500290{
291 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500292 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500293 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500294 int ret = errorcode;
295
296 exception->retry = 0;
297 switch(errorcode) {
298 case 0:
299 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500300 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400301 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400302 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500303 exception->retry = 1;
304 return 0;
305 }
306 if (state == NULL)
307 break;
308 nfs4_schedule_stateid_recovery(server, state);
309 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500310 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500311 case -NFS4ERR_ADMIN_REVOKED:
312 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500313 if (state == NULL)
314 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400315 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500316 nfs4_schedule_stateid_recovery(server, state);
317 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400318 case -NFS4ERR_EXPIRED:
319 if (state != NULL)
320 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500321 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500322 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500323 nfs4_schedule_lease_recovery(clp);
324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400326 case -NFS4ERR_BADSESSION:
327 case -NFS4ERR_BADSLOT:
328 case -NFS4ERR_BAD_HIGH_SLOT:
329 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
330 case -NFS4ERR_DEADSESSION:
331 case -NFS4ERR_SEQ_FALSE_RETRY:
332 case -NFS4ERR_SEQ_MISORDERED:
333 dprintk("%s ERROR: %d Reset session\n", __func__,
334 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400335 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400336 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500337 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500338#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500339 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500340 if (exception->timeout > HZ) {
341 /* We have retried a decent amount, time to
342 * fail
343 */
344 ret = -EBUSY;
345 break;
346 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500347 case -NFS4ERR_GRACE:
348 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500349 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500350 ret = nfs4_delay(server->client, &exception->timeout);
351 if (ret != 0)
352 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400353 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500354 case -NFS4ERR_OLD_STATEID:
355 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800356 break;
357 case -NFS4ERR_BADOWNER:
358 /* The following works around a Linux server bug! */
359 case -NFS4ERR_BADNAME:
360 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
361 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
362 exception->retry = 1;
363 printk(KERN_WARNING "NFS: v4 server %s "
364 "does not accept raw "
365 "uid/gids. "
366 "Reenabling the idmapper.\n",
367 server->nfs_client->cl_hostname);
368 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500369 }
370 /* We failed to handle the error */
371 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500372wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500373 ret = nfs4_wait_clnt_recover(clp);
374 if (ret == 0)
375 exception->retry = 1;
376 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500377}
378
379
Trond Myklebust452e9352010-07-31 14:29:06 -0400380static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 spin_lock(&clp->cl_lock);
383 if (time_before(clp->cl_last_renewal,timestamp))
384 clp->cl_last_renewal = timestamp;
385 spin_unlock(&clp->cl_lock);
386}
387
Trond Myklebust452e9352010-07-31 14:29:06 -0400388static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
389{
390 do_renew_lease(server->nfs_client, timestamp);
391}
392
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400393#if defined(CONFIG_NFS_V4_1)
394
Benny Halevy510b8172009-04-01 09:22:14 -0400395/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400396 * nfs4_free_slot - free a slot and efficiently update slot table.
397 *
398 * freeing a slot is trivially done by clearing its respective bit
399 * in the bitmap.
400 * If the freed slotid equals highest_used_slotid we want to update it
401 * so that the server would be able to size down the slot table if needed,
402 * otherwise we know that the highest_used_slotid is still in use.
403 * When updating highest_used_slotid there may be "holes" in the bitmap
404 * so we need to scan down from highest_used_slotid to 0 looking for the now
405 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500406 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 *
408 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400409 */
410static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500411nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400412{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500413 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400414 /* clear used bit in bitmap */
415 __clear_bit(slotid, tbl->used_slots);
416
417 /* update highest_used_slotid when it is freed */
418 if (slotid == tbl->highest_used_slotid) {
419 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500420 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400421 tbl->highest_used_slotid = slotid;
422 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500423 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400424 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500425 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
426 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400427}
428
Trond Myklebust961a8282012-01-17 22:57:37 -0500429bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
430{
431 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
432 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400433}
434
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800435/*
Andy Adamson42acd022011-01-06 02:04:34 +0000436 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800437 */
Andy Adamson42acd022011-01-06 02:04:34 +0000438static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800439{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400440 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500441 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
442 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800443 return;
444 }
445
Trond Myklebust45d43c22012-02-06 19:38:51 -0500446 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800447 return;
448
Andy Adamson42acd022011-01-06 02:04:34 +0000449 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
450 complete(&ses->fc_slot_table.complete);
451}
452
453/*
454 * Signal state manager thread if session back channel is drained
455 */
456void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
457{
458 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500459 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000460 return;
461 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
462 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800463}
464
Trond Myklebustd185a332010-06-16 09:52:25 -0400465static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400466{
467 struct nfs4_slot_table *tbl;
468
Trond Myklebustd185a332010-06-16 09:52:25 -0400469 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400470 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400471 /* just wake up the next guy waiting since
472 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500473 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500474 return;
Andy Adamson13615872009-04-01 09:22:17 -0400475 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500476
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500477 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500478 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000479 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500480 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400481 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400482}
483
Trond Myklebust14516c32010-07-31 14:29:06 -0400484static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400485{
486 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400487 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400488
489 /*
490 * sr_status remains 1 if an RPC level error occurred. The server
491 * may or may not have processed the sequence operation..
492 * Proceed as if the server received and processed the sequence
493 * operation.
494 */
495 if (res->sr_status == 1)
496 res->sr_status = NFS_OK;
497
Bryan Schumaker468f8612011-04-18 15:57:32 -0400498 /* don't increment the sequence number if the task wasn't sent */
499 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400500 goto out;
501
Andy Adamson691daf32009-12-04 15:55:39 -0500502 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400503 switch (res->sr_status) {
504 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400505 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400506 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400507 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400508 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400509 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500510 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500511 if (res->sr_status_flags != 0)
512 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 break;
514 case -NFS4ERR_DELAY:
515 /* The server detected a resend of the RPC call and
516 * returned NFS4ERR_DELAY as per Section 2.10.6.2
517 * of RFC5661.
518 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200519 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400520 __func__,
521 res->sr_slot - res->sr_session->fc_slot_table.slots,
522 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400523 goto out_retry;
524 default:
525 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400526 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400527 }
528out:
529 /* The session may be reset by one of the error handlers. */
530 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400531 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400532 return 1;
533out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400534 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400535 goto out;
536 rpc_delay(task, NFS4_POLL_RETRY_MAX);
537 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400538}
539
Trond Myklebust14516c32010-07-31 14:29:06 -0400540static int nfs4_sequence_done(struct rpc_task *task,
541 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400542{
Trond Myklebust14516c32010-07-31 14:29:06 -0400543 if (res->sr_session == NULL)
544 return 1;
545 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400546}
547
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400548/*
Benny Halevy510b8172009-04-01 09:22:14 -0400549 * nfs4_find_slot - efficiently look for a free slot
550 *
551 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
552 * If found, we mark the slot as used, update the highest_used_slotid,
553 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500554 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555 *
556 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400557 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500558static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800559nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400560{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500561 u32 slotid;
562 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400563
Trond Myklebust45d43c22012-02-06 19:38:51 -0500564 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400565 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
566 tbl->max_slots);
567 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
568 if (slotid >= tbl->max_slots)
569 goto out;
570 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500571 if (slotid > tbl->highest_used_slotid ||
572 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400573 tbl->highest_used_slotid = slotid;
574 ret_id = slotid;
575out:
576 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
577 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400578 return ret_id;
579}
580
Trond Myklebust9d12b212012-01-17 22:04:25 -0500581static void nfs41_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
583{
584 args->sa_session = NULL;
585 args->sa_cache_this = 0;
586 if (cache_reply)
587 args->sa_cache_this = 1;
588 res->sr_session = NULL;
589 res->sr_slot = NULL;
590}
591
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000592int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct nfs4_sequence_args *args,
594 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595 struct rpc_task *task)
596{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400597 struct nfs4_slot *slot;
598 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500599 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600
601 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400603 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 return 0;
605
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400606 tbl = &session->fc_slot_table;
607
608 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400609 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800610 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500611 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500612 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400613 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500614 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500615 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400616 }
617
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800618 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
619 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
620 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
621 spin_unlock(&tbl->slot_tbl_lock);
622 dprintk("%s enforce FIFO order\n", __func__);
623 return -EAGAIN;
624 }
625
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800626 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500627 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400628 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
629 spin_unlock(&tbl->slot_tbl_lock);
630 dprintk("<-- %s: no free slots\n", __func__);
631 return -EAGAIN;
632 }
633 spin_unlock(&tbl->slot_tbl_lock);
634
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800635 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400636 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400637 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400638 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400639
640 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
641
Benny Halevy99fe60d2009-04-01 09:22:29 -0400642 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400643 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400644 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400645 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400646 /*
647 * sr_status is only set in decode_sequence, and so will remain
648 * set to 1 if an rpc level failure occurs.
649 */
650 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400651 return 0;
652}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000653EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654
Trond Myklebust035168a2010-06-16 09:52:26 -0400655int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 struct nfs4_sequence_args *args,
657 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 struct rpc_task *task)
659{
Trond Myklebust035168a2010-06-16 09:52:26 -0400660 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 int ret = 0;
662
Trond Myklebust9d12b212012-01-17 22:04:25 -0500663 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400665
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200666 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400667 __func__, session->clp, session, res->sr_slot ?
668 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400669
Trond Myklebust9d12b212012-01-17 22:04:25 -0500670 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671out:
672 dprintk("<-- %s status=%d\n", __func__, ret);
673 return ret;
674}
675
676struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400677 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678 struct nfs4_sequence_args *seq_args;
679 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680};
681
682static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
683{
684 struct nfs41_call_sync_data *data = calldata;
685
Trond Myklebust035168a2010-06-16 09:52:26 -0400686 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
687
688 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500689 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 return;
691 rpc_call_start(task);
692}
693
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800694static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
695{
696 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
697 nfs41_call_sync_prepare(task, calldata);
698}
699
Andy Adamson69ab40c2009-04-01 09:22:19 -0400700static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
701{
702 struct nfs41_call_sync_data *data = calldata;
703
Trond Myklebust14516c32010-07-31 14:29:06 -0400704 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400705}
706
Trond Myklebust17280172012-03-11 13:11:00 -0400707static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400709 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400710};
711
Trond Myklebust17280172012-03-11 13:11:00 -0400712static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800713 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
714 .rpc_call_done = nfs41_call_sync_done,
715};
716
Bryan Schumaker7c513052011-03-24 17:12:24 +0000717static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
718 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400719 struct rpc_message *msg,
720 struct nfs4_sequence_args *args,
721 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800722 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400723{
724 int ret;
725 struct rpc_task *task;
726 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400727 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400728 .seq_args = args,
729 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400730 };
731 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000732 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400733 .rpc_message = msg,
734 .callback_ops = &nfs41_call_sync_ops,
735 .callback_data = &data
736 };
737
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800738 if (privileged)
739 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400740 task = rpc_run_task(&task_setup);
741 if (IS_ERR(task))
742 ret = PTR_ERR(task);
743 else {
744 ret = task->tk_status;
745 rpc_put_task(task);
746 }
747 return ret;
748}
749
Bryan Schumaker7c513052011-03-24 17:12:24 +0000750int _nfs4_call_sync_session(struct rpc_clnt *clnt,
751 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400752 struct rpc_message *msg,
753 struct nfs4_sequence_args *args,
754 struct nfs4_sequence_res *res,
755 int cache_reply)
756{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500757 nfs41_init_sequence(args, res, cache_reply);
758 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400759}
760
Trond Myklebustdf896452010-06-16 09:52:26 -0400761#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500762static inline
763void nfs41_init_sequence(struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res, int cache_reply)
765{
766}
767
Trond Myklebust14516c32010-07-31 14:29:06 -0400768static int nfs4_sequence_done(struct rpc_task *task,
769 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400770{
Trond Myklebust14516c32010-07-31 14:29:06 -0400771 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400772}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400773#endif /* CONFIG_NFS_V4_1 */
774
Bryan Schumaker7c513052011-03-24 17:12:24 +0000775int _nfs4_call_sync(struct rpc_clnt *clnt,
776 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400777 struct rpc_message *msg,
778 struct nfs4_sequence_args *args,
779 struct nfs4_sequence_res *res,
780 int cache_reply)
781{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500782 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000783 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400784}
785
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000786static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000787int nfs4_call_sync(struct rpc_clnt *clnt,
788 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000789 struct rpc_message *msg,
790 struct nfs4_sequence_args *args,
791 struct nfs4_sequence_res *res,
792 int cache_reply)
793{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000794 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
795 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000796}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400797
Trond Myklebust38478b22006-05-25 01:40:57 -0400798static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Trond Myklebust38478b22006-05-25 01:40:57 -0400800 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Trond Myklebust38478b22006-05-25 01:40:57 -0400802 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400803 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700804 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400805 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700806 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400807 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400811 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 struct nfs_openargs o_arg;
813 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100814 struct nfs_open_confirmargs c_arg;
815 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500816 struct nfs4_string owner_name;
817 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400820 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400822 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100824 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400825 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100826 int rpc_status;
827 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828};
829
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400830
831static void nfs4_init_opendata_res(struct nfs4_opendata *p)
832{
833 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400834 p->o_res.seqid = p->o_arg.seqid;
835 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400836 p->o_res.server = p->o_arg.server;
837 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500838 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400839}
840
Al Viro82a2c1b2011-06-22 18:30:55 -0400841static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400843 const struct iattr *attrs,
844 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845{
Al Viro82a2c1b2011-06-22 18:30:55 -0400846 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847 struct inode *dir = parent->d_inode;
848 struct nfs_server *server = NFS_SERVER(dir);
849 struct nfs4_opendata *p;
850
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400851 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 if (p == NULL)
853 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400854 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 if (p->o_arg.seqid == NULL)
856 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400857 nfs_sb_active(dentry->d_sb);
858 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 p->dir = parent;
860 p->owner = sp;
861 atomic_inc(&sp->so_count);
862 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500863 p->o_arg.open_flags = flags;
864 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400865 /* ask server to check for all possible rights as results are cached */
866 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
867 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
David Howells7539bba2006-08-22 20:06:09 -0400868 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400869 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
870 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400871 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100872 p->o_arg.server = server;
873 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400874 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500876 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500877 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400878
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879 p->o_arg.u.attrs = &p->attrs;
880 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500881
882 verf[0] = jiffies;
883 verf[1] = current->pid;
884 memcpy(p->o_arg.u.verifier.data, verf,
885 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100887 p->c_arg.fh = &p->o_res.fh;
888 p->c_arg.stateid = &p->o_res.stateid;
889 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400890 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400891 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100892 return p;
893err_free:
894 kfree(p);
895err:
896 dput(parent);
897 return NULL;
898}
899
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400900static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100901{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400902 struct nfs4_opendata *p = container_of(kref,
903 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400904 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400905
906 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400907 if (p->state != NULL)
908 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400909 nfs4_put_state_owner(p->owner);
910 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400911 dput(p->dentry);
912 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500913 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400914 kfree(p);
915}
916
917static void nfs4_opendata_put(struct nfs4_opendata *p)
918{
919 if (p != NULL)
920 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100921}
922
Trond Myklebust06f814a2006-01-03 09:55:07 +0100923static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
924{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100925 int ret;
926
Trond Myklebust06f814a2006-01-03 09:55:07 +0100927 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100928 return ret;
929}
930
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400932{
933 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934
Trond Myklebust536e43d2012-01-17 22:04:26 -0500935 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936 goto out;
937 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500939 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
940 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400941 break;
942 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500943 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
944 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400945 break;
946 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500947 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
948 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400949 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400951 return ret;
952}
953
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400955{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500956 if (delegation == NULL)
957 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400959 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500960 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400961 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500962 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400963 return 1;
964}
965
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100967{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100969 case FMODE_WRITE:
970 state->n_wronly++;
971 break;
972 case FMODE_READ:
973 state->n_rdonly++;
974 break;
975 case FMODE_READ|FMODE_WRITE:
976 state->n_rdwr++;
977 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100979}
980
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400982{
983 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500984 nfs4_stateid_copy(&state->stateid, stateid);
985 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400987 case FMODE_READ:
988 set_bit(NFS_O_RDONLY_STATE, &state->flags);
989 break;
990 case FMODE_WRITE:
991 set_bit(NFS_O_WRONLY_STATE, &state->flags);
992 break;
993 case FMODE_READ|FMODE_WRITE:
994 set_bit(NFS_O_RDWR_STATE, &state->flags);
995 }
996}
997
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400999{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001000 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001002 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001003}
1004
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005static 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 -07001006{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001007 /*
1008 * Protect the call to nfs4_state_set_mode_locked and
1009 * serialise the stateid update
1010 */
1011 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001012 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001013 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001014 set_bit(NFS_DELEGATED_STATE, &state->flags);
1015 }
1016 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001018 write_sequnlock(&state->seqlock);
1019 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001021 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024static 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 -05001025{
1026 struct nfs_inode *nfsi = NFS_I(state->inode);
1027 struct nfs_delegation *deleg_cur;
1028 int ret = 0;
1029
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001031
1032 rcu_read_lock();
1033 deleg_cur = rcu_dereference(nfsi->delegation);
1034 if (deleg_cur == NULL)
1035 goto no_delegation;
1036
1037 spin_lock(&deleg_cur->lock);
1038 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001040 goto no_delegation_unlock;
1041
1042 if (delegation == NULL)
1043 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001044 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001045 goto no_delegation_unlock;
1046
Trond Myklebustb7391f42008-12-23 15:21:52 -05001047 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001049 ret = 1;
1050no_delegation_unlock:
1051 spin_unlock(&deleg_cur->lock);
1052no_delegation:
1053 rcu_read_unlock();
1054
1055 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001057 ret = 1;
1058 }
1059
1060 return ret;
1061}
1062
1063
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001064static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001065{
1066 struct nfs_delegation *delegation;
1067
1068 rcu_read_lock();
1069 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071 rcu_read_unlock();
1072 return;
1073 }
1074 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001075 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076}
1077
Trond Myklebust6ee41262007-07-08 14:11:36 -04001078static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001079{
1080 struct nfs4_state *state = opendata->state;
1081 struct nfs_inode *nfsi = NFS_I(state->inode);
1082 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001083 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 nfs4_stateid stateid;
1086 int ret = -EAGAIN;
1087
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001091 if (can_open_cached(state, fmode, open_mode)) {
1092 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001093 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001094 goto out_return_state;
1095 }
1096 spin_unlock(&state->owner->so_lock);
1097 }
Trond Myklebust34310432008-12-23 15:21:38 -05001098 rcu_read_lock();
1099 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001100 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001101 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001102 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001103 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001105 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001106 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001107 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108 if (ret != 0)
1109 goto out;
1110 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001111
1112 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001113 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001114 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001115 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116out:
1117 return ERR_PTR(ret);
1118out_return_state:
1119 atomic_inc(&state->count);
1120 return state;
1121}
1122
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001123static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1124{
1125 struct inode *inode;
1126 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001127 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001128 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129
Trond Myklebustaac00a82007-07-05 19:02:21 -04001130 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001131 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001132 goto out;
1133 }
1134
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001135 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001136 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001137 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001138 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001139 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001140 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001141 goto err;
1142 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001143 state = nfs4_get_open_state(inode, data->owner);
1144 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001145 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001146 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001147 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001148 int delegation_flags = 0;
1149
Trond Myklebust003707c2007-07-05 18:07:55 -04001150 rcu_read_lock();
1151 delegation = rcu_dereference(NFS_I(inode)->delegation);
1152 if (delegation)
1153 delegation_flags = delegation->flags;
1154 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001155 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1156 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1157 "returning a delegation for "
1158 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001159 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001160 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001161 nfs_inode_set_delegation(state->inode,
1162 data->owner->so_cred,
1163 &data->o_res);
1164 else
1165 nfs_inode_reclaim_delegation(state->inode,
1166 data->owner->so_cred,
1167 &data->o_res);
1168 }
Trond Myklebust34310432008-12-23 15:21:38 -05001169
1170 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001171 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001172 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001173out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001174 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001175err_put_inode:
1176 iput(inode);
1177err:
1178 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001179}
1180
Trond Myklebust864472e2006-01-03 09:55:15 +01001181static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1182{
1183 struct nfs_inode *nfsi = NFS_I(state->inode);
1184 struct nfs_open_context *ctx;
1185
1186 spin_lock(&state->inode->i_lock);
1187 list_for_each_entry(ctx, &nfsi->open_files, list) {
1188 if (ctx->state != state)
1189 continue;
1190 get_nfs_open_context(ctx);
1191 spin_unlock(&state->inode->i_lock);
1192 return ctx;
1193 }
1194 spin_unlock(&state->inode->i_lock);
1195 return ERR_PTR(-ENOENT);
1196}
1197
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001198static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1199{
1200 struct nfs4_opendata *opendata;
1201
Al Viro3d4ff432011-06-22 18:40:12 -04001202 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001203 if (opendata == NULL)
1204 return ERR_PTR(-ENOMEM);
1205 opendata->state = state;
1206 atomic_inc(&state->count);
1207 return opendata;
1208}
1209
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001211{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 int ret;
1214
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 opendata->o_arg.open_flags = 0;
1216 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001217 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1218 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1219 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001220 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001221 if (ret != 0)
1222 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001223 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001224 if (IS_ERR(newstate))
1225 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001226 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001227 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 return 0;
1229}
1230
1231static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1232{
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 int ret;
1235
1236 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001237 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001238 smp_rmb();
1239 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001240 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001241 ret = nfs4_open_recover_helper(opendata, FMODE_READ|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_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001248 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001249 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &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 }
1255 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001256 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001257 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001258 if (ret != 0)
1259 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001260 if (newstate != state)
1261 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001262 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001263 /*
1264 * We may have performed cached opens for all three recoveries.
1265 * Check if we need to update the current stateid.
1266 */
1267 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001268 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001269 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001270 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001271 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001272 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001273 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001274 return 0;
1275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277/*
1278 * OPEN_RECLAIM:
1279 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001281static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001283 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001284 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001285 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int status;
1287
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001288 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1289 if (IS_ERR(opendata))
1290 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001291 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1292 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001293 rcu_read_lock();
1294 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001295 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001296 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001297 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001298 opendata->o_arg.u.delegation_type = delegation_type;
1299 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001300 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return status;
1302}
1303
Trond Myklebust539cd032007-06-05 11:46:42 -04001304static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 struct nfs_server *server = NFS_SERVER(state->inode);
1307 struct nfs4_exception exception = { };
1308 int err;
1309 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001310 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001311 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001312 break;
1313 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 } while (exception.retry);
1315 return err;
1316}
1317
Trond Myklebust864472e2006-01-03 09:55:15 +01001318static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1319{
1320 struct nfs_open_context *ctx;
1321 int ret;
1322
1323 ctx = nfs4_state_find_open_context(state);
1324 if (IS_ERR(ctx))
1325 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001326 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001327 put_nfs_open_context(ctx);
1328 return ret;
1329}
1330
Trond Myklebust13437e12007-07-06 15:10:43 -04001331static 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 -07001332{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001333 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001334 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001336 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1337 if (IS_ERR(opendata))
1338 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001339 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001340 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001341 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001342 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001343 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Trond Myklebust13437e12007-07-06 15:10:43 -04001346int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001349 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 int err;
1351 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001352 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 switch (err) {
1354 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001355 case -ENOENT:
1356 case -ESTALE:
1357 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001358 case -NFS4ERR_BADSESSION:
1359 case -NFS4ERR_BADSLOT:
1360 case -NFS4ERR_BAD_HIGH_SLOT:
1361 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1362 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001363 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 case -NFS4ERR_STALE_CLIENTID:
1366 case -NFS4ERR_STALE_STATEID:
1367 case -NFS4ERR_EXPIRED:
1368 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001369 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001370 goto out;
1371 case -ERESTARTSYS:
1372 /*
1373 * The show must go on: exit, but mark the
1374 * stateid as needing recovery.
1375 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001376 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001377 case -NFS4ERR_ADMIN_REVOKED:
1378 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001379 nfs_inode_find_state_and_recover(state->inode,
1380 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001381 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001382 case -EKEYEXPIRED:
1383 /*
1384 * User RPCSEC_GSS context has expired.
1385 * We cannot recover this stateid now, so
1386 * skip it and allow recovery thread to
1387 * proceed.
1388 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001389 case -ENOMEM:
1390 err = 0;
1391 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393 err = nfs4_handle_exception(server, err, &exception);
1394 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001395out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return err;
1397}
1398
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1400{
1401 struct nfs4_opendata *data = calldata;
1402
1403 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001404 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001405 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001406 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001407 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001408 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001409 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001410}
1411
1412static void nfs4_open_confirm_release(void *calldata)
1413{
1414 struct nfs4_opendata *data = calldata;
1415 struct nfs4_state *state = NULL;
1416
1417 /* If this request hasn't been cancelled, do nothing */
1418 if (data->cancelled == 0)
1419 goto out_free;
1420 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001421 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001422 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001423 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001424 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001425 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001426out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001427 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001428}
1429
1430static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001431 .rpc_call_done = nfs4_open_confirm_done,
1432 .rpc_release = nfs4_open_confirm_release,
1433};
1434
1435/*
1436 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1437 */
1438static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1439{
1440 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1441 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001442 struct rpc_message msg = {
1443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1444 .rpc_argp = &data->c_arg,
1445 .rpc_resp = &data->c_res,
1446 .rpc_cred = data->owner->so_cred,
1447 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001448 struct rpc_task_setup task_setup_data = {
1449 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001450 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001451 .callback_ops = &nfs4_open_confirm_ops,
1452 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001453 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001454 .flags = RPC_TASK_ASYNC,
1455 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 int status;
1457
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001458 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001459 data->rpc_done = 0;
1460 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001461 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001462 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001463 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001464 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001465 status = nfs4_wait_for_completion_rpc_task(task);
1466 if (status != 0) {
1467 data->cancelled = 1;
1468 smp_wmb();
1469 } else
1470 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001471 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return status;
1473}
1474
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001477 struct nfs4_opendata *data = calldata;
1478 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001479
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001480 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1481 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001482 /*
1483 * Check if we still need to send an OPEN call, or if we can use
1484 * a delegation instead.
1485 */
1486 if (data->state != NULL) {
1487 struct nfs_delegation *delegation;
1488
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001489 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001490 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001491 rcu_read_lock();
1492 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001493 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1494 can_open_delegated(delegation, data->o_arg.fmode))
1495 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001496 rcu_read_unlock();
1497 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001498 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001499 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001500 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001501 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001502 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1503 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001504 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001505 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001506 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001507 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001508 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001509 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001510 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001511unlock_no_action:
1512 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001513out_no_action:
1514 task->tk_action = NULL;
1515
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001518static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1519{
1520 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1521 nfs4_open_prepare(task, calldata);
1522}
1523
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524static void nfs4_open_done(struct rpc_task *task, void *calldata)
1525{
1526 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001529
Trond Myklebust14516c32010-07-31 14:29:06 -04001530 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1531 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001532
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 if (task->tk_status == 0) {
1534 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001535 case S_IFREG:
1536 break;
1537 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001539 break;
1540 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001541 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001542 break;
1543 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001545 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001546 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001547 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1548 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001549 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001550 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001552
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001553static void nfs4_open_release(void *calldata)
1554{
1555 struct nfs4_opendata *data = calldata;
1556 struct nfs4_state *state = NULL;
1557
1558 /* If this request hasn't been cancelled, do nothing */
1559 if (data->cancelled == 0)
1560 goto out_free;
1561 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001562 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001563 goto out_free;
1564 /* In case we need an open_confirm, no cleanup! */
1565 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1566 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001567 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001568 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001569 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001570out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001571 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001572}
1573
1574static const struct rpc_call_ops nfs4_open_ops = {
1575 .rpc_call_prepare = nfs4_open_prepare,
1576 .rpc_call_done = nfs4_open_done,
1577 .rpc_release = nfs4_open_release,
1578};
1579
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001580static const struct rpc_call_ops nfs4_recover_open_ops = {
1581 .rpc_call_prepare = nfs4_recover_open_prepare,
1582 .rpc_call_done = nfs4_open_done,
1583 .rpc_release = nfs4_open_release,
1584};
1585
1586static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001587{
1588 struct inode *dir = data->dir->d_inode;
1589 struct nfs_server *server = NFS_SERVER(dir);
1590 struct nfs_openargs *o_arg = &data->o_arg;
1591 struct nfs_openres *o_res = &data->o_res;
1592 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001593 struct rpc_message msg = {
1594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1595 .rpc_argp = o_arg,
1596 .rpc_resp = o_res,
1597 .rpc_cred = data->owner->so_cred,
1598 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001599 struct rpc_task_setup task_setup_data = {
1600 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001601 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001602 .callback_ops = &nfs4_open_ops,
1603 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001604 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001605 .flags = RPC_TASK_ASYNC,
1606 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001607 int status;
1608
Trond Myklebust9d12b212012-01-17 22:04:25 -05001609 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001610 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001611 data->rpc_done = 0;
1612 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001613 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001614 if (isrecover)
1615 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001616 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001617 if (IS_ERR(task))
1618 return PTR_ERR(task);
1619 status = nfs4_wait_for_completion_rpc_task(task);
1620 if (status != 0) {
1621 data->cancelled = 1;
1622 smp_wmb();
1623 } else
1624 status = data->rpc_status;
1625 rpc_put_task(task);
1626
1627 return status;
1628}
1629
1630static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1631{
1632 struct inode *dir = data->dir->d_inode;
1633 struct nfs_openres *o_res = &data->o_res;
1634 int status;
1635
1636 status = nfs4_run_open_task(data, 1);
1637 if (status != 0 || !data->rpc_done)
1638 return status;
1639
Trond Myklebust6926afd2012-01-07 13:22:46 -05001640 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1641
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001642 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1643 status = _nfs4_proc_open_confirm(data);
1644 if (status != 0)
1645 return status;
1646 }
1647
1648 return status;
1649}
1650
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001651static int nfs4_opendata_access(struct rpc_cred *cred,
1652 struct nfs4_opendata *opendata,
1653 struct nfs4_state *state, fmode_t fmode)
1654{
1655 struct nfs_access_entry cache;
1656 u32 mask;
1657
1658 /* access call failed or for some reason the server doesn't
1659 * support any access modes -- defer access call until later */
1660 if (opendata->o_res.access_supported == 0)
1661 return 0;
1662
1663 mask = 0;
1664 if (fmode & FMODE_READ)
1665 mask |= MAY_READ;
1666 if (fmode & FMODE_WRITE)
1667 mask |= MAY_WRITE;
1668 if (fmode & FMODE_EXEC)
1669 mask |= MAY_EXEC;
1670
1671 cache.cred = cred;
1672 cache.jiffies = jiffies;
1673 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1674 nfs_access_add_cache(state->inode, &cache);
1675
1676 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1677 return 0;
1678
1679 /* even though OPEN succeeded, access is denied. Close the file */
1680 nfs4_close_state(state, fmode);
1681 return -NFS4ERR_ACCESS;
1682}
1683
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001684/*
1685 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1686 */
1687static int _nfs4_proc_open(struct nfs4_opendata *data)
1688{
1689 struct inode *dir = data->dir->d_inode;
1690 struct nfs_server *server = NFS_SERVER(dir);
1691 struct nfs_openargs *o_arg = &data->o_arg;
1692 struct nfs_openres *o_res = &data->o_res;
1693 int status;
1694
1695 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001696 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001697 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001698 if (status != 0) {
1699 if (status == -NFS4ERR_BADNAME &&
1700 !(o_arg->open_flags & O_CREAT))
1701 return -ENOENT;
1702 return status;
1703 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001704
Trond Myklebust6926afd2012-01-07 13:22:46 -05001705 nfs_fattr_map_and_free_names(server, &data->f_attr);
1706
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001707 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001708 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001709 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1710 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001712 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001714 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001717 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Andy Adamsond83217c2011-03-01 01:34:17 +00001721static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001722{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001723 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001724 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001725
Trond Myklebusta78cb572009-08-09 15:06:19 -04001726 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001727 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001728 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001729 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001730 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1731 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001732 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001733 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001734 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001735 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001736 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001737}
1738
Andy Adamsond83217c2011-03-01 01:34:17 +00001739static int nfs4_recover_expired_lease(struct nfs_server *server)
1740{
1741 return nfs4_client_recover_expired_lease(server->nfs_client);
1742}
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744/*
1745 * OPEN_EXPIRED:
1746 * reclaim state on the server after a network partition.
1747 * Assumes caller holds the appropriate lock
1748 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001749static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001751 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001752 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001754 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1755 if (IS_ERR(opendata))
1756 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001757 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001758 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001759 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001760 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001761 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001764static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001765{
Trond Myklebust539cd032007-06-05 11:46:42 -04001766 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001767 struct nfs4_exception exception = { };
1768 int err;
1769
1770 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001771 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001772 switch (err) {
1773 default:
1774 goto out;
1775 case -NFS4ERR_GRACE:
1776 case -NFS4ERR_DELAY:
1777 nfs4_handle_exception(server, err, &exception);
1778 err = 0;
1779 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001780 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001781out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001782 return err;
1783}
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1786{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001788 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Trond Myklebust864472e2006-01-03 09:55:15 +01001790 ctx = nfs4_state_find_open_context(state);
1791 if (IS_ERR(ctx))
1792 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001793 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001794 put_nfs_open_context(ctx);
1795 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001798#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001799static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001800{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001801 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001802 nfs4_stateid *stateid = &state->stateid;
1803 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001804
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001805 /* If a state reset has been done, test_stateid is unneeded */
1806 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1807 return;
1808
1809 status = nfs41_test_stateid(server, stateid);
1810 if (status != NFS_OK) {
1811 /* Free the stateid unless the server explicitly
1812 * informs us the stateid is unrecognized. */
1813 if (status != -NFS4ERR_BAD_STATEID)
1814 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001815 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001816
Bryan Schumaker69388672012-09-26 15:25:52 -04001817 write_seqlock(&state->seqlock);
1818 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1819 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001820 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1821 }
1822}
1823
1824/**
1825 * nfs41_check_open_stateid - possibly free an open stateid
1826 *
1827 * @state: NFSv4 state for an inode
1828 *
1829 * Returns NFS_OK if recovery for this stateid is now finished.
1830 * Otherwise a negative NFS4ERR value is returned.
1831 */
1832static int nfs41_check_open_stateid(struct nfs4_state *state)
1833{
1834 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001835 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001836 int status;
1837
1838 /* If a state reset has been done, test_stateid is unneeded */
1839 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1840 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1841 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1842 return -NFS4ERR_BAD_STATEID;
1843
1844 status = nfs41_test_stateid(server, stateid);
1845 if (status != NFS_OK) {
1846 /* Free the stateid unless the server explicitly
1847 * informs us the stateid is unrecognized. */
1848 if (status != -NFS4ERR_BAD_STATEID)
1849 nfs41_free_stateid(server, stateid);
1850
1851 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1852 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1853 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001854 }
1855 return status;
1856}
1857
1858static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1859{
Chuck Levereb64cf92012-07-11 16:30:05 -04001860 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001861
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001862 nfs41_clear_delegation_stateid(state);
1863 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001864 if (status != NFS_OK)
1865 status = nfs4_open_expired(sp, state);
1866 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001867}
1868#endif
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001871 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1872 * fields corresponding to attributes that were used to store the verifier.
1873 * Make sure we clobber those fields in the later setattr call
1874 */
1875static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1876{
1877 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1878 !(sattr->ia_valid & ATTR_ATIME_SET))
1879 sattr->ia_valid |= ATTR_ATIME;
1880
1881 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1882 !(sattr->ia_valid & ATTR_MTIME_SET))
1883 sattr->ia_valid |= ATTR_MTIME;
1884}
1885
1886/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001887 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 */
Andy Adamson82be4172012-05-23 05:02:35 -04001889static int _nfs4_do_open(struct inode *dir,
1890 struct dentry *dentry,
1891 fmode_t fmode,
1892 int flags,
1893 struct iattr *sattr,
1894 struct rpc_cred *cred,
1895 struct nfs4_state **res,
1896 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct nfs4_state_owner *sp;
1899 struct nfs4_state *state = NULL;
1900 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001901 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001902 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001906 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1907 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1909 goto out_err;
1910 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001911 status = nfs4_recover_expired_lease(server);
1912 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001913 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001914 if (dentry->d_inode != NULL)
1915 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001916 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001917 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001918 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001919 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Andy Adamson82be4172012-05-23 05:02:35 -04001921 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1922 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1923 if (!opendata->f_attr.mdsthreshold)
1924 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001925 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001926 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001927 if (dentry->d_inode != NULL)
1928 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001929
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001930 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001932 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001934 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001935 status = PTR_ERR(state);
1936 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001937 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001938 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001939 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001940
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001941 status = nfs4_opendata_access(cred, opendata, state, fmode);
1942 if (status != 0)
1943 goto err_opendata_put;
1944
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001945 if (opendata->o_arg.open_flags & O_EXCL) {
1946 nfs4_exclusive_attrset(opendata, sattr);
1947
1948 nfs_fattr_init(opendata->o_res.f_attr);
1949 status = nfs4_do_setattr(state->inode, cred,
1950 opendata->o_res.f_attr, sattr,
1951 state);
1952 if (status == 0)
1953 nfs_setattr_update_inode(state->inode, sattr);
1954 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1955 }
Andy Adamson82be4172012-05-23 05:02:35 -04001956
1957 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1958 *ctx_th = opendata->f_attr.mdsthreshold;
1959 else
1960 kfree(opendata->f_attr.mdsthreshold);
1961 opendata->f_attr.mdsthreshold = NULL;
1962
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001963 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 *res = state;
1966 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001967err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001968 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001969 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001970err_put_state_owner:
1971 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 *res = NULL;
1974 return status;
1975}
1976
1977
Andy Adamson82be4172012-05-23 05:02:35 -04001978static struct nfs4_state *nfs4_do_open(struct inode *dir,
1979 struct dentry *dentry,
1980 fmode_t fmode,
1981 int flags,
1982 struct iattr *sattr,
1983 struct rpc_cred *cred,
1984 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 struct nfs4_exception exception = { };
1987 struct nfs4_state *res;
1988 int status;
1989
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001990 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001992 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1993 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (status == 0)
1995 break;
1996 /* NOTE: BAD_SEQID means the server and client disagree about the
1997 * book-keeping w.r.t. state-changing operations
1998 * (OPEN/CLOSE/LOCK/LOCKU...)
1999 * It is actually a sign of a bug on the client or on the server.
2000 *
2001 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002002 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 * have unhashed the old state_owner for us, and that we can
2004 * therefore safely retry using a new one. We should still warn
2005 * the user though...
2006 */
2007 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002008 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002009 " returned a bad sequence-id error!\n",
2010 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 exception.retry = 1;
2012 continue;
2013 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002014 /*
2015 * BAD_STATEID on OPEN means that the server cancelled our
2016 * state before it received the OPEN_CONFIRM.
2017 * Recover by retrying the request as per the discussion
2018 * on Page 181 of RFC3530.
2019 */
2020 if (status == -NFS4ERR_BAD_STATEID) {
2021 exception.retry = 1;
2022 continue;
2023 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002024 if (status == -EAGAIN) {
2025 /* We must have found a delegation */
2026 exception.retry = 1;
2027 continue;
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2030 status, &exception));
2031 } while (exception.retry);
2032 return res;
2033}
2034
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002035static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2036 struct nfs_fattr *fattr, struct iattr *sattr,
2037 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002039 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002041 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 .iap = sattr,
2043 .server = server,
2044 .bitmask = server->attr_bitmask,
2045 };
2046 struct nfs_setattrres res = {
2047 .fattr = fattr,
2048 .server = server,
2049 };
2050 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2052 .rpc_argp = &arg,
2053 .rpc_resp = &res,
2054 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002056 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002057 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Trond Myklebust0e574af2005-10-27 22:12:38 -04002059 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Trond Myklebust4fc87962012-03-08 17:42:01 -05002061 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002062 struct nfs_lockowner lockowner = {
2063 .l_owner = current->files,
2064 .l_pid = current->tgid,
2065 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002066 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002067 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002068 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2069 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002070 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002071 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002072 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Bryan Schumaker7c513052011-03-24 17:12:24 +00002074 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002075 if (status == 0 && state != NULL)
2076 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002077 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002080static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2081 struct nfs_fattr *fattr, struct iattr *sattr,
2082 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002084 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002085 struct nfs4_exception exception = {
2086 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002087 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002088 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 int err;
2090 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002091 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2092 switch (err) {
2093 case -NFS4ERR_OPENMODE:
2094 if (state && !(state->state & FMODE_WRITE)) {
2095 err = -EBADF;
2096 if (sattr->ia_valid & ATTR_OPEN)
2097 err = -EACCES;
2098 goto out;
2099 }
2100 }
2101 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002103out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return err;
2105}
2106
2107struct nfs4_closedata {
2108 struct inode *inode;
2109 struct nfs4_state *state;
2110 struct nfs_closeargs arg;
2111 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002112 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002113 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002114 bool roc;
2115 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116};
2117
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002118static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002119{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002120 struct nfs4_closedata *calldata = data;
2121 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002122 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002123
Fred Isamanf7e89172011-01-06 11:36:32 +00002124 if (calldata->roc)
2125 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002126 nfs4_put_open_state(calldata->state);
2127 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002128 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002129 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002130 kfree(calldata);
2131}
2132
Trond Myklebust88069f72009-12-08 08:33:16 -05002133static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2134 fmode_t fmode)
2135{
2136 spin_lock(&state->owner->so_lock);
2137 if (!(fmode & FMODE_READ))
2138 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2139 if (!(fmode & FMODE_WRITE))
2140 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2141 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2142 spin_unlock(&state->owner->so_lock);
2143}
2144
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002145static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002147 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 struct nfs_server *server = NFS_SERVER(calldata->inode);
2150
Chuck Levera3ca5652012-03-01 17:00:40 -05002151 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002152 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2153 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /* hmm. we are done with the inode, and in the process of freeing
2155 * the state_owner. we keep this around to process errors
2156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 switch (task->tk_status) {
2158 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002159 if (calldata->roc)
2160 pnfs_roc_set_barrier(state->inode,
2161 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002162 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002163 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002164 nfs4_close_clear_stateid_flags(state,
2165 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 break;
2167 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002168 case -NFS4ERR_OLD_STATEID:
2169 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002171 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002174 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2175 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002177 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002178 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002179 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002182static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002184 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002185 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002186 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002187 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002188
Chuck Levera3ca5652012-03-01 17:00:40 -05002189 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002190 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002191 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002192
Trond Myklebust88069f72009-12-08 08:33:16 -05002193 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2194 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002195 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002196 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002197 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002198 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002199 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2200 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2201 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002202 }
2203 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002204 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2205 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2206 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002207 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002208 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002209 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002210
2211 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002212 /* Note: exit _without_ calling nfs4_close_done */
2213 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002214 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002215 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002216
Fred Isamanf7e89172011-01-06 11:36:32 +00002217 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002218 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002219 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002220 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002221 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002222 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002223
Trond Myklebust516a6af2005-10-27 22:12:41 -04002224 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002225 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002226 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002227 &calldata->arg.seq_args,
2228 &calldata->res.seq_res,
2229 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002230 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002231 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002232out:
2233 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002236static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002237 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002238 .rpc_call_done = nfs4_close_done,
2239 .rpc_release = nfs4_free_closedata,
2240};
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242/*
2243 * It is possible for data to be read/written from a mem-mapped file
2244 * after the sys_close call (which hits the vfs layer as a flush).
2245 * This means that we can't safely call nfsv4 close on a file until
2246 * the inode is cleared. This in turn means that we are not good
2247 * NFSv4 citizens - we do not indicate to the server to update the file's
2248 * share state even when we are done with one of the three share
2249 * stateid's in the inode.
2250 *
2251 * NOTE: Caller must be holding the sp->so_owner semaphore!
2252 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002253int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002255 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002257 struct nfs4_state_owner *sp = state->owner;
2258 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002259 struct rpc_message msg = {
2260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2261 .rpc_cred = state->owner->so_cred,
2262 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002263 struct rpc_task_setup task_setup_data = {
2264 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002265 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002266 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002267 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002268 .flags = RPC_TASK_ASYNC,
2269 };
Trond Myklebust95121352005-10-18 14:20:12 -07002270 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002272 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002274 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002275 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002276 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002278 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002279 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002281 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002282 if (calldata->arg.seqid == NULL)
2283 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002284 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002285 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002286 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002287 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002288 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002289 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002290 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002291
Trond Myklebust1174dd12010-12-21 10:52:24 -05002292 msg.rpc_argp = &calldata->arg;
2293 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002294 task_setup_data.callback_data = calldata;
2295 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002296 if (IS_ERR(task))
2297 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002298 status = 0;
2299 if (wait)
2300 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002301 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002302 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002303out_free_calldata:
2304 kfree(calldata);
2305out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002306 nfs4_put_open_state(state);
2307 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002308 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Trond Myklebust2b484292010-09-17 10:56:51 -04002311static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002312nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 struct nfs4_state *state;
2315
Trond Myklebust565277f2007-10-15 18:17:53 -04002316 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002317 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2318 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002319 if (IS_ERR(state))
2320 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002321 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002322 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Trond Myklebust1185a552009-12-03 15:54:02 -05002325static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002326{
2327 if (ctx->state == NULL)
2328 return;
2329 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002330 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002331 else
Al Viro643168c2011-06-22 18:20:23 -04002332 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002333}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2336{
Benny Halevy43652ad2009-04-01 09:21:54 -04002337 struct nfs4_server_caps_arg args = {
2338 .fhandle = fhandle,
2339 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 struct nfs4_server_caps_res res = {};
2341 struct rpc_message msg = {
2342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002343 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 .rpc_resp = &res,
2345 };
2346 int status;
2347
Bryan Schumaker7c513052011-03-24 17:12:24 +00002348 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (status == 0) {
2350 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002351 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2352 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2353 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2354 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2355 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2357 server->caps |= NFS_CAP_ACLS;
2358 if (res.has_links != 0)
2359 server->caps |= NFS_CAP_HARDLINKS;
2360 if (res.has_symlinks != 0)
2361 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002362 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2363 server->caps |= NFS_CAP_FILEID;
2364 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2365 server->caps |= NFS_CAP_MODE;
2366 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2367 server->caps |= NFS_CAP_NLINK;
2368 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2369 server->caps |= NFS_CAP_OWNER;
2370 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2371 server->caps |= NFS_CAP_OWNER_GROUP;
2372 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2373 server->caps |= NFS_CAP_ATIME;
2374 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2375 server->caps |= NFS_CAP_CTIME;
2376 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2377 server->caps |= NFS_CAP_MTIME;
2378
Trond Myklebusta65318b2009-03-11 14:10:28 -04002379 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2380 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2381 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002383 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return status;
2387}
2388
Trond Myklebust55a97592006-06-09 09:34:19 -04002389int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
2391 struct nfs4_exception exception = { };
2392 int err;
2393 do {
2394 err = nfs4_handle_exception(server,
2395 _nfs4_server_capabilities(server, fhandle),
2396 &exception);
2397 } while (exception.retry);
2398 return err;
2399}
2400
2401static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2402 struct nfs_fsinfo *info)
2403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 struct nfs4_lookup_root_arg args = {
2405 .bitmask = nfs4_fattr_bitmap,
2406 };
2407 struct nfs4_lookup_res res = {
2408 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002409 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 .fh = fhandle,
2411 };
2412 struct rpc_message msg = {
2413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2414 .rpc_argp = &args,
2415 .rpc_resp = &res,
2416 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002417
Trond Myklebust0e574af2005-10-27 22:12:38 -04002418 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002419 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
2422static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2423 struct nfs_fsinfo *info)
2424{
2425 struct nfs4_exception exception = { };
2426 int err;
2427 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002428 err = _nfs4_lookup_root(server, fhandle, info);
2429 switch (err) {
2430 case 0:
2431 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002432 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002433 default:
2434 err = nfs4_handle_exception(server, err, &exception);
2435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002437out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return err;
2439}
2440
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002441static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2442 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2443{
2444 struct rpc_auth *auth;
2445 int ret;
2446
2447 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002448 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002449 ret = -EIO;
2450 goto out;
2451 }
2452 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002453out:
2454 return ret;
2455}
2456
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002457static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002458 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002460 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002461 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002463 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2464 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002465
2466 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002467 /* AUTH_UNIX is the default flavor if none was specified,
2468 * thus has already been tried. */
2469 if (flav_array[i] == RPC_AUTH_UNIX)
2470 continue;
2471
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002472 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002473 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002474 continue;
2475 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002476 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002477 /*
2478 * -EACCESS could mean that the user doesn't have correct permissions
2479 * to access the mount. It could also mean that we tried to mount
2480 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2481 * existing mount programs don't handle -EACCES very well so it should
2482 * be mapped to -EPERM instead.
2483 */
2484 if (status == -EACCES)
2485 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002486 return status;
2487}
2488
2489/*
2490 * get the file handle for the "/" directory on the server
2491 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002492int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2493 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002494{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002495 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002496 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002497 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2498 /*
2499 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2500 * by nfs4_map_errors() as this function exits.
2501 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002502 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (status == 0)
2504 status = nfs4_server_capabilities(server, fhandle);
2505 if (status == 0)
2506 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002507 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
Bryan Schumakerbae36242012-05-10 15:07:31 -04002510static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2511 struct nfs_fsinfo *info)
2512{
2513 int error;
2514 struct nfs_fattr *fattr = info->fattr;
2515
2516 error = nfs4_server_capabilities(server, mntfh);
2517 if (error < 0) {
2518 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2519 return error;
2520 }
2521
2522 error = nfs4_proc_getattr(server, mntfh, fattr);
2523 if (error < 0) {
2524 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2525 return error;
2526 }
2527
2528 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2529 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2530 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2531
2532 return error;
2533}
2534
Manoj Naik6b97fd32006-06-09 09:34:29 -04002535/*
2536 * Get locations and (maybe) other attributes of a referral.
2537 * Note that we'll actually follow the referral later when
2538 * we detect fsid mismatch in inode revalidation
2539 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002540static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2541 const struct qstr *name, struct nfs_fattr *fattr,
2542 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002543{
2544 int status = -ENOMEM;
2545 struct page *page = NULL;
2546 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002547
2548 page = alloc_page(GFP_KERNEL);
2549 if (page == NULL)
2550 goto out;
2551 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2552 if (locations == NULL)
2553 goto out;
2554
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002555 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002556 if (status != 0)
2557 goto out;
2558 /* Make sure server returned a different fsid for the referral */
2559 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002560 dprintk("%s: server did not return a different fsid for"
2561 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002562 status = -EIO;
2563 goto out;
2564 }
Andy Adamson533eb462011-06-13 18:25:56 -04002565 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2566 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002567
Andy Adamson533eb462011-06-13 18:25:56 -04002568 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002569 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002570 memset(fhandle, 0, sizeof(struct nfs_fh));
2571out:
2572 if (page)
2573 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002574 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002575 return status;
2576}
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2579{
2580 struct nfs4_getattr_arg args = {
2581 .fh = fhandle,
2582 .bitmask = server->attr_bitmask,
2583 };
2584 struct nfs4_getattr_res res = {
2585 .fattr = fattr,
2586 .server = server,
2587 };
2588 struct rpc_message msg = {
2589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2590 .rpc_argp = &args,
2591 .rpc_resp = &res,
2592 };
2593
Trond Myklebust0e574af2005-10-27 22:12:38 -04002594 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002595 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
2598static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2599{
2600 struct nfs4_exception exception = { };
2601 int err;
2602 do {
2603 err = nfs4_handle_exception(server,
2604 _nfs4_proc_getattr(server, fhandle, fattr),
2605 &exception);
2606 } while (exception.retry);
2607 return err;
2608}
2609
2610/*
2611 * The file is not closed if it is opened due to the a request to change
2612 * the size of the file. The open call will not be needed once the
2613 * VFS layer lookup-intents are implemented.
2614 *
2615 * Close is called when the inode is destroyed.
2616 * If we haven't opened the file for O_WRONLY, we
2617 * need to in the size_change case to obtain a stateid.
2618 *
2619 * Got race?
2620 * Because OPEN is always done by name in nfsv4, it is
2621 * possible that we opened a different file by the same
2622 * name. We can recognize this race condition, but we
2623 * can't do anything about it besides returning an error.
2624 *
2625 * This will be fixed with VFS changes (lookup-intent).
2626 */
2627static int
2628nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2629 struct iattr *sattr)
2630{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002631 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002632 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002633 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 int status;
2635
Benny Halevy8a1636c2010-07-14 15:43:57 -04002636 if (pnfs_ld_layoutret_on_setattr(inode))
2637 pnfs_return_layout(inode);
2638
Trond Myklebust0e574af2005-10-27 22:12:38 -04002639 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Andy Adamson26699402012-05-30 16:12:24 -04002641 /* Deal with open(O_TRUNC) */
2642 if (sattr->ia_valid & ATTR_OPEN)
2643 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2644
2645 /* Optimization: if the end result is no change, don't RPC */
2646 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2647 return 0;
2648
Trond Myklebustd5308382005-11-04 15:33:38 -05002649 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002650 if (sattr->ia_valid & ATTR_FILE) {
2651 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002652
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002653 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002654 if (ctx) {
2655 cred = ctx->cred;
2656 state = ctx->state;
2657 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002658 }
2659
2660 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002661 if (status == 0)
2662 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return status;
2664}
2665
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002666static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2667 const struct qstr *name, struct nfs_fh *fhandle,
2668 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002669{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002670 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002671 int status;
2672 struct nfs4_lookup_arg args = {
2673 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002674 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002675 .name = name,
2676 };
2677 struct nfs4_lookup_res res = {
2678 .server = server,
2679 .fattr = fattr,
2680 .fh = fhandle,
2681 };
2682 struct rpc_message msg = {
2683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2684 .rpc_argp = &args,
2685 .rpc_resp = &res,
2686 };
2687
2688 nfs_fattr_init(fattr);
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002691 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 dprintk("NFS reply lookup: %d\n", status);
2693 return status;
2694}
2695
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002696static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002697{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002698 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002699 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002700 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2701 fattr->nlink = 2;
2702}
2703
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002704static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2705 struct qstr *name, struct nfs_fh *fhandle,
2706 struct nfs_fattr *fattr)
2707{
2708 struct nfs4_exception exception = { };
2709 struct rpc_clnt *client = *clnt;
2710 int err;
2711 do {
2712 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2713 switch (err) {
2714 case -NFS4ERR_BADNAME:
2715 err = -ENOENT;
2716 goto out;
2717 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002718 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002719 goto out;
2720 case -NFS4ERR_WRONGSEC:
2721 err = -EPERM;
2722 if (client != *clnt)
2723 goto out;
2724
2725 client = nfs4_create_sec_client(client, dir, name);
2726 if (IS_ERR(client))
2727 return PTR_ERR(client);
2728
2729 exception.retry = 1;
2730 break;
2731 default:
2732 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2733 }
2734 } while (exception.retry);
2735
2736out:
2737 if (err == 0)
2738 *clnt = client;
2739 else if (client != *clnt)
2740 rpc_shutdown_client(client);
2741
2742 return err;
2743}
2744
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002745static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002746 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002748 int status;
2749 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002750
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002751 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2752 if (client != NFS_CLIENT(dir)) {
2753 rpc_shutdown_client(client);
2754 nfs_fixup_secinfo_attributes(fattr);
2755 }
2756 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002759struct rpc_clnt *
2760nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2761 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2762{
2763 int status;
2764 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2765
2766 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2767 if (status < 0) {
2768 rpc_shutdown_client(client);
2769 return ERR_PTR(status);
2770 }
2771 return client;
2772}
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2775{
Trond Myklebust76b32992007-08-10 17:45:11 -04002776 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 struct nfs4_accessargs args = {
2778 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002779 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002781 struct nfs4_accessres res = {
2782 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002783 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 struct rpc_message msg = {
2785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2786 .rpc_argp = &args,
2787 .rpc_resp = &res,
2788 .rpc_cred = entry->cred,
2789 };
2790 int mode = entry->mask;
2791 int status;
2792
2793 /*
2794 * Determine which access bits we want to ask for...
2795 */
2796 if (mode & MAY_READ)
2797 args.access |= NFS4_ACCESS_READ;
2798 if (S_ISDIR(inode->i_mode)) {
2799 if (mode & MAY_WRITE)
2800 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2801 if (mode & MAY_EXEC)
2802 args.access |= NFS4_ACCESS_LOOKUP;
2803 } else {
2804 if (mode & MAY_WRITE)
2805 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2806 if (mode & MAY_EXEC)
2807 args.access |= NFS4_ACCESS_EXECUTE;
2808 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002809
2810 res.fattr = nfs_alloc_fattr();
2811 if (res.fattr == NULL)
2812 return -ENOMEM;
2813
Bryan Schumaker7c513052011-03-24 17:12:24 +00002814 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002816 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002817 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002819 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return status;
2821}
2822
2823static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2824{
2825 struct nfs4_exception exception = { };
2826 int err;
2827 do {
2828 err = nfs4_handle_exception(NFS_SERVER(inode),
2829 _nfs4_proc_access(inode, entry),
2830 &exception);
2831 } while (exception.retry);
2832 return err;
2833}
2834
2835/*
2836 * TODO: For the time being, we don't try to get any attributes
2837 * along with any of the zero-copy operations READ, READDIR,
2838 * READLINK, WRITE.
2839 *
2840 * In the case of the first three, we want to put the GETATTR
2841 * after the read-type operation -- this is because it is hard
2842 * to predict the length of a GETATTR response in v4, and thus
2843 * align the READ data correctly. This means that the GETATTR
2844 * may end up partially falling into the page cache, and we should
2845 * shift it into the 'tail' of the xdr_buf before processing.
2846 * To do this efficiently, we need to know the total length
2847 * of data received, which doesn't seem to be available outside
2848 * of the RPC layer.
2849 *
2850 * In the case of WRITE, we also want to put the GETATTR after
2851 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002852 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 *
2854 * Both of these changes to the XDR layer would in fact be quite
2855 * minor, but I decided to leave them for a subsequent patch.
2856 */
2857static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2858 unsigned int pgbase, unsigned int pglen)
2859{
2860 struct nfs4_readlink args = {
2861 .fh = NFS_FH(inode),
2862 .pgbase = pgbase,
2863 .pglen = pglen,
2864 .pages = &page,
2865 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002866 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 struct rpc_message msg = {
2868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2869 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002870 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 };
2872
Bryan Schumaker7c513052011-03-24 17:12:24 +00002873 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 -07002874}
2875
2876static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2877 unsigned int pgbase, unsigned int pglen)
2878{
2879 struct nfs4_exception exception = { };
2880 int err;
2881 do {
2882 err = nfs4_handle_exception(NFS_SERVER(inode),
2883 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2884 &exception);
2885 } while (exception.retry);
2886 return err;
2887}
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002890 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892static int
2893nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002894 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002896 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 int status = 0;
2899
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002900 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2901 if (IS_ERR(ctx))
2902 return PTR_ERR(ctx);
2903
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002904 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002905 state = nfs4_do_open(dir, dentry, ctx->mode,
2906 flags, sattr, ctx->cred,
2907 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002908 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (IS_ERR(state)) {
2910 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002911 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002913 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002914 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002915 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002917 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return status;
2919}
2920
2921static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2922{
Trond Myklebust16e42952005-10-27 22:12:44 -04002923 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002924 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002926 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002928 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002929 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002930 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2933 .rpc_argp = &args,
2934 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002936 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002937
Bryan Schumaker7c513052011-03-24 17:12:24 +00002938 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002939 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002940 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return status;
2942}
2943
2944static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2945{
2946 struct nfs4_exception exception = { };
2947 int err;
2948 do {
2949 err = nfs4_handle_exception(NFS_SERVER(dir),
2950 _nfs4_proc_remove(dir, name),
2951 &exception);
2952 } while (exception.retry);
2953 return err;
2954}
2955
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002956static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002958 struct nfs_server *server = NFS_SERVER(dir);
2959 struct nfs_removeargs *args = msg->rpc_argp;
2960 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002962 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002964 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002967static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2968{
2969 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2970 &data->args.seq_args,
2971 &data->res.seq_res,
2972 task))
2973 return;
2974 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975}
2976
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002977static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002979 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2980
Trond Myklebust14516c32010-07-31 14:29:06 -04002981 if (!nfs4_sequence_done(task, &res->seq_res))
2982 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002983 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002984 return 0;
2985 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002986 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
2988
Jeff Laytond3d41522010-09-17 17:31:57 -04002989static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2990{
2991 struct nfs_server *server = NFS_SERVER(dir);
2992 struct nfs_renameargs *arg = msg->rpc_argp;
2993 struct nfs_renameres *res = msg->rpc_resp;
2994
2995 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002996 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002997 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002998}
2999
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003000static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3001{
3002 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3003 &data->args.seq_args,
3004 &data->res.seq_res,
3005 task))
3006 return;
3007 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003008}
3009
3010static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3011 struct inode *new_dir)
3012{
3013 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3014
3015 if (!nfs4_sequence_done(task, &res->seq_res))
3016 return 0;
3017 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3018 return 0;
3019
3020 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003021 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003022 return 1;
3023}
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3026 struct inode *new_dir, struct qstr *new_name)
3027{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003028 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003029 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 .old_dir = NFS_FH(old_dir),
3031 .new_dir = NFS_FH(new_dir),
3032 .old_name = old_name,
3033 .new_name = new_name,
3034 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003035 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003036 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003037 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 struct rpc_message msg = {
3039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3040 .rpc_argp = &arg,
3041 .rpc_resp = &res,
3042 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003043 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Bryan Schumaker7c513052011-03-24 17:12:24 +00003045 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 if (!status) {
3047 update_changeattr(old_dir, &res.old_cinfo);
3048 update_changeattr(new_dir, &res.new_cinfo);
3049 }
3050 return status;
3051}
3052
3053static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3054 struct inode *new_dir, struct qstr *new_name)
3055{
3056 struct nfs4_exception exception = { };
3057 int err;
3058 do {
3059 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3060 _nfs4_proc_rename(old_dir, old_name,
3061 new_dir, new_name),
3062 &exception);
3063 } while (exception.retry);
3064 return err;
3065}
3066
3067static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3068{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003069 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 struct nfs4_link_arg arg = {
3071 .fh = NFS_FH(inode),
3072 .dir_fh = NFS_FH(dir),
3073 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003074 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003076 struct nfs4_link_res res = {
3077 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003078 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 struct rpc_message msg = {
3080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3081 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003082 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003084 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Trond Myklebust136f2622010-04-16 16:22:49 -04003086 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003087 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003088 goto out;
3089
Bryan Schumaker7c513052011-03-24 17:12:24 +00003090 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003091 if (!status) {
3092 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003093 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003094 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003095out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003096 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return status;
3098}
3099
3100static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3101{
3102 struct nfs4_exception exception = { };
3103 int err;
3104 do {
3105 err = nfs4_handle_exception(NFS_SERVER(inode),
3106 _nfs4_proc_link(inode, dir, name),
3107 &exception);
3108 } while (exception.retry);
3109 return err;
3110}
3111
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112struct nfs4_createdata {
3113 struct rpc_message msg;
3114 struct nfs4_create_arg arg;
3115 struct nfs4_create_res res;
3116 struct nfs_fh fh;
3117 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003118};
3119
3120static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3121 struct qstr *name, struct iattr *sattr, u32 ftype)
3122{
3123 struct nfs4_createdata *data;
3124
3125 data = kzalloc(sizeof(*data), GFP_KERNEL);
3126 if (data != NULL) {
3127 struct nfs_server *server = NFS_SERVER(dir);
3128
3129 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3130 data->msg.rpc_argp = &data->arg;
3131 data->msg.rpc_resp = &data->res;
3132 data->arg.dir_fh = NFS_FH(dir);
3133 data->arg.server = server;
3134 data->arg.name = name;
3135 data->arg.attrs = sattr;
3136 data->arg.ftype = ftype;
3137 data->arg.bitmask = server->attr_bitmask;
3138 data->res.server = server;
3139 data->res.fh = &data->fh;
3140 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003141 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003142 }
3143 return data;
3144}
3145
3146static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3147{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003148 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003149 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003150 if (status == 0) {
3151 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003152 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3153 }
3154 return status;
3155}
3156
3157static void nfs4_free_createdata(struct nfs4_createdata *data)
3158{
3159 kfree(data);
3160}
3161
Chuck Lever4f390c12006-08-22 20:06:22 -04003162static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003163 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003165 struct nfs4_createdata *data;
3166 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Chuck Lever94a6d752006-08-22 20:06:23 -04003168 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003169 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003170
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003171 status = -ENOMEM;
3172 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3173 if (data == NULL)
3174 goto out;
3175
3176 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3177 data->arg.u.symlink.pages = &page;
3178 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003180 status = nfs4_do_create(dir, dentry, data);
3181
3182 nfs4_free_createdata(data);
3183out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return status;
3185}
3186
Chuck Lever4f390c12006-08-22 20:06:22 -04003187static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003188 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
3190 struct nfs4_exception exception = { };
3191 int err;
3192 do {
3193 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003194 _nfs4_proc_symlink(dir, dentry, page,
3195 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 &exception);
3197 } while (exception.retry);
3198 return err;
3199}
3200
3201static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3202 struct iattr *sattr)
3203{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003204 struct nfs4_createdata *data;
3205 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003207 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3208 if (data == NULL)
3209 goto out;
3210
3211 status = nfs4_do_create(dir, dentry, data);
3212
3213 nfs4_free_createdata(data);
3214out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 return status;
3216}
3217
3218static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3219 struct iattr *sattr)
3220{
3221 struct nfs4_exception exception = { };
3222 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003223
3224 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 do {
3226 err = nfs4_handle_exception(NFS_SERVER(dir),
3227 _nfs4_proc_mkdir(dir, dentry, sattr),
3228 &exception);
3229 } while (exception.retry);
3230 return err;
3231}
3232
3233static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003234 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
3236 struct inode *dir = dentry->d_inode;
3237 struct nfs4_readdir_arg args = {
3238 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003239 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 .pgbase = 0,
3241 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003242 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003243 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 };
3245 struct nfs4_readdir_res res;
3246 struct rpc_message msg = {
3247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3248 .rpc_argp = &args,
3249 .rpc_resp = &res,
3250 .rpc_cred = cred,
3251 };
3252 int status;
3253
Harvey Harrison3110ff82008-05-02 13:42:44 -07003254 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003255 dentry->d_parent->d_name.name,
3256 dentry->d_name.name,
3257 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003258 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003260 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 -05003261 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003262 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003263 status += args.pgbase;
3264 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003265
3266 nfs_invalidate_atime(dir);
3267
Harvey Harrison3110ff82008-05-02 13:42:44 -07003268 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return status;
3270}
3271
3272static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003273 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
3275 struct nfs4_exception exception = { };
3276 int err;
3277 do {
3278 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3279 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003280 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 &exception);
3282 } while (exception.retry);
3283 return err;
3284}
3285
3286static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3287 struct iattr *sattr, dev_t rdev)
3288{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003289 struct nfs4_createdata *data;
3290 int mode = sattr->ia_mode;
3291 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
3293 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3294 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003295
3296 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3297 if (data == NULL)
3298 goto out;
3299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003301 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003303 data->arg.ftype = NF4BLK;
3304 data->arg.u.device.specdata1 = MAJOR(rdev);
3305 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
3307 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003308 data->arg.ftype = NF4CHR;
3309 data->arg.u.device.specdata1 = MAJOR(rdev);
3310 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003313 status = nfs4_do_create(dir, dentry, data);
3314
3315 nfs4_free_createdata(data);
3316out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 return status;
3318}
3319
3320static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3321 struct iattr *sattr, dev_t rdev)
3322{
3323 struct nfs4_exception exception = { };
3324 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003325
3326 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 do {
3328 err = nfs4_handle_exception(NFS_SERVER(dir),
3329 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3330 &exception);
3331 } while (exception.retry);
3332 return err;
3333}
3334
3335static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3336 struct nfs_fsstat *fsstat)
3337{
3338 struct nfs4_statfs_arg args = {
3339 .fh = fhandle,
3340 .bitmask = server->attr_bitmask,
3341 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003342 struct nfs4_statfs_res res = {
3343 .fsstat = fsstat,
3344 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 struct rpc_message msg = {
3346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3347 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003348 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 };
3350
Trond Myklebust0e574af2005-10-27 22:12:38 -04003351 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003352 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
3355static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3356{
3357 struct nfs4_exception exception = { };
3358 int err;
3359 do {
3360 err = nfs4_handle_exception(server,
3361 _nfs4_proc_statfs(server, fhandle, fsstat),
3362 &exception);
3363 } while (exception.retry);
3364 return err;
3365}
3366
3367static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3368 struct nfs_fsinfo *fsinfo)
3369{
3370 struct nfs4_fsinfo_arg args = {
3371 .fh = fhandle,
3372 .bitmask = server->attr_bitmask,
3373 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003374 struct nfs4_fsinfo_res res = {
3375 .fsinfo = fsinfo,
3376 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 struct rpc_message msg = {
3378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3379 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003380 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 };
3382
Bryan Schumaker7c513052011-03-24 17:12:24 +00003383 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384}
3385
3386static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3387{
3388 struct nfs4_exception exception = { };
3389 int err;
3390
3391 do {
3392 err = nfs4_handle_exception(server,
3393 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3394 &exception);
3395 } while (exception.retry);
3396 return err;
3397}
3398
3399static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3400{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003401 int error;
3402
Trond Myklebust0e574af2005-10-27 22:12:38 -04003403 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003404 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3405 if (error == 0)
3406 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3407
3408 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409}
3410
3411static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3412 struct nfs_pathconf *pathconf)
3413{
3414 struct nfs4_pathconf_arg args = {
3415 .fh = fhandle,
3416 .bitmask = server->attr_bitmask,
3417 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003418 struct nfs4_pathconf_res res = {
3419 .pathconf = pathconf,
3420 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 struct rpc_message msg = {
3422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3423 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003424 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 };
3426
3427 /* None of the pathconf attributes are mandatory to implement */
3428 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3429 memset(pathconf, 0, sizeof(*pathconf));
3430 return 0;
3431 }
3432
Trond Myklebust0e574af2005-10-27 22:12:38 -04003433 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003434 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
3437static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3438 struct nfs_pathconf *pathconf)
3439{
3440 struct nfs4_exception exception = { };
3441 int err;
3442
3443 do {
3444 err = nfs4_handle_exception(server,
3445 _nfs4_proc_pathconf(server, fhandle, pathconf),
3446 &exception);
3447 } while (exception.retry);
3448 return err;
3449}
3450
Benny Halevyd20581a2011-05-22 19:52:03 +03003451void __nfs4_read_done_cb(struct nfs_read_data *data)
3452{
Fred Isamancd841602012-04-20 14:47:44 -04003453 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003454}
3455
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003456static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
Fred Isamancd841602012-04-20 14:47:44 -04003458 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003460 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003461 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003462 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003464
Benny Halevyd20581a2011-05-22 19:52:03 +03003465 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003467 renew_lease(server, data->timestamp);
3468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
3470
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003471static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3472{
3473
3474 dprintk("--> %s\n", __func__);
3475
3476 if (!nfs4_sequence_done(task, &data->res.seq_res))
3477 return -EAGAIN;
3478
Benny Halevyd20581a2011-05-22 19:52:03 +03003479 return data->read_done_cb ? data->read_done_cb(task, data) :
3480 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003481}
3482
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003483static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003486 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003487 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003488 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003491static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3492{
Fred Isamancd841602012-04-20 14:47:44 -04003493 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003494 &data->args.seq_args,
3495 &data->res.seq_res,
3496 task))
3497 return;
3498 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
Fred Isamanb029bc92011-03-03 15:13:42 +00003501static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
Fred Isamancd841602012-04-20 14:47:44 -04003503 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003505 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003506 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003507 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003509 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003511 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003512 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
3515
Fred Isamanb029bc92011-03-03 15:13:42 +00003516static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3517{
3518 if (!nfs4_sequence_done(task, &data->res.seq_res))
3519 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003520 return data->write_done_cb ? data->write_done_cb(task, data) :
3521 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003522}
3523
Trond Myklebust5a37f852012-04-28 14:55:16 -04003524static
3525bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003526{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003527 const struct nfs_pgio_header *hdr = data->header;
3528
3529 /* Don't request attributes for pNFS or O_DIRECT writes */
3530 if (data->ds_clp != NULL || hdr->dreq != NULL)
3531 return false;
3532 /* Otherwise, request attributes if and only if we don't hold
3533 * a delegation
3534 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003535 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003536}
Fred Isamana69aef12011-03-03 15:13:47 +00003537
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003538static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
Fred Isamancd841602012-04-20 14:47:44 -04003540 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003541
Trond Myklebust5a37f852012-04-28 14:55:16 -04003542 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003543 data->args.bitmask = NULL;
3544 data->res.fattr = NULL;
3545 } else
3546 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003547
Fred Isamanb029bc92011-03-03 15:13:42 +00003548 if (!data->write_done_cb)
3549 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003550 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 data->timestamp = jiffies;
3552
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003553 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003554 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555}
3556
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003557static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3558{
Fred Isamancd841602012-04-20 14:47:44 -04003559 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003560 &data->args.seq_args,
3561 &data->res.seq_res,
3562 task))
3563 return;
3564 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565}
3566
Fred Isaman0b7c0152012-04-20 14:47:39 -04003567static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3568{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3570 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003571 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 return;
3574 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003575}
3576
Fred Isaman0b7c0152012-04-20 14:47:39 -04003577static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
3579 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003580
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003581 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003582 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003583 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
Fred Isaman0b7c0152012-04-20 14:47:39 -04003588static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003589{
3590 if (!nfs4_sequence_done(task, &data->res.seq_res))
3591 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003592 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003593}
3594
Fred Isaman0b7c0152012-04-20 14:47:39 -04003595static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003597 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003598
Fred Isaman0b7c0152012-04-20 14:47:39 -04003599 if (data->commit_done_cb == NULL)
3600 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003601 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003602 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003603 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003606struct nfs4_renewdata {
3607 struct nfs_client *client;
3608 unsigned long timestamp;
3609};
3610
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611/*
3612 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3613 * standalone procedure for queueing an asynchronous RENEW.
3614 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003615static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003616{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003617 struct nfs4_renewdata *data = calldata;
3618 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003619
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003620 if (atomic_read(&clp->cl_count) > 1)
3621 nfs4_schedule_state_renewal(clp);
3622 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003623 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003624}
3625
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003626static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003628 struct nfs4_renewdata *data = calldata;
3629 struct nfs_client *clp = data->client;
3630 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
3632 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003633 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003634 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3635 return;
3636 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003637 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003638 return;
3639 }
3640 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003642 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003645static const struct rpc_call_ops nfs4_renew_ops = {
3646 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003647 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003648};
3649
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003650static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
3652 struct rpc_message msg = {
3653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3654 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003655 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003657 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003659 if (renew_flags == 0)
3660 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003661 if (!atomic_inc_not_zero(&clp->cl_count))
3662 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003663 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003664 if (data == NULL)
3665 return -ENOMEM;
3666 data->client = clp;
3667 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003669 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003672static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
3674 struct rpc_message msg = {
3675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3676 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003677 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 };
3679 unsigned long now = jiffies;
3680 int status;
3681
3682 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3683 if (status < 0)
3684 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003685 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 return 0;
3687}
3688
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003689static inline int nfs4_server_supports_acls(struct nfs_server *server)
3690{
3691 return (server->caps & NFS_CAP_ACLS)
3692 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3693 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3694}
3695
Trond Myklebust21f498c2012-08-24 10:59:25 -04003696/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3697 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003698 * the stack.
3699 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003700#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003701
Neil Hormane9e3d722011-03-04 19:26:03 -05003702static int buf_to_pages_noslab(const void *buf, size_t buflen,
3703 struct page **pages, unsigned int *pgbase)
3704{
3705 struct page *newpage, **spages;
3706 int rc = 0;
3707 size_t len;
3708 spages = pages;
3709
3710 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003711 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003712 newpage = alloc_page(GFP_KERNEL);
3713
3714 if (newpage == NULL)
3715 goto unwind;
3716 memcpy(page_address(newpage), buf, len);
3717 buf += len;
3718 buflen -= len;
3719 *pages++ = newpage;
3720 rc++;
3721 } while (buflen != 0);
3722
3723 return rc;
3724
3725unwind:
3726 for(; rc > 0; rc--)
3727 __free_page(spages[rc-1]);
3728 return -ENOMEM;
3729}
3730
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003731struct nfs4_cached_acl {
3732 int cached;
3733 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003734 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003735};
3736
3737static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003738{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003739 struct nfs_inode *nfsi = NFS_I(inode);
3740
3741 spin_lock(&inode->i_lock);
3742 kfree(nfsi->nfs4_acl);
3743 nfsi->nfs4_acl = acl;
3744 spin_unlock(&inode->i_lock);
3745}
3746
3747static void nfs4_zap_acl_attr(struct inode *inode)
3748{
3749 nfs4_set_cached_acl(inode, NULL);
3750}
3751
3752static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3753{
3754 struct nfs_inode *nfsi = NFS_I(inode);
3755 struct nfs4_cached_acl *acl;
3756 int ret = -ENOENT;
3757
3758 spin_lock(&inode->i_lock);
3759 acl = nfsi->nfs4_acl;
3760 if (acl == NULL)
3761 goto out;
3762 if (buf == NULL) /* user is just asking for length */
3763 goto out_len;
3764 if (acl->cached == 0)
3765 goto out;
3766 ret = -ERANGE; /* see getxattr(2) man page */
3767 if (acl->len > buflen)
3768 goto out;
3769 memcpy(buf, acl->data, acl->len);
3770out_len:
3771 ret = acl->len;
3772out:
3773 spin_unlock(&inode->i_lock);
3774 return ret;
3775}
3776
Sachin Prabhu5794d212012-04-17 14:36:40 +01003777static 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 +00003778{
3779 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003780 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003781
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003782 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003783 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003784 if (acl == NULL)
3785 goto out;
3786 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003787 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003788 } else {
3789 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3790 if (acl == NULL)
3791 goto out;
3792 acl->cached = 0;
3793 }
3794 acl->len = acl_len;
3795out:
3796 nfs4_set_cached_acl(inode, acl);
3797}
3798
Andy Adamsonbf118a32011-12-07 11:55:27 -05003799/*
3800 * The getxattr API returns the required buffer length when called with a
3801 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3802 * the required buf. On a NULL buf, we send a page of data to the server
3803 * guessing that the ACL request can be serviced by a page. If so, we cache
3804 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3805 * the cache. If not so, we throw away the page, and cache the required
3806 * length. The next getxattr call will then produce another round trip to
3807 * the server, this time with the input buf of the required size.
3808 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003809static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003810{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003811 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003812 struct nfs_getaclargs args = {
3813 .fh = NFS_FH(inode),
3814 .acl_pages = pages,
3815 .acl_len = buflen,
3816 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003817 struct nfs_getaclres res = {
3818 .acl_len = buflen,
3819 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003820 struct rpc_message msg = {
3821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3822 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003823 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003824 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003825 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3826 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003827
Andy Adamsonbf118a32011-12-07 11:55:27 -05003828 /* As long as we're doing a round trip to the server anyway,
3829 * let's be prepared for a page of acl data. */
3830 if (npages == 0)
3831 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003832 if (npages > ARRAY_SIZE(pages))
3833 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003834
Andy Adamsonbf118a32011-12-07 11:55:27 -05003835 for (i = 0; i < npages; i++) {
3836 pages[i] = alloc_page(GFP_KERNEL);
3837 if (!pages[i])
3838 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003839 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003840
3841 /* for decoding across pages */
3842 res.acl_scratch = alloc_page(GFP_KERNEL);
3843 if (!res.acl_scratch)
3844 goto out_free;
3845
Andy Adamsonbf118a32011-12-07 11:55:27 -05003846 args.acl_len = npages * PAGE_SIZE;
3847 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003848
Peng Taode040be2012-01-10 22:42:47 +08003849 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003850 __func__, buf, buflen, npages, args.acl_len);
3851 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3852 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003853 if (ret)
3854 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003855
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003856 /* Handle the case where the passed-in buffer is too short */
3857 if (res.acl_flags & NFS4_ACL_TRUNC) {
3858 /* Did the user only issue a request for the acl length? */
3859 if (buf == NULL)
3860 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003861 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003862 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003863 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003864 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3865 if (buf)
3866 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3867out_ok:
3868 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003869out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003870 for (i = 0; i < npages; i++)
3871 if (pages[i])
3872 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003873 if (res.acl_scratch)
3874 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003875 return ret;
3876}
3877
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003878static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3879{
3880 struct nfs4_exception exception = { };
3881 ssize_t ret;
3882 do {
3883 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3884 if (ret >= 0)
3885 break;
3886 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3887 } while (exception.retry);
3888 return ret;
3889}
3890
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003891static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3892{
3893 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003894 int ret;
3895
3896 if (!nfs4_server_supports_acls(server))
3897 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003898 ret = nfs_revalidate_inode(server, inode);
3899 if (ret < 0)
3900 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003901 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3902 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003903 ret = nfs4_read_cached_acl(inode, buf, buflen);
3904 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003905 /* -ENOENT is returned if there is no ACL or if there is an ACL
3906 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003907 return ret;
3908 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003909}
3910
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003911static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003912{
3913 struct nfs_server *server = NFS_SERVER(inode);
3914 struct page *pages[NFS4ACL_MAXPAGES];
3915 struct nfs_setaclargs arg = {
3916 .fh = NFS_FH(inode),
3917 .acl_pages = pages,
3918 .acl_len = buflen,
3919 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003920 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003921 struct rpc_message msg = {
3922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3923 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003924 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003925 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003926 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003927 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003928
3929 if (!nfs4_server_supports_acls(server))
3930 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003931 if (npages > ARRAY_SIZE(pages))
3932 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003933 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3934 if (i < 0)
3935 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003936 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003937 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003938
3939 /*
3940 * Free each page after tx, so the only ref left is
3941 * held by the network stack
3942 */
3943 for (; i > 0; i--)
3944 put_page(pages[i-1]);
3945
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003946 /*
3947 * Acl update can result in inode attribute update.
3948 * so mark the attribute cache invalid.
3949 */
3950 spin_lock(&inode->i_lock);
3951 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3952 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003953 nfs_access_zap_cache(inode);
3954 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003955 return ret;
3956}
3957
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003958static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3959{
3960 struct nfs4_exception exception = { };
3961 int err;
3962 do {
3963 err = nfs4_handle_exception(NFS_SERVER(inode),
3964 __nfs4_proc_set_acl(inode, buf, buflen),
3965 &exception);
3966 } while (exception.retry);
3967 return err;
3968}
3969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003971nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003973 struct nfs_client *clp = server->nfs_client;
3974
3975 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 return 0;
3977 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003978 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003979 case -NFS4ERR_ADMIN_REVOKED:
3980 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003981 if (state == NULL)
3982 break;
3983 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003984 case -NFS4ERR_OPENMODE:
3985 if (state == NULL)
3986 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003987 nfs4_schedule_stateid_recovery(server, state);
3988 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003989 case -NFS4ERR_EXPIRED:
3990 if (state != NULL)
3991 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003993 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003994 nfs4_schedule_lease_recovery(clp);
3995 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003996#if defined(CONFIG_NFS_V4_1)
3997 case -NFS4ERR_BADSESSION:
3998 case -NFS4ERR_BADSLOT:
3999 case -NFS4ERR_BAD_HIGH_SLOT:
4000 case -NFS4ERR_DEADSESSION:
4001 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4002 case -NFS4ERR_SEQ_FALSE_RETRY:
4003 case -NFS4ERR_SEQ_MISORDERED:
4004 dprintk("%s ERROR %d, Reset session\n", __func__,
4005 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004006 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004007 task->tk_status = 0;
4008 return -EAGAIN;
4009#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004011 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004012 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004013 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4015 task->tk_status = 0;
4016 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004017 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 case -NFS4ERR_OLD_STATEID:
4019 task->tk_status = 0;
4020 return -EAGAIN;
4021 }
4022 task->tk_status = nfs4_map_errors(task->tk_status);
4023 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004024wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004025 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004026 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4027 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4028 task->tk_status = 0;
4029 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030}
4031
Chuck Leverf0920752012-05-21 22:45:41 -04004032static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4033 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004034{
4035 __be32 verf[2];
4036
Chuck Lever2c820d92012-05-21 22:45:33 -04004037 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4038 /* An impossible timestamp guarantees this value
4039 * will never match a generated boot time. */
4040 verf[0] = 0;
4041 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4042 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004043 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4044 verf[0] = (__be32)nn->boot_time.tv_sec;
4045 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004046 }
Chuck Levercd937102012-03-02 17:14:31 -05004047 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4048}
4049
Chuck Levere984a552012-09-14 17:24:21 -04004050static unsigned int
4051nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4052 char *buf, size_t len)
4053{
4054 unsigned int result;
4055
4056 rcu_read_lock();
4057 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4058 clp->cl_ipaddr,
4059 rpc_peeraddr2str(clp->cl_rpcclient,
4060 RPC_DISPLAY_ADDR),
4061 rpc_peeraddr2str(clp->cl_rpcclient,
4062 RPC_DISPLAY_PROTO));
4063 rcu_read_unlock();
4064 return result;
4065}
4066
4067static unsigned int
4068nfs4_init_uniform_client_string(const struct nfs_client *clp,
4069 char *buf, size_t len)
4070{
4071 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4072 clp->rpc_ops->version, clp->cl_minorversion,
4073 clp->cl_rpcclient->cl_nodename);
4074}
4075
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004076/**
4077 * nfs4_proc_setclientid - Negotiate client ID
4078 * @clp: state data structure
4079 * @program: RPC program for NFSv4 callback service
4080 * @port: IP port number for NFS4 callback service
4081 * @cred: RPC credential to use for this call
4082 * @res: where to place the result
4083 *
4084 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4085 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004086int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4087 unsigned short port, struct rpc_cred *cred,
4088 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089{
4090 nfs4_verifier sc_verifier;
4091 struct nfs4_setclientid setclientid = {
4092 .sc_verifier = &sc_verifier,
4093 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004094 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 };
4096 struct rpc_message msg = {
4097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4098 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004099 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004100 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004102 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
Chuck Leverde734832012-07-11 16:30:50 -04004104 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004105 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004106 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4107 setclientid.sc_name_len =
4108 nfs4_init_uniform_client_string(clp,
4109 setclientid.sc_name,
4110 sizeof(setclientid.sc_name));
4111 else
4112 setclientid.sc_name_len =
4113 nfs4_init_nonuniform_client_string(clp,
4114 setclientid.sc_name,
4115 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004116 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004117 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004118 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004119 sizeof(setclientid.sc_netid),
4120 rpc_peeraddr2str(clp->cl_rpcclient,
4121 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004122 rcu_read_unlock();
4123 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004124 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 clp->cl_ipaddr, port >> 8, port & 255);
4126
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004127 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4128 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4129 setclientid.sc_name_len, setclientid.sc_name);
4130 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4131 dprintk("NFS reply setclientid: %d\n", status);
4132 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004135/**
4136 * nfs4_proc_setclientid_confirm - Confirm client ID
4137 * @clp: state data structure
4138 * @res: result of a previous SETCLIENTID
4139 * @cred: RPC credential to use for this call
4140 *
4141 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4142 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004143int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004144 struct nfs4_setclientid_res *arg,
4145 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
4147 struct nfs_fsinfo fsinfo;
4148 struct rpc_message msg = {
4149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004150 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004152 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 };
4154 unsigned long now;
4155 int status;
4156
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004157 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4158 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4159 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004161 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 if (status == 0) {
4163 spin_lock(&clp->cl_lock);
4164 clp->cl_lease_time = fsinfo.lease_time * HZ;
4165 clp->cl_last_renewal = now;
4166 spin_unlock(&clp->cl_lock);
4167 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004168 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 return status;
4170}
4171
Trond Myklebustfe650402006-01-03 09:55:18 +01004172struct nfs4_delegreturndata {
4173 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004174 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004175 struct nfs_fh fh;
4176 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004177 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004178 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004179 int rpc_status;
4180};
4181
Trond Myklebustfe650402006-01-03 09:55:18 +01004182static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4183{
4184 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004185
Trond Myklebust14516c32010-07-31 14:29:06 -04004186 if (!nfs4_sequence_done(task, &data->res.seq_res))
4187 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004188
Ricardo Labiaga79708862009-12-07 09:23:21 -05004189 switch (task->tk_status) {
4190 case -NFS4ERR_STALE_STATEID:
4191 case -NFS4ERR_EXPIRED:
4192 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004193 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004194 break;
4195 default:
4196 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4197 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004198 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004199 return;
4200 }
4201 }
4202 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004203}
4204
4205static void nfs4_delegreturn_release(void *calldata)
4206{
Trond Myklebustfe650402006-01-03 09:55:18 +01004207 kfree(calldata);
4208}
4209
Andy Adamson938e1012009-04-01 09:22:28 -04004210#if defined(CONFIG_NFS_V4_1)
4211static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4212{
4213 struct nfs4_delegreturndata *d_data;
4214
4215 d_data = (struct nfs4_delegreturndata *)data;
4216
Trond Myklebust035168a2010-06-16 09:52:26 -04004217 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004218 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004219 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004220 return;
4221 rpc_call_start(task);
4222}
4223#endif /* CONFIG_NFS_V4_1 */
4224
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004225static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004226#if defined(CONFIG_NFS_V4_1)
4227 .rpc_call_prepare = nfs4_delegreturn_prepare,
4228#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004229 .rpc_call_done = nfs4_delegreturn_done,
4230 .rpc_release = nfs4_delegreturn_release,
4231};
4232
Trond Myklebuste6f81072008-01-24 18:14:34 -05004233static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004234{
4235 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004236 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004237 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004238 struct rpc_message msg = {
4239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4240 .rpc_cred = cred,
4241 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004242 struct rpc_task_setup task_setup_data = {
4243 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004244 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004245 .callback_ops = &nfs4_delegreturn_ops,
4246 .flags = RPC_TASK_ASYNC,
4247 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004248 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004249
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004250 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004251 if (data == NULL)
4252 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004253 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004254 data->args.fhandle = &data->fh;
4255 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004256 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004257 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004258 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004259 data->res.fattr = &data->fattr;
4260 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004261 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004262 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004263 data->rpc_status = 0;
4264
Trond Myklebustc970aa82007-07-14 15:39:59 -04004265 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004266 msg.rpc_argp = &data->args;
4267 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004268 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004269 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004270 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004271 if (!issync)
4272 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004273 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004274 if (status != 0)
4275 goto out;
4276 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004277 if (status == 0)
4278 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4279 else
4280 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004281out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004282 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004283 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284}
4285
Trond Myklebuste6f81072008-01-24 18:14:34 -05004286int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287{
4288 struct nfs_server *server = NFS_SERVER(inode);
4289 struct nfs4_exception exception = { };
4290 int err;
4291 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004292 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 switch (err) {
4294 case -NFS4ERR_STALE_STATEID:
4295 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 case 0:
4297 return 0;
4298 }
4299 err = nfs4_handle_exception(server, err, &exception);
4300 } while (exception.retry);
4301 return err;
4302}
4303
4304#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4305#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4306
4307/*
4308 * sleep, with exponential backoff, and retry the LOCK operation.
4309 */
4310static unsigned long
4311nfs4_set_lock_task_retry(unsigned long timeout)
4312{
Jeff Laytond3103102011-12-01 22:44:39 +01004313 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 timeout <<= 1;
4315 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4316 return NFS4_LOCK_MAXTIMEOUT;
4317 return timeout;
4318}
4319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4321{
4322 struct inode *inode = state->inode;
4323 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004324 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004325 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004327 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004329 struct nfs_lockt_res res = {
4330 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 };
4332 struct rpc_message msg = {
4333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4334 .rpc_argp = &arg,
4335 .rpc_resp = &res,
4336 .rpc_cred = state->owner->so_cred,
4337 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 struct nfs4_lock_state *lsp;
4339 int status;
4340
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004341 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004342 status = nfs4_set_lock_state(state, request);
4343 if (status != 0)
4344 goto out;
4345 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004346 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004347 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004348 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004349 switch (status) {
4350 case 0:
4351 request->fl_type = F_UNLCK;
4352 break;
4353 case -NFS4ERR_DENIED:
4354 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004356 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004357out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 return status;
4359}
4360
4361static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4362{
4363 struct nfs4_exception exception = { };
4364 int err;
4365
4366 do {
4367 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4368 _nfs4_proc_getlk(state, cmd, request),
4369 &exception);
4370 } while (exception.retry);
4371 return err;
4372}
4373
4374static int do_vfs_lock(struct file *file, struct file_lock *fl)
4375{
4376 int res = 0;
4377 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4378 case FL_POSIX:
4379 res = posix_lock_file_wait(file, fl);
4380 break;
4381 case FL_FLOCK:
4382 res = flock_lock_file_wait(file, fl);
4383 break;
4384 default:
4385 BUG();
4386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 return res;
4388}
4389
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004390struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004391 struct nfs_locku_args arg;
4392 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004393 struct nfs4_lock_state *lsp;
4394 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004395 struct file_lock fl;
4396 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004397 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004398};
4399
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004400static struct nfs4_unlockdata *nfs4_alloc_unlockdata(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 *p;
4406 struct inode *inode = lsp->ls_state->inode;
4407
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004408 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004409 if (p == NULL)
4410 return NULL;
4411 p->arg.fh = NFS_FH(inode);
4412 p->arg.fl = &p->fl;
4413 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004414 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004415 p->arg.stateid = &lsp->ls_stateid;
4416 p->lsp = lsp;
4417 atomic_inc(&lsp->ls_count);
4418 /* Ensure we don't close file until we're done freeing locks! */
4419 p->ctx = get_nfs_open_context(ctx);
4420 memcpy(&p->fl, fl, sizeof(p->fl));
4421 p->server = NFS_SERVER(inode);
4422 return p;
4423}
4424
Trond Myklebust06f814a2006-01-03 09:55:07 +01004425static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004426{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004427 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004428 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004429 nfs4_put_lock_state(calldata->lsp);
4430 put_nfs_open_context(calldata->ctx);
4431 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004432}
4433
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004434static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004435{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004436 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004437
Trond Myklebust14516c32010-07-31 14:29:06 -04004438 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4439 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004440 switch (task->tk_status) {
4441 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004442 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4443 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004444 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004445 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004446 case -NFS4ERR_BAD_STATEID:
4447 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004448 case -NFS4ERR_STALE_STATEID:
4449 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004450 break;
4451 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004452 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004453 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004454 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004455}
4456
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004457static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004458{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004459 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004461 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004462 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004463 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004464 /* Note: exit _without_ running nfs4_locku_done */
4465 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004466 return;
4467 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004468 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004469 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004470 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004471 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004472 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004473 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474}
4475
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004476static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004477 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004478 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004479 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004480};
4481
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004482static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4483 struct nfs_open_context *ctx,
4484 struct nfs4_lock_state *lsp,
4485 struct nfs_seqid *seqid)
4486{
4487 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004488 struct rpc_message msg = {
4489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4490 .rpc_cred = ctx->cred,
4491 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004492 struct rpc_task_setup task_setup_data = {
4493 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004494 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004495 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004496 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004497 .flags = RPC_TASK_ASYNC,
4498 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004499
Frank Filz137d6ac2007-07-09 15:32:29 -07004500 /* Ensure this is an unlock - when canceling a lock, the
4501 * canceled lock is passed in, and it won't be an unlock.
4502 */
4503 fl->fl_type = F_UNLCK;
4504
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004505 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4506 if (data == NULL) {
4507 nfs_free_seqid(seqid);
4508 return ERR_PTR(-ENOMEM);
4509 }
4510
Trond Myklebust9d12b212012-01-17 22:04:25 -05004511 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004512 msg.rpc_argp = &data->arg;
4513 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004514 task_setup_data.callback_data = data;
4515 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004516}
4517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4519{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004520 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004521 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004522 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004523 struct rpc_task *task;
4524 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004525 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
Trond Myklebust9b073572006-06-29 16:38:34 -04004527 status = nfs4_set_lock_state(state, request);
4528 /* Unlock _before_ we do the RPC call */
4529 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004530 down_read(&nfsi->rwsem);
4531 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4532 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004533 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004534 }
4535 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004536 if (status != 0)
4537 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004538 /* Is this a delegated lock? */
4539 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004540 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004541 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004542 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004543 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004544 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004545 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004546 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004547 status = PTR_ERR(task);
4548 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004549 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004550 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004551 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004552out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004553 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004554 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555}
4556
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004557struct nfs4_lockdata {
4558 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004559 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004560 struct nfs4_lock_state *lsp;
4561 struct nfs_open_context *ctx;
4562 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004563 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004564 int rpc_status;
4565 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004566 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567};
4568
4569static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004570 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4571 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004572{
4573 struct nfs4_lockdata *p;
4574 struct inode *inode = lsp->ls_state->inode;
4575 struct nfs_server *server = NFS_SERVER(inode);
4576
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004577 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004578 if (p == NULL)
4579 return NULL;
4580
4581 p->arg.fh = NFS_FH(inode);
4582 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004583 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004584 if (p->arg.open_seqid == NULL)
4585 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004586 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004587 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004588 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004589 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004590 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004591 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004592 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004593 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004594 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004595 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004596 atomic_inc(&lsp->ls_count);
4597 p->ctx = get_nfs_open_context(ctx);
4598 memcpy(&p->fl, fl, sizeof(p->fl));
4599 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004600out_free_seqid:
4601 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004602out_free:
4603 kfree(p);
4604 return NULL;
4605}
4606
4607static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4608{
4609 struct nfs4_lockdata *data = calldata;
4610 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Harvey Harrison3110ff82008-05-02 13:42:44 -07004612 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004613 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4614 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004615 /* Do we need to do an open_to_lock_owner? */
4616 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004617 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4618 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004619 data->arg.open_stateid = &state->stateid;
4620 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004621 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004622 } else
4623 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004624 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004625 if (nfs4_setup_sequence(data->server,
4626 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004627 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004628 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004629 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004630 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004631}
4632
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004633static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4634{
4635 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4636 nfs4_lock_prepare(task, calldata);
4637}
4638
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004639static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4640{
4641 struct nfs4_lockdata *data = calldata;
4642
Harvey Harrison3110ff82008-05-02 13:42:44 -07004643 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004644
Trond Myklebust14516c32010-07-31 14:29:06 -04004645 if (!nfs4_sequence_done(task, &data->res.seq_res))
4646 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004647
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004648 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004649 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004650 if (data->rpc_status == 0)
4651 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4652 else
4653 goto out;
4654 }
4655 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004656 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004657 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004658 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004659 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004660out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004661 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004662}
4663
4664static void nfs4_lock_release(void *calldata)
4665{
4666 struct nfs4_lockdata *data = calldata;
4667
Harvey Harrison3110ff82008-05-02 13:42:44 -07004668 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004669 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004670 if (data->cancelled != 0) {
4671 struct rpc_task *task;
4672 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4673 data->arg.lock_seqid);
4674 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004675 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004676 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004677 } else
4678 nfs_free_seqid(data->arg.lock_seqid);
4679 nfs4_put_lock_state(data->lsp);
4680 put_nfs_open_context(data->ctx);
4681 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004682 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004683}
4684
4685static const struct rpc_call_ops nfs4_lock_ops = {
4686 .rpc_call_prepare = nfs4_lock_prepare,
4687 .rpc_call_done = nfs4_lock_done,
4688 .rpc_release = nfs4_lock_release,
4689};
4690
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004691static const struct rpc_call_ops nfs4_recover_lock_ops = {
4692 .rpc_call_prepare = nfs4_recover_lock_prepare,
4693 .rpc_call_done = nfs4_lock_done,
4694 .rpc_release = nfs4_lock_release,
4695};
4696
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004697static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4698{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004699 switch (error) {
4700 case -NFS4ERR_ADMIN_REVOKED:
4701 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004702 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004703 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004704 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004705 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004706 break;
4707 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004708 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004709 case -NFS4ERR_EXPIRED:
4710 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004711 };
4712}
4713
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004714static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004715{
4716 struct nfs4_lockdata *data;
4717 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004718 struct rpc_message msg = {
4719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4720 .rpc_cred = state->owner->so_cred,
4721 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004722 struct rpc_task_setup task_setup_data = {
4723 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004724 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004725 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004726 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004727 .flags = RPC_TASK_ASYNC,
4728 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004729 int ret;
4730
Harvey Harrison3110ff82008-05-02 13:42:44 -07004731 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004732 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004733 fl->fl_u.nfs4_fl.owner,
4734 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004735 if (data == NULL)
4736 return -ENOMEM;
4737 if (IS_SETLKW(cmd))
4738 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004739 if (recovery_type > NFS_LOCK_NEW) {
4740 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004741 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004742 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4743 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004744 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004745 msg.rpc_argp = &data->arg;
4746 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004747 task_setup_data.callback_data = data;
4748 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004749 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004750 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004751 ret = nfs4_wait_for_completion_rpc_task(task);
4752 if (ret == 0) {
4753 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004754 if (ret)
4755 nfs4_handle_setlk_error(data->server, data->lsp,
4756 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004757 } else
4758 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004759 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004760 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004761 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
4764static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4765{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004766 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004767 struct nfs4_exception exception = {
4768 .inode = state->inode,
4769 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004770 int err;
4771
4772 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004773 /* Cache the lock if possible... */
4774 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4775 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004776 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004777 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004778 break;
4779 nfs4_handle_exception(server, err, &exception);
4780 } while (exception.retry);
4781 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782}
4783
4784static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4785{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004786 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004787 struct nfs4_exception exception = {
4788 .inode = state->inode,
4789 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004790 int err;
4791
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004792 err = nfs4_set_lock_state(state, request);
4793 if (err != 0)
4794 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004795 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004796 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4797 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004798 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004799 switch (err) {
4800 default:
4801 goto out;
4802 case -NFS4ERR_GRACE:
4803 case -NFS4ERR_DELAY:
4804 nfs4_handle_exception(server, err, &exception);
4805 err = 0;
4806 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004807 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004808out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004809 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
4811
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004812#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004813/**
4814 * nfs41_check_expired_locks - possibly free a lock stateid
4815 *
4816 * @state: NFSv4 state for an inode
4817 *
4818 * Returns NFS_OK if recovery for this stateid is now finished.
4819 * Otherwise a negative NFS4ERR value is returned.
4820 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004821static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004822{
Chuck Levereb64cf92012-07-11 16:30:05 -04004823 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004824 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004825 struct nfs_server *server = NFS_SERVER(state->inode);
4826
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004827 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004828 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004829 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4830 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004831 /* Free the stateid unless the server
4832 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004833 if (status != -NFS4ERR_BAD_STATEID)
4834 nfs41_free_stateid(server,
4835 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004836 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004837 ret = status;
4838 }
4839 }
4840 };
4841
4842 return ret;
4843}
4844
4845static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4846{
4847 int status = NFS_OK;
4848
4849 if (test_bit(LK_STATE_IN_USE, &state->flags))
4850 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004851 if (status != NFS_OK)
4852 status = nfs4_lock_expired(state, request);
4853 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004854}
4855#endif
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4858{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004859 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004860 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004861 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004863 if ((fl_flags & FL_POSIX) &&
4864 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4865 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004866 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004867 status = nfs4_set_lock_state(state, request);
4868 if (status != 0)
4869 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004870 request->fl_flags |= FL_ACCESS;
4871 status = do_vfs_lock(request->fl_file, request);
4872 if (status < 0)
4873 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004874 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004875 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004876 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004877 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004878 request->fl_flags = fl_flags & ~FL_SLEEP;
4879 status = do_vfs_lock(request->fl_file, request);
4880 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004881 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004882 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004883 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004884 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004885 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004886 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004887 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004888 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4889 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004890out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004891 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004892out:
4893 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 return status;
4895}
4896
4897static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4898{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004899 struct nfs4_exception exception = {
4900 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004901 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004902 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 int err;
4904
4905 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004906 err = _nfs4_proc_setlk(state, cmd, request);
4907 if (err == -NFS4ERR_DENIED)
4908 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004910 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 } while (exception.retry);
4912 return err;
4913}
4914
4915static int
4916nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4917{
4918 struct nfs_open_context *ctx;
4919 struct nfs4_state *state;
4920 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4921 int status;
4922
4923 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004924 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 state = ctx->state;
4926
4927 if (request->fl_start < 0 || request->fl_end < 0)
4928 return -EINVAL;
4929
Trond Myklebustd9531262009-07-21 19:22:38 -04004930 if (IS_GETLK(cmd)) {
4931 if (state != NULL)
4932 return nfs4_proc_getlk(state, F_GETLK, request);
4933 return 0;
4934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
4936 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4937 return -EINVAL;
4938
Trond Myklebustd9531262009-07-21 19:22:38 -04004939 if (request->fl_type == F_UNLCK) {
4940 if (state != NULL)
4941 return nfs4_proc_unlck(state, cmd, request);
4942 return 0;
4943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
Trond Myklebustd9531262009-07-21 19:22:38 -04004945 if (state == NULL)
4946 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004947 /*
4948 * Don't rely on the VFS having checked the file open mode,
4949 * since it won't do this for flock() locks.
4950 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004951 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004952 case F_RDLCK:
4953 if (!(filp->f_mode & FMODE_READ))
4954 return -EBADF;
4955 break;
4956 case F_WRLCK:
4957 if (!(filp->f_mode & FMODE_WRITE))
4958 return -EBADF;
4959 }
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 do {
4962 status = nfs4_proc_setlk(state, cmd, request);
4963 if ((status != -EAGAIN) || IS_SETLK(cmd))
4964 break;
4965 timeout = nfs4_set_lock_task_retry(timeout);
4966 status = -ERESTARTSYS;
4967 if (signalled())
4968 break;
4969 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 return status;
4971}
4972
Trond Myklebust888e6942005-11-04 15:38:11 -05004973int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4974{
4975 struct nfs_server *server = NFS_SERVER(state->inode);
4976 struct nfs4_exception exception = { };
4977 int err;
4978
4979 err = nfs4_set_lock_state(state, fl);
4980 if (err != 0)
4981 goto out;
4982 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004983 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004984 switch (err) {
4985 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004986 printk(KERN_ERR "NFS: %s: unhandled error "
4987 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004988 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004989 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004990 goto out;
4991 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004992 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004993 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004994 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004995 nfs4_schedule_lease_recovery(server->nfs_client);
4996 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004997 case -NFS4ERR_BADSESSION:
4998 case -NFS4ERR_BADSLOT:
4999 case -NFS4ERR_BAD_HIGH_SLOT:
5000 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5001 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005002 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005003 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005004 case -ERESTARTSYS:
5005 /*
5006 * The show must go on: exit, but mark the
5007 * stateid as needing recovery.
5008 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005009 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005010 case -NFS4ERR_ADMIN_REVOKED:
5011 case -NFS4ERR_BAD_STATEID:
5012 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005013 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005014 err = 0;
5015 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005016 case -EKEYEXPIRED:
5017 /*
5018 * User RPCSEC_GSS context has expired.
5019 * We cannot recover this stateid now, so
5020 * skip it and allow recovery thread to
5021 * proceed.
5022 */
5023 err = 0;
5024 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005025 case -ENOMEM:
5026 case -NFS4ERR_DENIED:
5027 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5028 err = 0;
5029 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005030 case -NFS4ERR_DELAY:
5031 break;
5032 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005033 err = nfs4_handle_exception(server, err, &exception);
5034 } while (exception.retry);
5035out:
5036 return err;
5037}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005038
Trond Myklebustcf470c32012-03-07 13:49:12 -05005039struct nfs_release_lockowner_data {
5040 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005041 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005042 struct nfs_release_lockowner_args args;
5043};
5044
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005045static void nfs4_release_lockowner_release(void *calldata)
5046{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005047 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005048 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005049 kfree(calldata);
5050}
5051
Trond Myklebust17280172012-03-11 13:11:00 -04005052static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005053 .rpc_release = nfs4_release_lockowner_release,
5054};
5055
Trond Myklebustcf470c32012-03-07 13:49:12 -05005056int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005057{
5058 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005059 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005060 struct rpc_message msg = {
5061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5062 };
5063
5064 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005065 return -EINVAL;
5066 data = kmalloc(sizeof(*data), GFP_NOFS);
5067 if (!data)
5068 return -ENOMEM;
5069 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005070 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005071 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5072 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5073 data->args.lock_owner.s_dev = server->s_dev;
5074 msg.rpc_argp = &data->args;
5075 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5076 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005077}
5078
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005079#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5080
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005081static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5082 const void *buf, size_t buflen,
5083 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005084{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005085 if (strcmp(key, "") != 0)
5086 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005087
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005088 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005089}
5090
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005091static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5092 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005093{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005094 if (strcmp(key, "") != 0)
5095 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005096
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005097 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005098}
5099
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005100static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5101 size_t list_len, const char *name,
5102 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005103{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005104 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005105
J. Bruce Fields096455a2006-03-20 23:23:42 -05005106 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5107 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005108
5109 if (list && len <= list_len)
5110 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005111 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005112}
5113
Andy Adamson533eb462011-06-13 18:25:56 -04005114/*
5115 * nfs_fhget will use either the mounted_on_fileid or the fileid
5116 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005117static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5118{
Andy Adamson533eb462011-06-13 18:25:56 -04005119 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5120 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5121 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005122 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005123 return;
5124
5125 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005126 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005127 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5128 fattr->nlink = 2;
5129}
5130
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005131static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5132 const struct qstr *name,
5133 struct nfs4_fs_locations *fs_locations,
5134 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005135{
5136 struct nfs_server *server = NFS_SERVER(dir);
5137 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005138 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005139 };
5140 struct nfs4_fs_locations_arg args = {
5141 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005142 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005143 .page = page,
5144 .bitmask = bitmask,
5145 };
Benny Halevy22958462009-04-01 09:22:02 -04005146 struct nfs4_fs_locations_res res = {
5147 .fs_locations = fs_locations,
5148 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005149 struct rpc_message msg = {
5150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5151 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005152 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005153 };
5154 int status;
5155
Harvey Harrison3110ff82008-05-02 13:42:44 -07005156 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005157
5158 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5159 * is not supported */
5160 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5161 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5162 else
5163 bitmask[0] |= FATTR4_WORD0_FILEID;
5164
Trond Myklebustc228fd32007-01-13 02:28:11 -05005165 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005166 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005167 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005168 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005169 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005170 return status;
5171}
5172
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005173int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5174 const struct qstr *name,
5175 struct nfs4_fs_locations *fs_locations,
5176 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005177{
5178 struct nfs4_exception exception = { };
5179 int err;
5180 do {
5181 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005182 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005183 &exception);
5184 } while (exception.retry);
5185 return err;
5186}
5187
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005188static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5189{
5190 int status;
5191 struct nfs4_secinfo_arg args = {
5192 .dir_fh = NFS_FH(dir),
5193 .name = name,
5194 };
5195 struct nfs4_secinfo_res res = {
5196 .flavors = flavors,
5197 };
5198 struct rpc_message msg = {
5199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5200 .rpc_argp = &args,
5201 .rpc_resp = &res,
5202 };
5203
5204 dprintk("NFS call secinfo %s\n", name->name);
5205 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5206 dprintk("NFS reply secinfo: %d\n", status);
5207 return status;
5208}
5209
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005210int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5211 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005212{
5213 struct nfs4_exception exception = { };
5214 int err;
5215 do {
5216 err = nfs4_handle_exception(NFS_SERVER(dir),
5217 _nfs4_proc_secinfo(dir, name, flavors),
5218 &exception);
5219 } while (exception.retry);
5220 return err;
5221}
5222
Andy Adamson557134a2009-04-01 09:21:53 -04005223#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005224/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005225 * Check the exchange flags returned by the server for invalid flags, having
5226 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5227 * DS flags set.
5228 */
5229static int nfs4_check_cl_exchange_flags(u32 flags)
5230{
5231 if (flags & ~EXCHGID4_FLAG_MASK_R)
5232 goto out_inval;
5233 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5234 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5235 goto out_inval;
5236 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5237 goto out_inval;
5238 return NFS_OK;
5239out_inval:
5240 return -NFS4ERR_INVAL;
5241}
5242
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005243static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005244nfs41_same_server_scope(struct nfs41_server_scope *a,
5245 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005246{
5247 if (a->server_scope_sz == b->server_scope_sz &&
5248 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5249 return true;
5250
5251 return false;
5252}
5253
Andy Adamson357f54d2010-12-14 10:11:57 -05005254/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005255 * nfs4_proc_bind_conn_to_session()
5256 *
5257 * The 4.1 client currently uses the same TCP connection for the
5258 * fore and backchannel.
5259 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005260int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005261{
5262 int status;
5263 struct nfs41_bind_conn_to_session_res res;
5264 struct rpc_message msg = {
5265 .rpc_proc =
5266 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5267 .rpc_argp = clp,
5268 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005269 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005270 };
5271
5272 dprintk("--> %s\n", __func__);
5273 BUG_ON(clp == NULL);
5274
5275 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5276 if (unlikely(res.session == NULL)) {
5277 status = -ENOMEM;
5278 goto out;
5279 }
5280
5281 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5282 if (status == 0) {
5283 if (memcmp(res.session->sess_id.data,
5284 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5285 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5286 status = -EIO;
5287 goto out_session;
5288 }
5289 if (res.dir != NFS4_CDFS4_BOTH) {
5290 dprintk("NFS: %s: Unexpected direction from server\n",
5291 __func__);
5292 status = -EIO;
5293 goto out_session;
5294 }
5295 if (res.use_conn_in_rdma_mode) {
5296 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5297 __func__);
5298 status = -EIO;
5299 goto out_session;
5300 }
5301 }
5302out_session:
5303 kfree(res.session);
5304out:
5305 dprintk("<-- %s status= %d\n", __func__, status);
5306 return status;
5307}
5308
5309/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005310 * nfs4_proc_exchange_id()
5311 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005312 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5313 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005314 * Since the clientid has expired, all compounds using sessions
5315 * associated with the stale clientid will be returning
5316 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5317 * be in some phase of session reset.
5318 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005319int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005320{
5321 nfs4_verifier verifier;
5322 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005323 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005324 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005325 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005326 };
5327 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005328 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005329 };
5330 int status;
5331 struct rpc_message msg = {
5332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5333 .rpc_argp = &args,
5334 .rpc_resp = &res,
5335 .rpc_cred = cred,
5336 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005337
Chuck Leverf0920752012-05-21 22:45:41 -04005338 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005339 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5340 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005341 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5342 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5343 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005344
Chuck Leveracdeb692012-05-21 22:46:16 -04005345 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005346 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005347 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005348 status = -ENOMEM;
5349 goto out;
5350 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005351
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005352 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005353 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005354 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005355 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005356 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005357 }
5358
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005359 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005360 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005361 status = -ENOMEM;
5362 goto out_server_scope;
5363 }
5364
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005365 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005366 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005367 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005368
Chuck Lever177313f2012-05-21 22:44:58 -04005369 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005370 clp->cl_clientid = res.clientid;
5371 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5372 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5373 clp->cl_seqid = res.seqid;
5374
Chuck Leveracdeb692012-05-21 22:46:16 -04005375 kfree(clp->cl_serverowner);
5376 clp->cl_serverowner = res.server_owner;
5377 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005378
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005379 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005380 kfree(clp->cl_implid);
5381 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005382
Chuck Lever177313f2012-05-21 22:44:58 -04005383 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005384 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005385 res.server_scope)) {
5386 dprintk("%s: server_scope mismatch detected\n",
5387 __func__);
5388 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005389 kfree(clp->cl_serverscope);
5390 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005391 }
5392
Chuck Lever177313f2012-05-21 22:44:58 -04005393 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005394 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005395 goto out;
5396 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005397 } else
5398 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005399
Chuck Leveracdeb692012-05-21 22:46:16 -04005400out_server_owner:
5401 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005402out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005403 kfree(res.server_scope);
5404out:
Chuck Lever177313f2012-05-21 22:44:58 -04005405 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005406 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005407 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005408 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005409 clp->cl_implid->date.seconds,
5410 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005411 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005412 return status;
5413}
5414
Trond Myklebust66245532012-05-25 17:18:09 -04005415static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5416 struct rpc_cred *cred)
5417{
5418 struct rpc_message msg = {
5419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5420 .rpc_argp = clp,
5421 .rpc_cred = cred,
5422 };
5423 int status;
5424
5425 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5426 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005427 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005428 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5429 return status;
5430}
5431
5432static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5433 struct rpc_cred *cred)
5434{
5435 unsigned int loop;
5436 int ret;
5437
5438 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5439 ret = _nfs4_proc_destroy_clientid(clp, cred);
5440 switch (ret) {
5441 case -NFS4ERR_DELAY:
5442 case -NFS4ERR_CLIENTID_BUSY:
5443 ssleep(1);
5444 break;
5445 default:
5446 return ret;
5447 }
5448 }
5449 return 0;
5450}
5451
5452int nfs4_destroy_clientid(struct nfs_client *clp)
5453{
5454 struct rpc_cred *cred;
5455 int ret = 0;
5456
5457 if (clp->cl_mvops->minor_version < 1)
5458 goto out;
5459 if (clp->cl_exchange_flags == 0)
5460 goto out;
5461 cred = nfs4_get_exchange_id_cred(clp);
5462 ret = nfs4_proc_destroy_clientid(clp, cred);
5463 if (cred)
5464 put_rpccred(cred);
5465 switch (ret) {
5466 case 0:
5467 case -NFS4ERR_STALE_CLIENTID:
5468 clp->cl_exchange_flags = 0;
5469 }
5470out:
5471 return ret;
5472}
5473
Andy Adamson2050f0c2009-04-01 09:22:30 -04005474struct nfs4_get_lease_time_data {
5475 struct nfs4_get_lease_time_args *args;
5476 struct nfs4_get_lease_time_res *res;
5477 struct nfs_client *clp;
5478};
5479
5480static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5481 void *calldata)
5482{
5483 int ret;
5484 struct nfs4_get_lease_time_data *data =
5485 (struct nfs4_get_lease_time_data *)calldata;
5486
5487 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005488 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005489 /* just setup sequence, do not trigger session recovery
5490 since we're invoked within one */
5491 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005492 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005493 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005494
5495 BUG_ON(ret == -EAGAIN);
5496 rpc_call_start(task);
5497 dprintk("<-- %s\n", __func__);
5498}
5499
5500/*
5501 * Called from nfs4_state_manager thread for session setup, so don't recover
5502 * from sequence operation or clientid errors.
5503 */
5504static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5505{
5506 struct nfs4_get_lease_time_data *data =
5507 (struct nfs4_get_lease_time_data *)calldata;
5508
5509 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005510 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5511 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005512 switch (task->tk_status) {
5513 case -NFS4ERR_DELAY:
5514 case -NFS4ERR_GRACE:
5515 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5516 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5517 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005518 /* fall through */
5519 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005520 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005521 return;
5522 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005523 dprintk("<-- %s\n", __func__);
5524}
5525
Trond Myklebust17280172012-03-11 13:11:00 -04005526static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005527 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5528 .rpc_call_done = nfs4_get_lease_time_done,
5529};
5530
5531int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5532{
5533 struct rpc_task *task;
5534 struct nfs4_get_lease_time_args args;
5535 struct nfs4_get_lease_time_res res = {
5536 .lr_fsinfo = fsinfo,
5537 };
5538 struct nfs4_get_lease_time_data data = {
5539 .args = &args,
5540 .res = &res,
5541 .clp = clp,
5542 };
5543 struct rpc_message msg = {
5544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5545 .rpc_argp = &args,
5546 .rpc_resp = &res,
5547 };
5548 struct rpc_task_setup task_setup = {
5549 .rpc_client = clp->cl_rpcclient,
5550 .rpc_message = &msg,
5551 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005552 .callback_data = &data,
5553 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005554 };
5555 int status;
5556
Trond Myklebust9d12b212012-01-17 22:04:25 -05005557 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005558 dprintk("--> %s\n", __func__);
5559 task = rpc_run_task(&task_setup);
5560
5561 if (IS_ERR(task))
5562 status = PTR_ERR(task);
5563 else {
5564 status = task->tk_status;
5565 rpc_put_task(task);
5566 }
5567 dprintk("<-- %s return %d\n", __func__, status);
5568
5569 return status;
5570}
5571
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005572static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5573{
5574 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5575}
5576
5577static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5578 struct nfs4_slot *new,
5579 u32 max_slots,
5580 u32 ivalue)
5581{
5582 struct nfs4_slot *old = NULL;
5583 u32 i;
5584
5585 spin_lock(&tbl->slot_tbl_lock);
5586 if (new) {
5587 old = tbl->slots;
5588 tbl->slots = new;
5589 tbl->max_slots = max_slots;
5590 }
5591 tbl->highest_used_slotid = -1; /* no slot is currently used */
5592 for (i = 0; i < tbl->max_slots; i++)
5593 tbl->slots[i].seq_nr = ivalue;
5594 spin_unlock(&tbl->slot_tbl_lock);
5595 kfree(old);
5596}
5597
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005598/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005599 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005600 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005601static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5602 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005603{
Andy Adamson104aeba2010-01-14 17:45:10 -05005604 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005605 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005606
Andy Adamson104aeba2010-01-14 17:45:10 -05005607 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5608 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005609
Andy Adamson104aeba2010-01-14 17:45:10 -05005610 /* Does the newly negotiated max_reqs match the existing slot table? */
5611 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005612 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005613 if (!new)
5614 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005615 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005616 ret = 0;
5617
5618 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005619 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5620 tbl, tbl->slots, tbl->max_slots);
5621out:
5622 dprintk("<-- %s: return %d\n", __func__, ret);
5623 return ret;
5624}
5625
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005626/* Destroy the slot table */
5627static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5628{
5629 if (session->fc_slot_table.slots != NULL) {
5630 kfree(session->fc_slot_table.slots);
5631 session->fc_slot_table.slots = NULL;
5632 }
5633 if (session->bc_slot_table.slots != NULL) {
5634 kfree(session->bc_slot_table.slots);
5635 session->bc_slot_table.slots = NULL;
5636 }
5637 return;
5638}
5639
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005640/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005641 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005642 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005643static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005644{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005645 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005646 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005647
Andy Adamsonaacd5532011-11-09 13:58:21 -05005648 dprintk("--> %s\n", __func__);
5649 /* Fore channel */
5650 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005651 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5652 if (status) /* -ENOMEM */
5653 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005654 /* Back channel */
5655 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005656 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5657 if (status && tbl->slots == NULL)
5658 /* Fore and back channel share a connection so get
5659 * both slot tables or neither */
5660 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005661 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005662}
5663
5664struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5665{
5666 struct nfs4_session *session;
5667 struct nfs4_slot_table *tbl;
5668
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005669 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005670 if (!session)
5671 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005672
Andy Adamson557134a2009-04-01 09:21:53 -04005673 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005674 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005675 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005676 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005677 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005678
5679 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005680 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005681 spin_lock_init(&tbl->slot_tbl_lock);
5682 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005683 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005684
Trond Myklebust1055d762010-06-16 09:52:27 -04005685 session->session_state = 1<<NFS4_SESSION_INITING;
5686
Andy Adamson557134a2009-04-01 09:21:53 -04005687 session->clp = clp;
5688 return session;
5689}
5690
5691void nfs4_destroy_session(struct nfs4_session *session)
5692{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005693 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005694 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005695
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005696 cred = nfs4_get_exchange_id_cred(session->clp);
5697 nfs4_proc_destroy_session(session, cred);
5698 if (cred)
5699 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005700
5701 rcu_read_lock();
5702 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5703 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005704 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005705 __func__, xprt);
5706 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005707 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005708 kfree(session);
5709}
5710
Andy Adamsonfc931582009-04-01 09:22:31 -04005711/*
5712 * Initialize the values to be used by the client in CREATE_SESSION
5713 * If nfs4_init_session set the fore channel request and response sizes,
5714 * use them.
5715 *
5716 * Set the back channel max_resp_sz_cached to zero to force the client to
5717 * always set csa_cachethis to FALSE because the current implementation
5718 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5719 */
5720static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5721{
5722 struct nfs4_session *session = args->client->cl_session;
5723 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5724 mxresp_sz = session->fc_attrs.max_resp_sz;
5725
5726 if (mxrqst_sz == 0)
5727 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5728 if (mxresp_sz == 0)
5729 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5730 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005731 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5732 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005733 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005734 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005735
5736 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005737 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005738 __func__,
5739 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005740 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005741
5742 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005743 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5744 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5745 args->bc_attrs.max_resp_sz_cached = 0;
5746 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5747 args->bc_attrs.max_reqs = 1;
5748
5749 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5750 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5751 __func__,
5752 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5753 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5754 args->bc_attrs.max_reqs);
5755}
5756
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005757static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005758{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005759 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5760 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5761
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005762 if (rcvd->max_resp_sz > sent->max_resp_sz)
5763 return -EINVAL;
5764 /*
5765 * Our requested max_ops is the minimum we need; we're not
5766 * prepared to break up compounds into smaller pieces than that.
5767 * So, no point even trying to continue if the server won't
5768 * cooperate:
5769 */
5770 if (rcvd->max_ops < sent->max_ops)
5771 return -EINVAL;
5772 if (rcvd->max_reqs == 0)
5773 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005774 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5775 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005776 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005777}
5778
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005779static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5780{
5781 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5782 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005783
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005784 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5785 return -EINVAL;
5786 if (rcvd->max_resp_sz < sent->max_resp_sz)
5787 return -EINVAL;
5788 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5789 return -EINVAL;
5790 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005791 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005792 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005793 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005794 return -EINVAL;
5795 return 0;
5796}
Andy Adamson8d353012009-04-01 09:22:32 -04005797
Andy Adamson8d353012009-04-01 09:22:32 -04005798static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5799 struct nfs4_session *session)
5800{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005801 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005802
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005803 ret = nfs4_verify_fore_channel_attrs(args, session);
5804 if (ret)
5805 return ret;
5806 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005807}
5808
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005809static int _nfs4_proc_create_session(struct nfs_client *clp,
5810 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005811{
5812 struct nfs4_session *session = clp->cl_session;
5813 struct nfs41_create_session_args args = {
5814 .client = clp,
5815 .cb_program = NFS4_CALLBACK,
5816 };
5817 struct nfs41_create_session_res res = {
5818 .client = clp,
5819 };
5820 struct rpc_message msg = {
5821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5822 .rpc_argp = &args,
5823 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005824 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005825 };
5826 int status;
5827
5828 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005829 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005830
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005831 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005832
Andy Adamson8d353012009-04-01 09:22:32 -04005833 if (!status)
5834 /* Verify the session's negotiated channel_attrs values */
5835 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005836 if (!status) {
5837 /* Increment the clientid slot sequence id */
5838 clp->cl_seqid++;
5839 }
5840
5841 return status;
5842}
5843
5844/*
5845 * Issues a CREATE_SESSION operation to the server.
5846 * It is the responsibility of the caller to verify the session is
5847 * expired before calling this routine.
5848 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005849int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005850{
5851 int status;
5852 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005853 struct nfs4_session *session = clp->cl_session;
5854
5855 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5856
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005857 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005858 if (status)
5859 goto out;
5860
Andy Adamsonaacd5532011-11-09 13:58:21 -05005861 /* Init or reset the session slot tables */
5862 status = nfs4_setup_session_slot_tables(session);
5863 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005864 if (status)
5865 goto out;
5866
5867 ptr = (unsigned *)&session->sess_id.data[0];
5868 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5869 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005870out:
5871 dprintk("<-- %s\n", __func__);
5872 return status;
5873}
5874
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005875/*
5876 * Issue the over-the-wire RPC DESTROY_SESSION.
5877 * The caller must serialize access to this routine.
5878 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005879int nfs4_proc_destroy_session(struct nfs4_session *session,
5880 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005881{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005882 struct rpc_message msg = {
5883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5884 .rpc_argp = session,
5885 .rpc_cred = cred,
5886 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005887 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005888
5889 dprintk("--> nfs4_proc_destroy_session\n");
5890
5891 /* session is still being setup */
5892 if (session->clp->cl_cons_state != NFS_CS_READY)
5893 return status;
5894
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005895 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005896
5897 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005898 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005899 "Session has been destroyed regardless...\n", status);
5900
5901 dprintk("<-- nfs4_proc_destroy_session\n");
5902 return status;
5903}
5904
Trond Myklebust7b38c362012-05-23 13:23:31 -04005905/*
5906 * With sessions, the client is not marked ready until after a
5907 * successful EXCHANGE_ID and CREATE_SESSION.
5908 *
5909 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5910 * other versions of NFS can be tried.
5911 */
5912static int nfs41_check_session_ready(struct nfs_client *clp)
5913{
5914 int ret;
5915
5916 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5917 ret = nfs4_client_recover_expired_lease(clp);
5918 if (ret)
5919 return ret;
5920 }
5921 if (clp->cl_cons_state < NFS_CS_READY)
5922 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005923 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005924 return 0;
5925}
5926
Trond Myklebustfccba802009-07-21 16:48:07 -04005927int nfs4_init_session(struct nfs_server *server)
5928{
5929 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005930 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005931 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005932
5933 if (!nfs4_has_session(clp))
5934 return 0;
5935
Trond Myklebust1055d762010-06-16 09:52:27 -04005936 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005937 spin_lock(&clp->cl_lock);
5938 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005939
Trond Myklebust7b38c362012-05-23 13:23:31 -04005940 rsize = server->rsize;
5941 if (rsize == 0)
5942 rsize = NFS_MAX_FILE_IO_SIZE;
5943 wsize = server->wsize;
5944 if (wsize == 0)
5945 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005946
Trond Myklebust7b38c362012-05-23 13:23:31 -04005947 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5948 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5949 }
5950 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005951
Trond Myklebust7b38c362012-05-23 13:23:31 -04005952 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005953}
5954
Trond Myklebust7b38c362012-05-23 13:23:31 -04005955int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005956{
5957 struct nfs4_session *session = clp->cl_session;
5958 int ret;
5959
Trond Myklebust7b38c362012-05-23 13:23:31 -04005960 spin_lock(&clp->cl_lock);
5961 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5962 /*
5963 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5964 * DS lease to be equal to the MDS lease.
5965 */
5966 clp->cl_lease_time = lease_time;
5967 clp->cl_last_renewal = jiffies;
5968 }
5969 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005970
Trond Myklebust7b38c362012-05-23 13:23:31 -04005971 ret = nfs41_check_session_ready(clp);
5972 if (ret)
5973 return ret;
5974 /* Test for the DS role */
5975 if (!is_ds_client(clp))
5976 return -ENODEV;
5977 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005978}
5979EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5980
5981
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005982/*
5983 * Renew the cl_session lease.
5984 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005985struct nfs4_sequence_data {
5986 struct nfs_client *clp;
5987 struct nfs4_sequence_args args;
5988 struct nfs4_sequence_res res;
5989};
5990
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005991static void nfs41_sequence_release(void *data)
5992{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005993 struct nfs4_sequence_data *calldata = data;
5994 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005995
Alexandros Batsakis71358402010-02-05 03:45:05 -08005996 if (atomic_read(&clp->cl_count) > 1)
5997 nfs4_schedule_state_renewal(clp);
5998 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005999 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006000}
6001
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006002static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6003{
6004 switch(task->tk_status) {
6005 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006006 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6007 return -EAGAIN;
6008 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006009 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006010 }
6011 return 0;
6012}
6013
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006014static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006015{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006016 struct nfs4_sequence_data *calldata = data;
6017 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006018
Trond Myklebust14516c32010-07-31 14:29:06 -04006019 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6020 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006021
6022 if (task->tk_status < 0) {
6023 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006024 if (atomic_read(&clp->cl_count) == 1)
6025 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006026
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006027 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6028 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006029 return;
6030 }
6031 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006032 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006033out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006034 dprintk("<-- %s\n", __func__);
6035}
6036
6037static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6038{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006039 struct nfs4_sequence_data *calldata = data;
6040 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006041 struct nfs4_sequence_args *args;
6042 struct nfs4_sequence_res *res;
6043
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006044 args = task->tk_msg.rpc_argp;
6045 res = task->tk_msg.rpc_resp;
6046
Trond Myklebust9d12b212012-01-17 22:04:25 -05006047 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006048 return;
6049 rpc_call_start(task);
6050}
6051
6052static const struct rpc_call_ops nfs41_sequence_ops = {
6053 .rpc_call_done = nfs41_sequence_call_done,
6054 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006055 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006056};
6057
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006058static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006059{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006060 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006061 struct rpc_message msg = {
6062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6063 .rpc_cred = cred,
6064 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006065 struct rpc_task_setup task_setup_data = {
6066 .rpc_client = clp->cl_rpcclient,
6067 .rpc_message = &msg,
6068 .callback_ops = &nfs41_sequence_ops,
6069 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6070 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006071
Alexandros Batsakis71358402010-02-05 03:45:05 -08006072 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006073 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006074 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006075 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006076 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006077 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006078 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006079 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006080 msg.rpc_argp = &calldata->args;
6081 msg.rpc_resp = &calldata->res;
6082 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006083 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006084
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006085 return rpc_run_task(&task_setup_data);
6086}
6087
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006088static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006089{
6090 struct rpc_task *task;
6091 int ret = 0;
6092
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006093 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6094 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006095 task = _nfs41_proc_sequence(clp, cred);
6096 if (IS_ERR(task))
6097 ret = PTR_ERR(task);
6098 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006099 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006100 dprintk("<-- %s status=%d\n", __func__, ret);
6101 return ret;
6102}
6103
6104static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6105{
6106 struct rpc_task *task;
6107 int ret;
6108
6109 task = _nfs41_proc_sequence(clp, cred);
6110 if (IS_ERR(task)) {
6111 ret = PTR_ERR(task);
6112 goto out;
6113 }
6114 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006115 if (!ret) {
6116 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6117
6118 if (task->tk_status == 0)
6119 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006120 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006121 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006122 rpc_put_task(task);
6123out:
6124 dprintk("<-- %s status=%d\n", __func__, ret);
6125 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006126}
6127
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006128struct nfs4_reclaim_complete_data {
6129 struct nfs_client *clp;
6130 struct nfs41_reclaim_complete_args arg;
6131 struct nfs41_reclaim_complete_res res;
6132};
6133
6134static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6135{
6136 struct nfs4_reclaim_complete_data *calldata = data;
6137
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006138 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006139 if (nfs41_setup_sequence(calldata->clp->cl_session,
6140 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006141 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006142 return;
6143
6144 rpc_call_start(task);
6145}
6146
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006147static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6148{
6149 switch(task->tk_status) {
6150 case 0:
6151 case -NFS4ERR_COMPLETE_ALREADY:
6152 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6153 break;
6154 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006155 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006156 /* fall through */
6157 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006158 return -EAGAIN;
6159 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006160 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006161 }
6162 return 0;
6163}
6164
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006165static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6166{
6167 struct nfs4_reclaim_complete_data *calldata = data;
6168 struct nfs_client *clp = calldata->clp;
6169 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6170
6171 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006172 if (!nfs41_sequence_done(task, res))
6173 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006174
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006175 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6176 rpc_restart_call_prepare(task);
6177 return;
6178 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006179 dprintk("<-- %s\n", __func__);
6180}
6181
6182static void nfs4_free_reclaim_complete_data(void *data)
6183{
6184 struct nfs4_reclaim_complete_data *calldata = data;
6185
6186 kfree(calldata);
6187}
6188
6189static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6190 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6191 .rpc_call_done = nfs4_reclaim_complete_done,
6192 .rpc_release = nfs4_free_reclaim_complete_data,
6193};
6194
6195/*
6196 * Issue a global reclaim complete.
6197 */
6198static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6199{
6200 struct nfs4_reclaim_complete_data *calldata;
6201 struct rpc_task *task;
6202 struct rpc_message msg = {
6203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6204 };
6205 struct rpc_task_setup task_setup_data = {
6206 .rpc_client = clp->cl_rpcclient,
6207 .rpc_message = &msg,
6208 .callback_ops = &nfs4_reclaim_complete_call_ops,
6209 .flags = RPC_TASK_ASYNC,
6210 };
6211 int status = -ENOMEM;
6212
6213 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006214 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006215 if (calldata == NULL)
6216 goto out;
6217 calldata->clp = clp;
6218 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006219
Trond Myklebust9d12b212012-01-17 22:04:25 -05006220 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006221 msg.rpc_argp = &calldata->arg;
6222 msg.rpc_resp = &calldata->res;
6223 task_setup_data.callback_data = calldata;
6224 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006225 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006226 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006227 goto out;
6228 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006229 status = nfs4_wait_for_completion_rpc_task(task);
6230 if (status == 0)
6231 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006232 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006233 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006234out:
6235 dprintk("<-- %s status=%d\n", __func__, status);
6236 return status;
6237}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006238
6239static void
6240nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6241{
6242 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006243 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006244
6245 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006246 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6247 * right now covering the LAYOUTGET we are about to send.
6248 * However, that is not so catastrophic, and there seems
6249 * to be no way to prevent it completely.
6250 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006251 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006252 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006253 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006254 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6255 NFS_I(lgp->args.inode)->layout,
6256 lgp->args.ctx->state)) {
6257 rpc_exit(task, NFS4_OK);
6258 return;
6259 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006260 rpc_call_start(task);
6261}
6262
6263static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6264{
6265 struct nfs4_layoutget *lgp = calldata;
6266 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6267
6268 dprintk("--> %s\n", __func__);
6269
6270 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6271 return;
6272
6273 switch (task->tk_status) {
6274 case 0:
6275 break;
6276 case -NFS4ERR_LAYOUTTRYLATER:
6277 case -NFS4ERR_RECALLCONFLICT:
6278 task->tk_status = -NFS4ERR_DELAY;
6279 /* Fall through */
6280 default:
6281 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6282 rpc_restart_call_prepare(task);
6283 return;
6284 }
6285 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006286 dprintk("<-- %s\n", __func__);
6287}
6288
Idan Kedar85541162012-08-02 11:47:10 +03006289static size_t max_response_pages(struct nfs_server *server)
6290{
6291 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6292 return nfs_page_array_len(0, max_resp_sz);
6293}
6294
6295static void nfs4_free_pages(struct page **pages, size_t size)
6296{
6297 int i;
6298
6299 if (!pages)
6300 return;
6301
6302 for (i = 0; i < size; i++) {
6303 if (!pages[i])
6304 break;
6305 __free_page(pages[i]);
6306 }
6307 kfree(pages);
6308}
6309
6310static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6311{
6312 struct page **pages;
6313 int i;
6314
6315 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6316 if (!pages) {
6317 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6318 return NULL;
6319 }
6320
6321 for (i = 0; i < size; i++) {
6322 pages[i] = alloc_page(gfp_flags);
6323 if (!pages[i]) {
6324 dprintk("%s: failed to allocate page\n", __func__);
6325 nfs4_free_pages(pages, size);
6326 return NULL;
6327 }
6328 }
6329
6330 return pages;
6331}
6332
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006333static void nfs4_layoutget_release(void *calldata)
6334{
6335 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006336 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6337 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006338
6339 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006340 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006341 put_nfs_open_context(lgp->args.ctx);
6342 kfree(calldata);
6343 dprintk("<-- %s\n", __func__);
6344}
6345
6346static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6347 .rpc_call_prepare = nfs4_layoutget_prepare,
6348 .rpc_call_done = nfs4_layoutget_done,
6349 .rpc_release = nfs4_layoutget_release,
6350};
6351
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006352struct pnfs_layout_segment *
6353nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006354{
6355 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006356 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006357 struct rpc_task *task;
6358 struct rpc_message msg = {
6359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6360 .rpc_argp = &lgp->args,
6361 .rpc_resp = &lgp->res,
6362 };
6363 struct rpc_task_setup task_setup_data = {
6364 .rpc_client = server->client,
6365 .rpc_message = &msg,
6366 .callback_ops = &nfs4_layoutget_call_ops,
6367 .callback_data = lgp,
6368 .flags = RPC_TASK_ASYNC,
6369 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006370 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006371 int status = 0;
6372
6373 dprintk("--> %s\n", __func__);
6374
Idan Kedar85541162012-08-02 11:47:10 +03006375 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6376 if (!lgp->args.layout.pages) {
6377 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006378 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006379 }
6380 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6381
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006382 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006383 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006384 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006385 task = rpc_run_task(&task_setup_data);
6386 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006387 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006388 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006389 if (status == 0)
6390 status = task->tk_status;
6391 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006392 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006393 rpc_put_task(task);
6394 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006395 if (status)
6396 return ERR_PTR(status);
6397 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006398}
6399
Benny Halevycbe82602011-05-22 19:52:37 +03006400static void
6401nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6402{
6403 struct nfs4_layoutreturn *lrp = calldata;
6404
6405 dprintk("--> %s\n", __func__);
6406 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006407 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006408 return;
6409 rpc_call_start(task);
6410}
6411
6412static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6413{
6414 struct nfs4_layoutreturn *lrp = calldata;
6415 struct nfs_server *server;
6416
6417 dprintk("--> %s\n", __func__);
6418
6419 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6420 return;
6421
6422 server = NFS_SERVER(lrp->args.inode);
6423 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006424 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006425 return;
6426 }
Benny Halevycbe82602011-05-22 19:52:37 +03006427 dprintk("<-- %s\n", __func__);
6428}
6429
6430static void nfs4_layoutreturn_release(void *calldata)
6431{
6432 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006433 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006434
6435 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006436 spin_lock(&lo->plh_inode->i_lock);
6437 if (lrp->res.lrs_present)
6438 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6439 lo->plh_block_lgets--;
6440 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006441 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006442 kfree(calldata);
6443 dprintk("<-- %s\n", __func__);
6444}
6445
6446static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6447 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6448 .rpc_call_done = nfs4_layoutreturn_done,
6449 .rpc_release = nfs4_layoutreturn_release,
6450};
6451
6452int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6453{
6454 struct rpc_task *task;
6455 struct rpc_message msg = {
6456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6457 .rpc_argp = &lrp->args,
6458 .rpc_resp = &lrp->res,
6459 };
6460 struct rpc_task_setup task_setup_data = {
6461 .rpc_client = lrp->clp->cl_rpcclient,
6462 .rpc_message = &msg,
6463 .callback_ops = &nfs4_layoutreturn_call_ops,
6464 .callback_data = lrp,
6465 };
6466 int status;
6467
6468 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006469 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006470 task = rpc_run_task(&task_setup_data);
6471 if (IS_ERR(task))
6472 return PTR_ERR(task);
6473 status = task->tk_status;
6474 dprintk("<-- %s status=%d\n", __func__, status);
6475 rpc_put_task(task);
6476 return status;
6477}
6478
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006479/*
6480 * Retrieve the list of Data Server devices from the MDS.
6481 */
6482static int _nfs4_getdevicelist(struct nfs_server *server,
6483 const struct nfs_fh *fh,
6484 struct pnfs_devicelist *devlist)
6485{
6486 struct nfs4_getdevicelist_args args = {
6487 .fh = fh,
6488 .layoutclass = server->pnfs_curr_ld->id,
6489 };
6490 struct nfs4_getdevicelist_res res = {
6491 .devlist = devlist,
6492 };
6493 struct rpc_message msg = {
6494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6495 .rpc_argp = &args,
6496 .rpc_resp = &res,
6497 };
6498 int status;
6499
6500 dprintk("--> %s\n", __func__);
6501 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6502 &res.seq_res, 0);
6503 dprintk("<-- %s status=%d\n", __func__, status);
6504 return status;
6505}
6506
6507int nfs4_proc_getdevicelist(struct nfs_server *server,
6508 const struct nfs_fh *fh,
6509 struct pnfs_devicelist *devlist)
6510{
6511 struct nfs4_exception exception = { };
6512 int err;
6513
6514 do {
6515 err = nfs4_handle_exception(server,
6516 _nfs4_getdevicelist(server, fh, devlist),
6517 &exception);
6518 } while (exception.retry);
6519
6520 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6521 err, devlist->num_devs);
6522
6523 return err;
6524}
6525EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6526
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006527static int
6528_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6529{
6530 struct nfs4_getdeviceinfo_args args = {
6531 .pdev = pdev,
6532 };
6533 struct nfs4_getdeviceinfo_res res = {
6534 .pdev = pdev,
6535 };
6536 struct rpc_message msg = {
6537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6538 .rpc_argp = &args,
6539 .rpc_resp = &res,
6540 };
6541 int status;
6542
6543 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006544 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006545 dprintk("<-- %s status=%d\n", __func__, status);
6546
6547 return status;
6548}
6549
6550int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6551{
6552 struct nfs4_exception exception = { };
6553 int err;
6554
6555 do {
6556 err = nfs4_handle_exception(server,
6557 _nfs4_proc_getdeviceinfo(server, pdev),
6558 &exception);
6559 } while (exception.retry);
6560 return err;
6561}
6562EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6563
Andy Adamson863a3c62011-03-23 13:27:54 +00006564static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6565{
6566 struct nfs4_layoutcommit_data *data = calldata;
6567 struct nfs_server *server = NFS_SERVER(data->args.inode);
6568
6569 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006570 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006571 return;
6572 rpc_call_start(task);
6573}
6574
6575static void
6576nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6577{
6578 struct nfs4_layoutcommit_data *data = calldata;
6579 struct nfs_server *server = NFS_SERVER(data->args.inode);
6580
6581 if (!nfs4_sequence_done(task, &data->res.seq_res))
6582 return;
6583
6584 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006585 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6586 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6587 case -NFS4ERR_BADLAYOUT: /* no layout */
6588 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006589 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006590 break;
6591 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006592 nfs_post_op_update_inode_force_wcc(data->args.inode,
6593 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006594 break;
6595 default:
6596 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6597 rpc_restart_call_prepare(task);
6598 return;
6599 }
6600 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006601}
6602
6603static void nfs4_layoutcommit_release(void *calldata)
6604{
6605 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006606 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006607 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006608
Andy Adamsondb29c082011-07-30 20:52:38 -04006609 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006610 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006611 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6612 list_del_init(&lseg->pls_lc_list);
6613 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6614 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006615 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006616 }
Peng Tao92407e72011-10-23 20:21:17 -07006617
6618 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6619 smp_mb__after_clear_bit();
6620 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6621
Andy Adamson863a3c62011-03-23 13:27:54 +00006622 put_rpccred(data->cred);
6623 kfree(data);
6624}
6625
6626static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6627 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6628 .rpc_call_done = nfs4_layoutcommit_done,
6629 .rpc_release = nfs4_layoutcommit_release,
6630};
6631
6632int
Andy Adamsonef311532011-03-12 02:58:10 -05006633nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006634{
6635 struct rpc_message msg = {
6636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6637 .rpc_argp = &data->args,
6638 .rpc_resp = &data->res,
6639 .rpc_cred = data->cred,
6640 };
6641 struct rpc_task_setup task_setup_data = {
6642 .task = &data->task,
6643 .rpc_client = NFS_CLIENT(data->args.inode),
6644 .rpc_message = &msg,
6645 .callback_ops = &nfs4_layoutcommit_ops,
6646 .callback_data = data,
6647 .flags = RPC_TASK_ASYNC,
6648 };
6649 struct rpc_task *task;
6650 int status = 0;
6651
6652 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6653 "lbw: %llu inode %lu\n",
6654 data->task.tk_pid, sync,
6655 data->args.lastbytewritten,
6656 data->args.inode->i_ino);
6657
Trond Myklebust9d12b212012-01-17 22:04:25 -05006658 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006659 task = rpc_run_task(&task_setup_data);
6660 if (IS_ERR(task))
6661 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006662 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006663 goto out;
6664 status = nfs4_wait_for_completion_rpc_task(task);
6665 if (status != 0)
6666 goto out;
6667 status = task->tk_status;
6668out:
6669 dprintk("%s: status %d\n", __func__, status);
6670 rpc_put_task(task);
6671 return status;
6672}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006673
6674static int
6675_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6676 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6677{
6678 struct nfs41_secinfo_no_name_args args = {
6679 .style = SECINFO_STYLE_CURRENT_FH,
6680 };
6681 struct nfs4_secinfo_res res = {
6682 .flavors = flavors,
6683 };
6684 struct rpc_message msg = {
6685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6686 .rpc_argp = &args,
6687 .rpc_resp = &res,
6688 };
6689 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6690}
6691
6692static int
6693nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6694 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6695{
6696 struct nfs4_exception exception = { };
6697 int err;
6698 do {
6699 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6700 switch (err) {
6701 case 0:
6702 case -NFS4ERR_WRONGSEC:
6703 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006704 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006705 default:
6706 err = nfs4_handle_exception(server, err, &exception);
6707 }
6708 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006709out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006710 return err;
6711}
6712
6713static int
6714nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6715 struct nfs_fsinfo *info)
6716{
6717 int err;
6718 struct page *page;
6719 rpc_authflavor_t flavor;
6720 struct nfs4_secinfo_flavors *flavors;
6721
6722 page = alloc_page(GFP_KERNEL);
6723 if (!page) {
6724 err = -ENOMEM;
6725 goto out;
6726 }
6727
6728 flavors = page_address(page);
6729 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6730
6731 /*
6732 * Fall back on "guess and check" method if
6733 * the server doesn't support SECINFO_NO_NAME
6734 */
6735 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6736 err = nfs4_find_root_sec(server, fhandle, info);
6737 goto out_freepage;
6738 }
6739 if (err)
6740 goto out_freepage;
6741
6742 flavor = nfs_find_best_sec(flavors);
6743 if (err == 0)
6744 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6745
6746out_freepage:
6747 put_page(page);
6748 if (err == -EACCES)
6749 return -EPERM;
6750out:
6751 return err;
6752}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006753
6754static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006755{
6756 int status;
6757 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006758 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006759 };
6760 struct nfs41_test_stateid_res res;
6761 struct rpc_message msg = {
6762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6763 .rpc_argp = &args,
6764 .rpc_resp = &res,
6765 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006766
Chuck Lever38527b12012-07-11 16:30:23 -04006767 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006768 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006769 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006770 if (status != NFS_OK) {
6771 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006772 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006773 }
6774 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006775 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006776}
6777
Chuck Lever38527b12012-07-11 16:30:23 -04006778/**
6779 * nfs41_test_stateid - perform a TEST_STATEID operation
6780 *
6781 * @server: server / transport on which to perform the operation
6782 * @stateid: state ID to test
6783 *
6784 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6785 * Otherwise a negative NFS4ERR value is returned if the operation
6786 * failed or the state ID is not currently valid.
6787 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006788static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006789{
6790 struct nfs4_exception exception = { };
6791 int err;
6792 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006793 err = _nfs41_test_stateid(server, stateid);
6794 if (err != -NFS4ERR_DELAY)
6795 break;
6796 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006797 } while (exception.retry);
6798 return err;
6799}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006800
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006801static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006802{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006803 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006804 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006805 };
6806 struct nfs41_free_stateid_res res;
6807 struct rpc_message msg = {
6808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6809 .rpc_argp = &args,
6810 .rpc_resp = &res,
6811 };
Chuck Lever38527b12012-07-11 16:30:23 -04006812 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006813
Chuck Lever38527b12012-07-11 16:30:23 -04006814 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006815 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006816 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006817 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006818 dprintk("NFS reply free_stateid: %d\n", status);
6819 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006820}
6821
Chuck Lever38527b12012-07-11 16:30:23 -04006822/**
6823 * nfs41_free_stateid - perform a FREE_STATEID operation
6824 *
6825 * @server: server / transport on which to perform the operation
6826 * @stateid: state ID to release
6827 *
6828 * Returns NFS_OK if the server freed "stateid". Otherwise a
6829 * negative NFS4ERR value is returned.
6830 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006831static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006832{
6833 struct nfs4_exception exception = { };
6834 int err;
6835 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006836 err = _nfs4_free_stateid(server, stateid);
6837 if (err != -NFS4ERR_DELAY)
6838 break;
6839 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006840 } while (exception.retry);
6841 return err;
6842}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006843
6844static bool nfs41_match_stateid(const nfs4_stateid *s1,
6845 const nfs4_stateid *s2)
6846{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006847 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006848 return false;
6849
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006850 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006851 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006852 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006853 return true;
6854
6855 return false;
6856}
6857
Andy Adamson557134a2009-04-01 09:21:53 -04006858#endif /* CONFIG_NFS_V4_1 */
6859
Trond Myklebust36281ca2012-03-04 18:13:56 -05006860static bool nfs4_match_stateid(const nfs4_stateid *s1,
6861 const nfs4_stateid *s2)
6862{
Trond Myklebustf597c532012-03-04 18:13:56 -05006863 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006864}
6865
6866
Trond Myklebust17280172012-03-11 13:11:00 -04006867static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006868 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006869 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 .recover_open = nfs4_open_reclaim,
6871 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006872 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006873 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874};
6875
Andy Adamson591d71c2009-04-01 09:22:47 -04006876#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006877static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006878 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6879 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6880 .recover_open = nfs4_open_reclaim,
6881 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006882 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006883 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006884 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006885};
6886#endif /* CONFIG_NFS_V4_1 */
6887
Trond Myklebust17280172012-03-11 13:11:00 -04006888static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006889 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006890 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 .recover_open = nfs4_open_expired,
6892 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006893 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006894 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895};
6896
Andy Adamson591d71c2009-04-01 09:22:47 -04006897#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006898static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006899 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6900 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006901 .recover_open = nfs41_open_expired,
6902 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006903 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006904 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006905};
6906#endif /* CONFIG_NFS_V4_1 */
6907
Trond Myklebust17280172012-03-11 13:11:00 -04006908static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006909 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006910 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006911 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006912};
6913
6914#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006915static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006916 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006917 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006918 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006919};
6920#endif
6921
Trond Myklebust97dc1352010-06-16 09:52:26 -04006922static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6923 .minor_version = 0,
6924 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006925 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006926 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006927 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6928 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6929 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006930};
6931
6932#if defined(CONFIG_NFS_V4_1)
6933static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6934 .minor_version = 1,
6935 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006936 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006937 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006938 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6939 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6940 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006941};
6942#endif
6943
Trond Myklebust97dc1352010-06-16 09:52:26 -04006944const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6945 [0] = &nfs_v4_0_minor_ops,
6946#if defined(CONFIG_NFS_V4_1)
6947 [1] = &nfs_v4_1_minor_ops,
6948#endif
6949};
6950
Bryan Schumaker73a79702012-07-16 16:39:12 -04006951const struct inode_operations nfs4_dir_inode_operations = {
6952 .create = nfs_create,
6953 .lookup = nfs_lookup,
6954 .atomic_open = nfs_atomic_open,
6955 .link = nfs_link,
6956 .unlink = nfs_unlink,
6957 .symlink = nfs_symlink,
6958 .mkdir = nfs_mkdir,
6959 .rmdir = nfs_rmdir,
6960 .mknod = nfs_mknod,
6961 .rename = nfs_rename,
6962 .permission = nfs_permission,
6963 .getattr = nfs_getattr,
6964 .setattr = nfs_setattr,
6965 .getxattr = generic_getxattr,
6966 .setxattr = generic_setxattr,
6967 .listxattr = generic_listxattr,
6968 .removexattr = generic_removexattr,
6969};
6970
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006971static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006972 .permission = nfs_permission,
6973 .getattr = nfs_getattr,
6974 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006975 .getxattr = generic_getxattr,
6976 .setxattr = generic_setxattr,
6977 .listxattr = generic_listxattr,
6978 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006979};
6980
David Howells509de812006-08-22 20:06:11 -04006981const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 .version = 4, /* protocol version */
6983 .dentry_ops = &nfs4_dentry_operations,
6984 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006985 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006986 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006988 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006989 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 .getattr = nfs4_proc_getattr,
6991 .setattr = nfs4_proc_setattr,
6992 .lookup = nfs4_proc_lookup,
6993 .access = nfs4_proc_access,
6994 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 .create = nfs4_proc_create,
6996 .remove = nfs4_proc_remove,
6997 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006998 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 .unlink_done = nfs4_proc_unlink_done,
7000 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007001 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007002 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007003 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 .link = nfs4_proc_link,
7005 .symlink = nfs4_proc_symlink,
7006 .mkdir = nfs4_proc_mkdir,
7007 .rmdir = nfs4_proc_remove,
7008 .readdir = nfs4_proc_readdir,
7009 .mknod = nfs4_proc_mknod,
7010 .statfs = nfs4_proc_statfs,
7011 .fsinfo = nfs4_proc_fsinfo,
7012 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007013 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 .decode_dirent = nfs4_decode_dirent,
7015 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007016 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007017 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007018 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007020 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007021 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007022 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007024 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007025 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007027 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007028 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007029 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007030 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007031 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007032 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007033 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007034 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007035 .create_server = nfs4_create_server,
7036 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037};
7038
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007039static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7040 .prefix = XATTR_NAME_NFSV4_ACL,
7041 .list = nfs4_xattr_list_nfs4_acl,
7042 .get = nfs4_xattr_get_nfs4_acl,
7043 .set = nfs4_xattr_set_nfs4_acl,
7044};
7045
7046const struct xattr_handler *nfs4_xattr_handlers[] = {
7047 &nfs4_xattr_nfs4_acl_handler,
7048 NULL
7049};
7050
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051/*
7052 * Local variables:
7053 * c-basic-offset: 8
7054 * End:
7055 */