blob: 9aa97112426fdcc9d4c202f39089ec12eb93ae48 [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 Myklebust62ab4602009-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 Myklebust62ab4602009-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 Adamsoncbdabc72011-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 Adamsoncbdabc72011-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 Adamsoncbdabc72011-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 Adamsoncbdabc72011-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 Myklebust16b42892006-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 Myklebust16b42892006-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 Myklebust16b42892006-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 Myklebust16b42892006-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{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004071 char *nodename = clp->cl_rpcclient->cl_nodename;
4072
4073 if (nfs4_client_id_uniquifier[0] != '\0')
4074 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004075 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4076 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004077 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004078}
4079
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004080/**
4081 * nfs4_proc_setclientid - Negotiate client ID
4082 * @clp: state data structure
4083 * @program: RPC program for NFSv4 callback service
4084 * @port: IP port number for NFS4 callback service
4085 * @cred: RPC credential to use for this call
4086 * @res: where to place the result
4087 *
4088 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4089 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004090int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4091 unsigned short port, struct rpc_cred *cred,
4092 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
4094 nfs4_verifier sc_verifier;
4095 struct nfs4_setclientid setclientid = {
4096 .sc_verifier = &sc_verifier,
4097 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004098 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 };
4100 struct rpc_message msg = {
4101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4102 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004103 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004104 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004106 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Chuck Leverde734832012-07-11 16:30:50 -04004108 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004109 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004110 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4111 setclientid.sc_name_len =
4112 nfs4_init_uniform_client_string(clp,
4113 setclientid.sc_name,
4114 sizeof(setclientid.sc_name));
4115 else
4116 setclientid.sc_name_len =
4117 nfs4_init_nonuniform_client_string(clp,
4118 setclientid.sc_name,
4119 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004120 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004121 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004122 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004123 sizeof(setclientid.sc_netid),
4124 rpc_peeraddr2str(clp->cl_rpcclient,
4125 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004126 rcu_read_unlock();
4127 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004128 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 clp->cl_ipaddr, port >> 8, port & 255);
4130
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004131 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4132 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4133 setclientid.sc_name_len, setclientid.sc_name);
4134 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4135 dprintk("NFS reply setclientid: %d\n", status);
4136 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137}
4138
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004139/**
4140 * nfs4_proc_setclientid_confirm - Confirm client ID
4141 * @clp: state data structure
4142 * @res: result of a previous SETCLIENTID
4143 * @cred: RPC credential to use for this call
4144 *
4145 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4146 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004147int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004148 struct nfs4_setclientid_res *arg,
4149 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
4151 struct nfs_fsinfo fsinfo;
4152 struct rpc_message msg = {
4153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004154 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004156 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 };
4158 unsigned long now;
4159 int status;
4160
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004161 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4162 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4163 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004165 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 if (status == 0) {
4167 spin_lock(&clp->cl_lock);
4168 clp->cl_lease_time = fsinfo.lease_time * HZ;
4169 clp->cl_last_renewal = now;
4170 spin_unlock(&clp->cl_lock);
4171 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004172 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 return status;
4174}
4175
Trond Myklebustfe650402006-01-03 09:55:18 +01004176struct nfs4_delegreturndata {
4177 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004178 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004179 struct nfs_fh fh;
4180 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004181 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004182 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004183 int rpc_status;
4184};
4185
Trond Myklebustfe650402006-01-03 09:55:18 +01004186static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4187{
4188 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004189
Trond Myklebust14516c32010-07-31 14:29:06 -04004190 if (!nfs4_sequence_done(task, &data->res.seq_res))
4191 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004192
Ricardo Labiaga79708862009-12-07 09:23:21 -05004193 switch (task->tk_status) {
4194 case -NFS4ERR_STALE_STATEID:
4195 case -NFS4ERR_EXPIRED:
4196 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004197 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004198 break;
4199 default:
4200 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4201 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004202 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004203 return;
4204 }
4205 }
4206 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004207}
4208
4209static void nfs4_delegreturn_release(void *calldata)
4210{
Trond Myklebustfe650402006-01-03 09:55:18 +01004211 kfree(calldata);
4212}
4213
Andy Adamson938e1012009-04-01 09:22:28 -04004214#if defined(CONFIG_NFS_V4_1)
4215static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4216{
4217 struct nfs4_delegreturndata *d_data;
4218
4219 d_data = (struct nfs4_delegreturndata *)data;
4220
Trond Myklebust035168a2010-06-16 09:52:26 -04004221 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004222 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004223 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004224 return;
4225 rpc_call_start(task);
4226}
4227#endif /* CONFIG_NFS_V4_1 */
4228
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004229static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004230#if defined(CONFIG_NFS_V4_1)
4231 .rpc_call_prepare = nfs4_delegreturn_prepare,
4232#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004233 .rpc_call_done = nfs4_delegreturn_done,
4234 .rpc_release = nfs4_delegreturn_release,
4235};
4236
Trond Myklebuste6f81072008-01-24 18:14:34 -05004237static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004238{
4239 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004240 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004241 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004242 struct rpc_message msg = {
4243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4244 .rpc_cred = cred,
4245 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004246 struct rpc_task_setup task_setup_data = {
4247 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004248 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004249 .callback_ops = &nfs4_delegreturn_ops,
4250 .flags = RPC_TASK_ASYNC,
4251 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004252 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004253
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004254 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004255 if (data == NULL)
4256 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004257 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004258 data->args.fhandle = &data->fh;
4259 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004260 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004261 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004262 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004263 data->res.fattr = &data->fattr;
4264 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004265 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004266 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004267 data->rpc_status = 0;
4268
Trond Myklebustc970aa82007-07-14 15:39:59 -04004269 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004270 msg.rpc_argp = &data->args;
4271 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004272 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004273 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004274 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004275 if (!issync)
4276 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004277 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004278 if (status != 0)
4279 goto out;
4280 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004281 if (status == 0)
4282 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4283 else
4284 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004285out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004286 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004287 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
4289
Trond Myklebuste6f81072008-01-24 18:14:34 -05004290int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
4292 struct nfs_server *server = NFS_SERVER(inode);
4293 struct nfs4_exception exception = { };
4294 int err;
4295 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004296 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 switch (err) {
4298 case -NFS4ERR_STALE_STATEID:
4299 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 case 0:
4301 return 0;
4302 }
4303 err = nfs4_handle_exception(server, err, &exception);
4304 } while (exception.retry);
4305 return err;
4306}
4307
4308#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4309#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4310
4311/*
4312 * sleep, with exponential backoff, and retry the LOCK operation.
4313 */
4314static unsigned long
4315nfs4_set_lock_task_retry(unsigned long timeout)
4316{
Jeff Laytond3103102011-12-01 22:44:39 +01004317 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 timeout <<= 1;
4319 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4320 return NFS4_LOCK_MAXTIMEOUT;
4321 return timeout;
4322}
4323
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4325{
4326 struct inode *inode = state->inode;
4327 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004328 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004329 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004331 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004333 struct nfs_lockt_res res = {
4334 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 };
4336 struct rpc_message msg = {
4337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4338 .rpc_argp = &arg,
4339 .rpc_resp = &res,
4340 .rpc_cred = state->owner->so_cred,
4341 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 struct nfs4_lock_state *lsp;
4343 int status;
4344
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004345 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004346 status = nfs4_set_lock_state(state, request);
4347 if (status != 0)
4348 goto out;
4349 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004350 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004351 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004352 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004353 switch (status) {
4354 case 0:
4355 request->fl_type = F_UNLCK;
4356 break;
4357 case -NFS4ERR_DENIED:
4358 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004360 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004361out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 return status;
4363}
4364
4365static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4366{
4367 struct nfs4_exception exception = { };
4368 int err;
4369
4370 do {
4371 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4372 _nfs4_proc_getlk(state, cmd, request),
4373 &exception);
4374 } while (exception.retry);
4375 return err;
4376}
4377
4378static int do_vfs_lock(struct file *file, struct file_lock *fl)
4379{
4380 int res = 0;
4381 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4382 case FL_POSIX:
4383 res = posix_lock_file_wait(file, fl);
4384 break;
4385 case FL_FLOCK:
4386 res = flock_lock_file_wait(file, fl);
4387 break;
4388 default:
4389 BUG();
4390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 return res;
4392}
4393
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004394struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004395 struct nfs_locku_args arg;
4396 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004397 struct nfs4_lock_state *lsp;
4398 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004399 struct file_lock fl;
4400 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004401 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004402};
4403
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004404static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4405 struct nfs_open_context *ctx,
4406 struct nfs4_lock_state *lsp,
4407 struct nfs_seqid *seqid)
4408{
4409 struct nfs4_unlockdata *p;
4410 struct inode *inode = lsp->ls_state->inode;
4411
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004412 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004413 if (p == NULL)
4414 return NULL;
4415 p->arg.fh = NFS_FH(inode);
4416 p->arg.fl = &p->fl;
4417 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004418 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004419 p->arg.stateid = &lsp->ls_stateid;
4420 p->lsp = lsp;
4421 atomic_inc(&lsp->ls_count);
4422 /* Ensure we don't close file until we're done freeing locks! */
4423 p->ctx = get_nfs_open_context(ctx);
4424 memcpy(&p->fl, fl, sizeof(p->fl));
4425 p->server = NFS_SERVER(inode);
4426 return p;
4427}
4428
Trond Myklebust06f814a2006-01-03 09:55:07 +01004429static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004430{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004431 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004432 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004433 nfs4_put_lock_state(calldata->lsp);
4434 put_nfs_open_context(calldata->ctx);
4435 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004436}
4437
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004438static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004439{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004440 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004441
Trond Myklebust14516c32010-07-31 14:29:06 -04004442 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4443 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004444 switch (task->tk_status) {
4445 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004446 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4447 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004448 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004449 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004450 case -NFS4ERR_BAD_STATEID:
4451 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004452 case -NFS4ERR_STALE_STATEID:
4453 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004454 break;
4455 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004456 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004457 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004458 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004459}
4460
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004461static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004462{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004463 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004465 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004466 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004467 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004468 /* Note: exit _without_ running nfs4_locku_done */
4469 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004470 return;
4471 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004472 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004473 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004474 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004475 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004476 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004477 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478}
4479
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004480static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004481 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004482 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004483 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004484};
4485
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004486static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4487 struct nfs_open_context *ctx,
4488 struct nfs4_lock_state *lsp,
4489 struct nfs_seqid *seqid)
4490{
4491 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004492 struct rpc_message msg = {
4493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4494 .rpc_cred = ctx->cred,
4495 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004496 struct rpc_task_setup task_setup_data = {
4497 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004498 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004499 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004500 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004501 .flags = RPC_TASK_ASYNC,
4502 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004503
Frank Filz137d6ac2007-07-09 15:32:29 -07004504 /* Ensure this is an unlock - when canceling a lock, the
4505 * canceled lock is passed in, and it won't be an unlock.
4506 */
4507 fl->fl_type = F_UNLCK;
4508
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4510 if (data == NULL) {
4511 nfs_free_seqid(seqid);
4512 return ERR_PTR(-ENOMEM);
4513 }
4514
Trond Myklebust9d12b212012-01-17 22:04:25 -05004515 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004516 msg.rpc_argp = &data->arg;
4517 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004518 task_setup_data.callback_data = data;
4519 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520}
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4523{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004524 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004525 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004526 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004527 struct rpc_task *task;
4528 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004529 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Trond Myklebust9b073572006-06-29 16:38:34 -04004531 status = nfs4_set_lock_state(state, request);
4532 /* Unlock _before_ we do the RPC call */
4533 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004534 down_read(&nfsi->rwsem);
4535 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4536 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004537 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004538 }
4539 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004540 if (status != 0)
4541 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004542 /* Is this a delegated lock? */
4543 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004544 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004545 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004546 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004547 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004548 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004549 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004550 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004551 status = PTR_ERR(task);
4552 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004553 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004554 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004555 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004556out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004557 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004558 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559}
4560
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004561struct nfs4_lockdata {
4562 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004563 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004564 struct nfs4_lock_state *lsp;
4565 struct nfs_open_context *ctx;
4566 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004567 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004568 int rpc_status;
4569 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004570 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004571};
4572
4573static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004574 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4575 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004576{
4577 struct nfs4_lockdata *p;
4578 struct inode *inode = lsp->ls_state->inode;
4579 struct nfs_server *server = NFS_SERVER(inode);
4580
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004581 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004582 if (p == NULL)
4583 return NULL;
4584
4585 p->arg.fh = NFS_FH(inode);
4586 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004587 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004588 if (p->arg.open_seqid == NULL)
4589 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004590 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004591 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004592 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004594 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004595 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004596 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004597 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004598 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004599 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004600 atomic_inc(&lsp->ls_count);
4601 p->ctx = get_nfs_open_context(ctx);
4602 memcpy(&p->fl, fl, sizeof(p->fl));
4603 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004604out_free_seqid:
4605 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004606out_free:
4607 kfree(p);
4608 return NULL;
4609}
4610
4611static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4612{
4613 struct nfs4_lockdata *data = calldata;
4614 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Harvey Harrison3110ff82008-05-02 13:42:44 -07004616 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004617 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4618 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004619 /* Do we need to do an open_to_lock_owner? */
4620 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004621 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4622 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004623 data->arg.open_stateid = &state->stateid;
4624 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004625 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004626 } else
4627 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004628 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004629 if (nfs4_setup_sequence(data->server,
4630 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004631 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004632 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004633 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004634 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004635}
4636
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004637static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4638{
4639 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4640 nfs4_lock_prepare(task, calldata);
4641}
4642
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004643static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4644{
4645 struct nfs4_lockdata *data = calldata;
4646
Harvey Harrison3110ff82008-05-02 13:42:44 -07004647 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004648
Trond Myklebust14516c32010-07-31 14:29:06 -04004649 if (!nfs4_sequence_done(task, &data->res.seq_res))
4650 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004651
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004652 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004653 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004654 if (data->rpc_status == 0)
4655 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4656 else
4657 goto out;
4658 }
4659 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004660 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004661 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004662 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004663 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004664out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004665 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004666}
4667
4668static void nfs4_lock_release(void *calldata)
4669{
4670 struct nfs4_lockdata *data = calldata;
4671
Harvey Harrison3110ff82008-05-02 13:42:44 -07004672 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004673 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004674 if (data->cancelled != 0) {
4675 struct rpc_task *task;
4676 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4677 data->arg.lock_seqid);
4678 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004679 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004680 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004681 } else
4682 nfs_free_seqid(data->arg.lock_seqid);
4683 nfs4_put_lock_state(data->lsp);
4684 put_nfs_open_context(data->ctx);
4685 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004686 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004687}
4688
4689static const struct rpc_call_ops nfs4_lock_ops = {
4690 .rpc_call_prepare = nfs4_lock_prepare,
4691 .rpc_call_done = nfs4_lock_done,
4692 .rpc_release = nfs4_lock_release,
4693};
4694
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004695static const struct rpc_call_ops nfs4_recover_lock_ops = {
4696 .rpc_call_prepare = nfs4_recover_lock_prepare,
4697 .rpc_call_done = nfs4_lock_done,
4698 .rpc_release = nfs4_lock_release,
4699};
4700
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004701static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4702{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004703 switch (error) {
4704 case -NFS4ERR_ADMIN_REVOKED:
4705 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004706 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004707 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004708 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004709 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004710 break;
4711 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004712 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004713 case -NFS4ERR_EXPIRED:
4714 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004715 };
4716}
4717
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004718static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004719{
4720 struct nfs4_lockdata *data;
4721 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004722 struct rpc_message msg = {
4723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4724 .rpc_cred = state->owner->so_cred,
4725 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004726 struct rpc_task_setup task_setup_data = {
4727 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004728 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004729 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004730 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004731 .flags = RPC_TASK_ASYNC,
4732 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004733 int ret;
4734
Harvey Harrison3110ff82008-05-02 13:42:44 -07004735 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004736 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004737 fl->fl_u.nfs4_fl.owner,
4738 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004739 if (data == NULL)
4740 return -ENOMEM;
4741 if (IS_SETLKW(cmd))
4742 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004743 if (recovery_type > NFS_LOCK_NEW) {
4744 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004745 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004746 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4747 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004748 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004749 msg.rpc_argp = &data->arg;
4750 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004751 task_setup_data.callback_data = data;
4752 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004753 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004754 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004755 ret = nfs4_wait_for_completion_rpc_task(task);
4756 if (ret == 0) {
4757 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004758 if (ret)
4759 nfs4_handle_setlk_error(data->server, data->lsp,
4760 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004761 } else
4762 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004763 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004764 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004765 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766}
4767
4768static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4769{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004770 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004771 struct nfs4_exception exception = {
4772 .inode = state->inode,
4773 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004774 int err;
4775
4776 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004777 /* Cache the lock if possible... */
4778 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4779 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004780 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004781 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004782 break;
4783 nfs4_handle_exception(server, err, &exception);
4784 } while (exception.retry);
4785 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
4788static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4789{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004790 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004791 struct nfs4_exception exception = {
4792 .inode = state->inode,
4793 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004794 int err;
4795
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004796 err = nfs4_set_lock_state(state, request);
4797 if (err != 0)
4798 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004799 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004800 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4801 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004802 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004803 switch (err) {
4804 default:
4805 goto out;
4806 case -NFS4ERR_GRACE:
4807 case -NFS4ERR_DELAY:
4808 nfs4_handle_exception(server, err, &exception);
4809 err = 0;
4810 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004811 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004812out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004813 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814}
4815
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004816#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004817/**
4818 * nfs41_check_expired_locks - possibly free a lock stateid
4819 *
4820 * @state: NFSv4 state for an inode
4821 *
4822 * Returns NFS_OK if recovery for this stateid is now finished.
4823 * Otherwise a negative NFS4ERR value is returned.
4824 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004825static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004826{
Chuck Levereb64cf92012-07-11 16:30:05 -04004827 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004828 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004829 struct nfs_server *server = NFS_SERVER(state->inode);
4830
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004831 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004832 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004833 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4834 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004835 /* Free the stateid unless the server
4836 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004837 if (status != -NFS4ERR_BAD_STATEID)
4838 nfs41_free_stateid(server,
4839 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004840 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004841 ret = status;
4842 }
4843 }
4844 };
4845
4846 return ret;
4847}
4848
4849static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4850{
4851 int status = NFS_OK;
4852
4853 if (test_bit(LK_STATE_IN_USE, &state->flags))
4854 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004855 if (status != NFS_OK)
4856 status = nfs4_lock_expired(state, request);
4857 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004858}
4859#endif
4860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4862{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004863 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004864 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004865 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004867 if ((fl_flags & FL_POSIX) &&
4868 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4869 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004870 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004871 status = nfs4_set_lock_state(state, request);
4872 if (status != 0)
4873 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004874 request->fl_flags |= FL_ACCESS;
4875 status = do_vfs_lock(request->fl_file, request);
4876 if (status < 0)
4877 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004878 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004879 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004880 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004881 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004882 request->fl_flags = fl_flags & ~FL_SLEEP;
4883 status = do_vfs_lock(request->fl_file, request);
4884 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004885 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004886 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004887 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004888 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004889 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004890 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004891 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004892 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4893 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004894out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004895 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004896out:
4897 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 return status;
4899}
4900
4901static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4902{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004903 struct nfs4_exception exception = {
4904 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004905 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004906 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 int err;
4908
4909 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004910 err = _nfs4_proc_setlk(state, cmd, request);
4911 if (err == -NFS4ERR_DENIED)
4912 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004914 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 } while (exception.retry);
4916 return err;
4917}
4918
4919static int
4920nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4921{
4922 struct nfs_open_context *ctx;
4923 struct nfs4_state *state;
4924 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4925 int status;
4926
4927 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004928 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 state = ctx->state;
4930
4931 if (request->fl_start < 0 || request->fl_end < 0)
4932 return -EINVAL;
4933
Trond Myklebustd9531262009-07-21 19:22:38 -04004934 if (IS_GETLK(cmd)) {
4935 if (state != NULL)
4936 return nfs4_proc_getlk(state, F_GETLK, request);
4937 return 0;
4938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
4940 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4941 return -EINVAL;
4942
Trond Myklebustd9531262009-07-21 19:22:38 -04004943 if (request->fl_type == F_UNLCK) {
4944 if (state != NULL)
4945 return nfs4_proc_unlck(state, cmd, request);
4946 return 0;
4947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
Trond Myklebustd9531262009-07-21 19:22:38 -04004949 if (state == NULL)
4950 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004951 /*
4952 * Don't rely on the VFS having checked the file open mode,
4953 * since it won't do this for flock() locks.
4954 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004955 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004956 case F_RDLCK:
4957 if (!(filp->f_mode & FMODE_READ))
4958 return -EBADF;
4959 break;
4960 case F_WRLCK:
4961 if (!(filp->f_mode & FMODE_WRITE))
4962 return -EBADF;
4963 }
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 do {
4966 status = nfs4_proc_setlk(state, cmd, request);
4967 if ((status != -EAGAIN) || IS_SETLK(cmd))
4968 break;
4969 timeout = nfs4_set_lock_task_retry(timeout);
4970 status = -ERESTARTSYS;
4971 if (signalled())
4972 break;
4973 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 return status;
4975}
4976
Trond Myklebust888e6942005-11-04 15:38:11 -05004977int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4978{
4979 struct nfs_server *server = NFS_SERVER(state->inode);
4980 struct nfs4_exception exception = { };
4981 int err;
4982
4983 err = nfs4_set_lock_state(state, fl);
4984 if (err != 0)
4985 goto out;
4986 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004987 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004988 switch (err) {
4989 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004990 printk(KERN_ERR "NFS: %s: unhandled error "
4991 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004992 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004993 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004994 goto out;
4995 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004996 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004997 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004998 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004999 nfs4_schedule_lease_recovery(server->nfs_client);
5000 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005001 case -NFS4ERR_BADSESSION:
5002 case -NFS4ERR_BADSLOT:
5003 case -NFS4ERR_BAD_HIGH_SLOT:
5004 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5005 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005006 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005007 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005008 case -ERESTARTSYS:
5009 /*
5010 * The show must go on: exit, but mark the
5011 * stateid as needing recovery.
5012 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005013 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005014 case -NFS4ERR_ADMIN_REVOKED:
5015 case -NFS4ERR_BAD_STATEID:
5016 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005017 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005018 err = 0;
5019 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005020 case -EKEYEXPIRED:
5021 /*
5022 * User RPCSEC_GSS context has expired.
5023 * We cannot recover this stateid now, so
5024 * skip it and allow recovery thread to
5025 * proceed.
5026 */
5027 err = 0;
5028 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005029 case -ENOMEM:
5030 case -NFS4ERR_DENIED:
5031 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5032 err = 0;
5033 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005034 case -NFS4ERR_DELAY:
5035 break;
5036 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005037 err = nfs4_handle_exception(server, err, &exception);
5038 } while (exception.retry);
5039out:
5040 return err;
5041}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005042
Trond Myklebustcf470c32012-03-07 13:49:12 -05005043struct nfs_release_lockowner_data {
5044 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005045 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005046 struct nfs_release_lockowner_args args;
5047};
5048
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005049static void nfs4_release_lockowner_release(void *calldata)
5050{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005051 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005052 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005053 kfree(calldata);
5054}
5055
Trond Myklebust17280172012-03-11 13:11:00 -04005056static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005057 .rpc_release = nfs4_release_lockowner_release,
5058};
5059
Trond Myklebustcf470c32012-03-07 13:49:12 -05005060int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005061{
5062 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005063 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005064 struct rpc_message msg = {
5065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5066 };
5067
5068 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005069 return -EINVAL;
5070 data = kmalloc(sizeof(*data), GFP_NOFS);
5071 if (!data)
5072 return -ENOMEM;
5073 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005074 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005075 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5076 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5077 data->args.lock_owner.s_dev = server->s_dev;
5078 msg.rpc_argp = &data->args;
5079 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5080 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005081}
5082
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005083#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5084
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005085static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5086 const void *buf, size_t buflen,
5087 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005088{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005089 if (strcmp(key, "") != 0)
5090 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005091
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005092 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005093}
5094
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005095static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5096 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005097{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005098 if (strcmp(key, "") != 0)
5099 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005100
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005101 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005102}
5103
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005104static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5105 size_t list_len, const char *name,
5106 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005107{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005108 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005109
J. Bruce Fields096455a2006-03-20 23:23:42 -05005110 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5111 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005112
5113 if (list && len <= list_len)
5114 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005115 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005116}
5117
Andy Adamson533eb462011-06-13 18:25:56 -04005118/*
5119 * nfs_fhget will use either the mounted_on_fileid or the fileid
5120 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005121static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5122{
Andy Adamson533eb462011-06-13 18:25:56 -04005123 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5124 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5125 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005126 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005127 return;
5128
5129 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005130 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005131 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5132 fattr->nlink = 2;
5133}
5134
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005135static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5136 const struct qstr *name,
5137 struct nfs4_fs_locations *fs_locations,
5138 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005139{
5140 struct nfs_server *server = NFS_SERVER(dir);
5141 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005142 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005143 };
5144 struct nfs4_fs_locations_arg args = {
5145 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005146 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005147 .page = page,
5148 .bitmask = bitmask,
5149 };
Benny Halevy22958462009-04-01 09:22:02 -04005150 struct nfs4_fs_locations_res res = {
5151 .fs_locations = fs_locations,
5152 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005153 struct rpc_message msg = {
5154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5155 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005156 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005157 };
5158 int status;
5159
Harvey Harrison3110ff82008-05-02 13:42:44 -07005160 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005161
5162 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5163 * is not supported */
5164 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5165 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5166 else
5167 bitmask[0] |= FATTR4_WORD0_FILEID;
5168
Trond Myklebustc228fd32007-01-13 02:28:11 -05005169 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005170 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005171 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005172 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005173 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005174 return status;
5175}
5176
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005177int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5178 const struct qstr *name,
5179 struct nfs4_fs_locations *fs_locations,
5180 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005181{
5182 struct nfs4_exception exception = { };
5183 int err;
5184 do {
5185 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005186 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005187 &exception);
5188 } while (exception.retry);
5189 return err;
5190}
5191
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005192static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5193{
5194 int status;
5195 struct nfs4_secinfo_arg args = {
5196 .dir_fh = NFS_FH(dir),
5197 .name = name,
5198 };
5199 struct nfs4_secinfo_res res = {
5200 .flavors = flavors,
5201 };
5202 struct rpc_message msg = {
5203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5204 .rpc_argp = &args,
5205 .rpc_resp = &res,
5206 };
5207
5208 dprintk("NFS call secinfo %s\n", name->name);
5209 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5210 dprintk("NFS reply secinfo: %d\n", status);
5211 return status;
5212}
5213
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005214int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5215 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005216{
5217 struct nfs4_exception exception = { };
5218 int err;
5219 do {
5220 err = nfs4_handle_exception(NFS_SERVER(dir),
5221 _nfs4_proc_secinfo(dir, name, flavors),
5222 &exception);
5223 } while (exception.retry);
5224 return err;
5225}
5226
Andy Adamson557134a2009-04-01 09:21:53 -04005227#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005228/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005229 * Check the exchange flags returned by the server for invalid flags, having
5230 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5231 * DS flags set.
5232 */
5233static int nfs4_check_cl_exchange_flags(u32 flags)
5234{
5235 if (flags & ~EXCHGID4_FLAG_MASK_R)
5236 goto out_inval;
5237 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5238 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5239 goto out_inval;
5240 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5241 goto out_inval;
5242 return NFS_OK;
5243out_inval:
5244 return -NFS4ERR_INVAL;
5245}
5246
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005247static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005248nfs41_same_server_scope(struct nfs41_server_scope *a,
5249 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005250{
5251 if (a->server_scope_sz == b->server_scope_sz &&
5252 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5253 return true;
5254
5255 return false;
5256}
5257
Andy Adamson357f54d2010-12-14 10:11:57 -05005258/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005259 * nfs4_proc_bind_conn_to_session()
5260 *
5261 * The 4.1 client currently uses the same TCP connection for the
5262 * fore and backchannel.
5263 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005264int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005265{
5266 int status;
5267 struct nfs41_bind_conn_to_session_res res;
5268 struct rpc_message msg = {
5269 .rpc_proc =
5270 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5271 .rpc_argp = clp,
5272 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005273 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005274 };
5275
5276 dprintk("--> %s\n", __func__);
5277 BUG_ON(clp == NULL);
5278
5279 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5280 if (unlikely(res.session == NULL)) {
5281 status = -ENOMEM;
5282 goto out;
5283 }
5284
5285 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5286 if (status == 0) {
5287 if (memcmp(res.session->sess_id.data,
5288 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5289 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5290 status = -EIO;
5291 goto out_session;
5292 }
5293 if (res.dir != NFS4_CDFS4_BOTH) {
5294 dprintk("NFS: %s: Unexpected direction from server\n",
5295 __func__);
5296 status = -EIO;
5297 goto out_session;
5298 }
5299 if (res.use_conn_in_rdma_mode) {
5300 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5301 __func__);
5302 status = -EIO;
5303 goto out_session;
5304 }
5305 }
5306out_session:
5307 kfree(res.session);
5308out:
5309 dprintk("<-- %s status= %d\n", __func__, status);
5310 return status;
5311}
5312
5313/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005314 * nfs4_proc_exchange_id()
5315 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005316 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5317 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005318 * Since the clientid has expired, all compounds using sessions
5319 * associated with the stale clientid will be returning
5320 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5321 * be in some phase of session reset.
5322 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005323int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005324{
5325 nfs4_verifier verifier;
5326 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005327 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005328 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005329 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005330 };
5331 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005332 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005333 };
5334 int status;
5335 struct rpc_message msg = {
5336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5337 .rpc_argp = &args,
5338 .rpc_resp = &res,
5339 .rpc_cred = cred,
5340 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005341
Chuck Leverf0920752012-05-21 22:45:41 -04005342 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005343 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5344 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005345 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5346 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5347 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005348
Chuck Leveracdeb692012-05-21 22:46:16 -04005349 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005350 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005351 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005352 status = -ENOMEM;
5353 goto out;
5354 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005355
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005356 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005357 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005358 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005359 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005360 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005361 }
5362
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005363 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005364 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005365 status = -ENOMEM;
5366 goto out_server_scope;
5367 }
5368
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005369 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005370 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005371 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005372
Chuck Lever177313f2012-05-21 22:44:58 -04005373 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005374 clp->cl_clientid = res.clientid;
5375 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5376 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5377 clp->cl_seqid = res.seqid;
5378
Chuck Leveracdeb692012-05-21 22:46:16 -04005379 kfree(clp->cl_serverowner);
5380 clp->cl_serverowner = res.server_owner;
5381 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005382
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005383 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005384 kfree(clp->cl_implid);
5385 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005386
Chuck Lever177313f2012-05-21 22:44:58 -04005387 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005388 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005389 res.server_scope)) {
5390 dprintk("%s: server_scope mismatch detected\n",
5391 __func__);
5392 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005393 kfree(clp->cl_serverscope);
5394 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005395 }
5396
Chuck Lever177313f2012-05-21 22:44:58 -04005397 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005398 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005399 goto out;
5400 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005401 } else
5402 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005403
Chuck Leveracdeb692012-05-21 22:46:16 -04005404out_server_owner:
5405 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005406out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005407 kfree(res.server_scope);
5408out:
Chuck Lever177313f2012-05-21 22:44:58 -04005409 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005410 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005411 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005412 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005413 clp->cl_implid->date.seconds,
5414 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005415 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005416 return status;
5417}
5418
Trond Myklebust66245532012-05-25 17:18:09 -04005419static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5420 struct rpc_cred *cred)
5421{
5422 struct rpc_message msg = {
5423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5424 .rpc_argp = clp,
5425 .rpc_cred = cred,
5426 };
5427 int status;
5428
5429 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5430 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005431 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005432 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5433 return status;
5434}
5435
5436static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5437 struct rpc_cred *cred)
5438{
5439 unsigned int loop;
5440 int ret;
5441
5442 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5443 ret = _nfs4_proc_destroy_clientid(clp, cred);
5444 switch (ret) {
5445 case -NFS4ERR_DELAY:
5446 case -NFS4ERR_CLIENTID_BUSY:
5447 ssleep(1);
5448 break;
5449 default:
5450 return ret;
5451 }
5452 }
5453 return 0;
5454}
5455
5456int nfs4_destroy_clientid(struct nfs_client *clp)
5457{
5458 struct rpc_cred *cred;
5459 int ret = 0;
5460
5461 if (clp->cl_mvops->minor_version < 1)
5462 goto out;
5463 if (clp->cl_exchange_flags == 0)
5464 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005465 if (clp->cl_preserve_clid)
5466 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005467 cred = nfs4_get_exchange_id_cred(clp);
5468 ret = nfs4_proc_destroy_clientid(clp, cred);
5469 if (cred)
5470 put_rpccred(cred);
5471 switch (ret) {
5472 case 0:
5473 case -NFS4ERR_STALE_CLIENTID:
5474 clp->cl_exchange_flags = 0;
5475 }
5476out:
5477 return ret;
5478}
5479
Andy Adamson2050f0c2009-04-01 09:22:30 -04005480struct nfs4_get_lease_time_data {
5481 struct nfs4_get_lease_time_args *args;
5482 struct nfs4_get_lease_time_res *res;
5483 struct nfs_client *clp;
5484};
5485
5486static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5487 void *calldata)
5488{
5489 int ret;
5490 struct nfs4_get_lease_time_data *data =
5491 (struct nfs4_get_lease_time_data *)calldata;
5492
5493 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005494 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005495 /* just setup sequence, do not trigger session recovery
5496 since we're invoked within one */
5497 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005498 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005499 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005500
5501 BUG_ON(ret == -EAGAIN);
5502 rpc_call_start(task);
5503 dprintk("<-- %s\n", __func__);
5504}
5505
5506/*
5507 * Called from nfs4_state_manager thread for session setup, so don't recover
5508 * from sequence operation or clientid errors.
5509 */
5510static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5511{
5512 struct nfs4_get_lease_time_data *data =
5513 (struct nfs4_get_lease_time_data *)calldata;
5514
5515 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005516 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5517 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005518 switch (task->tk_status) {
5519 case -NFS4ERR_DELAY:
5520 case -NFS4ERR_GRACE:
5521 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5522 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5523 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005524 /* fall through */
5525 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005526 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005527 return;
5528 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005529 dprintk("<-- %s\n", __func__);
5530}
5531
Trond Myklebust17280172012-03-11 13:11:00 -04005532static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005533 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5534 .rpc_call_done = nfs4_get_lease_time_done,
5535};
5536
5537int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5538{
5539 struct rpc_task *task;
5540 struct nfs4_get_lease_time_args args;
5541 struct nfs4_get_lease_time_res res = {
5542 .lr_fsinfo = fsinfo,
5543 };
5544 struct nfs4_get_lease_time_data data = {
5545 .args = &args,
5546 .res = &res,
5547 .clp = clp,
5548 };
5549 struct rpc_message msg = {
5550 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5551 .rpc_argp = &args,
5552 .rpc_resp = &res,
5553 };
5554 struct rpc_task_setup task_setup = {
5555 .rpc_client = clp->cl_rpcclient,
5556 .rpc_message = &msg,
5557 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005558 .callback_data = &data,
5559 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005560 };
5561 int status;
5562
Trond Myklebust9d12b212012-01-17 22:04:25 -05005563 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005564 dprintk("--> %s\n", __func__);
5565 task = rpc_run_task(&task_setup);
5566
5567 if (IS_ERR(task))
5568 status = PTR_ERR(task);
5569 else {
5570 status = task->tk_status;
5571 rpc_put_task(task);
5572 }
5573 dprintk("<-- %s return %d\n", __func__, status);
5574
5575 return status;
5576}
5577
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005578static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5579{
5580 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5581}
5582
5583static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5584 struct nfs4_slot *new,
5585 u32 max_slots,
5586 u32 ivalue)
5587{
5588 struct nfs4_slot *old = NULL;
5589 u32 i;
5590
5591 spin_lock(&tbl->slot_tbl_lock);
5592 if (new) {
5593 old = tbl->slots;
5594 tbl->slots = new;
5595 tbl->max_slots = max_slots;
5596 }
5597 tbl->highest_used_slotid = -1; /* no slot is currently used */
5598 for (i = 0; i < tbl->max_slots; i++)
5599 tbl->slots[i].seq_nr = ivalue;
5600 spin_unlock(&tbl->slot_tbl_lock);
5601 kfree(old);
5602}
5603
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005604/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005605 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005606 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005607static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5608 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005609{
Andy Adamson104aeba2010-01-14 17:45:10 -05005610 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005611 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005612
Andy Adamson104aeba2010-01-14 17:45:10 -05005613 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5614 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005615
Andy Adamson104aeba2010-01-14 17:45:10 -05005616 /* Does the newly negotiated max_reqs match the existing slot table? */
5617 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005618 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005619 if (!new)
5620 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005621 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005622 ret = 0;
5623
5624 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005625 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5626 tbl, tbl->slots, tbl->max_slots);
5627out:
5628 dprintk("<-- %s: return %d\n", __func__, ret);
5629 return ret;
5630}
5631
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005632/* Destroy the slot table */
5633static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5634{
5635 if (session->fc_slot_table.slots != NULL) {
5636 kfree(session->fc_slot_table.slots);
5637 session->fc_slot_table.slots = NULL;
5638 }
5639 if (session->bc_slot_table.slots != NULL) {
5640 kfree(session->bc_slot_table.slots);
5641 session->bc_slot_table.slots = NULL;
5642 }
5643 return;
5644}
5645
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005646/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005647 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005648 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005649static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005650{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005651 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005652 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005653
Andy Adamsonaacd5532011-11-09 13:58:21 -05005654 dprintk("--> %s\n", __func__);
5655 /* Fore channel */
5656 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005657 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5658 if (status) /* -ENOMEM */
5659 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005660 /* Back channel */
5661 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005662 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5663 if (status && tbl->slots == NULL)
5664 /* Fore and back channel share a connection so get
5665 * both slot tables or neither */
5666 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005667 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005668}
5669
5670struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5671{
5672 struct nfs4_session *session;
5673 struct nfs4_slot_table *tbl;
5674
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005675 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005676 if (!session)
5677 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005678
Andy Adamson557134a2009-04-01 09:21:53 -04005679 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005680 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005681 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005682 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005683 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005684
5685 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005686 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005687 spin_lock_init(&tbl->slot_tbl_lock);
5688 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005689 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005690
Trond Myklebust1055d762010-06-16 09:52:27 -04005691 session->session_state = 1<<NFS4_SESSION_INITING;
5692
Andy Adamson557134a2009-04-01 09:21:53 -04005693 session->clp = clp;
5694 return session;
5695}
5696
5697void nfs4_destroy_session(struct nfs4_session *session)
5698{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005699 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005700 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005701
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005702 cred = nfs4_get_exchange_id_cred(session->clp);
5703 nfs4_proc_destroy_session(session, cred);
5704 if (cred)
5705 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005706
5707 rcu_read_lock();
5708 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5709 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005710 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005711 __func__, xprt);
5712 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005713 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005714 kfree(session);
5715}
5716
Andy Adamsonfc931582009-04-01 09:22:31 -04005717/*
5718 * Initialize the values to be used by the client in CREATE_SESSION
5719 * If nfs4_init_session set the fore channel request and response sizes,
5720 * use them.
5721 *
5722 * Set the back channel max_resp_sz_cached to zero to force the client to
5723 * always set csa_cachethis to FALSE because the current implementation
5724 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5725 */
5726static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5727{
5728 struct nfs4_session *session = args->client->cl_session;
5729 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5730 mxresp_sz = session->fc_attrs.max_resp_sz;
5731
5732 if (mxrqst_sz == 0)
5733 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5734 if (mxresp_sz == 0)
5735 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5736 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005737 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5738 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005739 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005740 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005741
5742 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005743 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005744 __func__,
5745 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005746 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005747
5748 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005749 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5750 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5751 args->bc_attrs.max_resp_sz_cached = 0;
5752 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5753 args->bc_attrs.max_reqs = 1;
5754
5755 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5756 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5757 __func__,
5758 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5759 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5760 args->bc_attrs.max_reqs);
5761}
5762
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005763static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005764{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005765 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5766 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5767
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005768 if (rcvd->max_resp_sz > sent->max_resp_sz)
5769 return -EINVAL;
5770 /*
5771 * Our requested max_ops is the minimum we need; we're not
5772 * prepared to break up compounds into smaller pieces than that.
5773 * So, no point even trying to continue if the server won't
5774 * cooperate:
5775 */
5776 if (rcvd->max_ops < sent->max_ops)
5777 return -EINVAL;
5778 if (rcvd->max_reqs == 0)
5779 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005780 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5781 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005782 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005783}
5784
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005785static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5786{
5787 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5788 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005789
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005790 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5791 return -EINVAL;
5792 if (rcvd->max_resp_sz < sent->max_resp_sz)
5793 return -EINVAL;
5794 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5795 return -EINVAL;
5796 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005797 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005798 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005799 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005800 return -EINVAL;
5801 return 0;
5802}
Andy Adamson8d353012009-04-01 09:22:32 -04005803
Andy Adamson8d353012009-04-01 09:22:32 -04005804static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5805 struct nfs4_session *session)
5806{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005807 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005808
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005809 ret = nfs4_verify_fore_channel_attrs(args, session);
5810 if (ret)
5811 return ret;
5812 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005813}
5814
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005815static int _nfs4_proc_create_session(struct nfs_client *clp,
5816 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005817{
5818 struct nfs4_session *session = clp->cl_session;
5819 struct nfs41_create_session_args args = {
5820 .client = clp,
5821 .cb_program = NFS4_CALLBACK,
5822 };
5823 struct nfs41_create_session_res res = {
5824 .client = clp,
5825 };
5826 struct rpc_message msg = {
5827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5828 .rpc_argp = &args,
5829 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005830 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005831 };
5832 int status;
5833
5834 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005835 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005836
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005837 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005838
Andy Adamson8d353012009-04-01 09:22:32 -04005839 if (!status)
5840 /* Verify the session's negotiated channel_attrs values */
5841 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005842 if (!status) {
5843 /* Increment the clientid slot sequence id */
5844 clp->cl_seqid++;
5845 }
5846
5847 return status;
5848}
5849
5850/*
5851 * Issues a CREATE_SESSION operation to the server.
5852 * It is the responsibility of the caller to verify the session is
5853 * expired before calling this routine.
5854 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005855int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005856{
5857 int status;
5858 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005859 struct nfs4_session *session = clp->cl_session;
5860
5861 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5862
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005863 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005864 if (status)
5865 goto out;
5866
Andy Adamsonaacd5532011-11-09 13:58:21 -05005867 /* Init or reset the session slot tables */
5868 status = nfs4_setup_session_slot_tables(session);
5869 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005870 if (status)
5871 goto out;
5872
5873 ptr = (unsigned *)&session->sess_id.data[0];
5874 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5875 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005876out:
5877 dprintk("<-- %s\n", __func__);
5878 return status;
5879}
5880
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005881/*
5882 * Issue the over-the-wire RPC DESTROY_SESSION.
5883 * The caller must serialize access to this routine.
5884 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005885int nfs4_proc_destroy_session(struct nfs4_session *session,
5886 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005887{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005888 struct rpc_message msg = {
5889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5890 .rpc_argp = session,
5891 .rpc_cred = cred,
5892 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005893 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005894
5895 dprintk("--> nfs4_proc_destroy_session\n");
5896
5897 /* session is still being setup */
5898 if (session->clp->cl_cons_state != NFS_CS_READY)
5899 return status;
5900
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005901 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005902
5903 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005904 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005905 "Session has been destroyed regardless...\n", status);
5906
5907 dprintk("<-- nfs4_proc_destroy_session\n");
5908 return status;
5909}
5910
Trond Myklebust7b38c362012-05-23 13:23:31 -04005911/*
5912 * With sessions, the client is not marked ready until after a
5913 * successful EXCHANGE_ID and CREATE_SESSION.
5914 *
5915 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5916 * other versions of NFS can be tried.
5917 */
5918static int nfs41_check_session_ready(struct nfs_client *clp)
5919{
5920 int ret;
5921
5922 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5923 ret = nfs4_client_recover_expired_lease(clp);
5924 if (ret)
5925 return ret;
5926 }
5927 if (clp->cl_cons_state < NFS_CS_READY)
5928 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005929 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005930 return 0;
5931}
5932
Trond Myklebustfccba802009-07-21 16:48:07 -04005933int nfs4_init_session(struct nfs_server *server)
5934{
5935 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005936 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005937 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005938
5939 if (!nfs4_has_session(clp))
5940 return 0;
5941
Trond Myklebust1055d762010-06-16 09:52:27 -04005942 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005943 spin_lock(&clp->cl_lock);
5944 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005945
Trond Myklebust7b38c362012-05-23 13:23:31 -04005946 rsize = server->rsize;
5947 if (rsize == 0)
5948 rsize = NFS_MAX_FILE_IO_SIZE;
5949 wsize = server->wsize;
5950 if (wsize == 0)
5951 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005952
Trond Myklebust7b38c362012-05-23 13:23:31 -04005953 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5954 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5955 }
5956 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005957
Trond Myklebust7b38c362012-05-23 13:23:31 -04005958 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005959}
5960
Trond Myklebust7b38c362012-05-23 13:23:31 -04005961int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005962{
5963 struct nfs4_session *session = clp->cl_session;
5964 int ret;
5965
Trond Myklebust7b38c362012-05-23 13:23:31 -04005966 spin_lock(&clp->cl_lock);
5967 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5968 /*
5969 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5970 * DS lease to be equal to the MDS lease.
5971 */
5972 clp->cl_lease_time = lease_time;
5973 clp->cl_last_renewal = jiffies;
5974 }
5975 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005976
Trond Myklebust7b38c362012-05-23 13:23:31 -04005977 ret = nfs41_check_session_ready(clp);
5978 if (ret)
5979 return ret;
5980 /* Test for the DS role */
5981 if (!is_ds_client(clp))
5982 return -ENODEV;
5983 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005984}
5985EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5986
5987
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005988/*
5989 * Renew the cl_session lease.
5990 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005991struct nfs4_sequence_data {
5992 struct nfs_client *clp;
5993 struct nfs4_sequence_args args;
5994 struct nfs4_sequence_res res;
5995};
5996
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005997static void nfs41_sequence_release(void *data)
5998{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005999 struct nfs4_sequence_data *calldata = data;
6000 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006001
Alexandros Batsakis71358402010-02-05 03:45:05 -08006002 if (atomic_read(&clp->cl_count) > 1)
6003 nfs4_schedule_state_renewal(clp);
6004 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006005 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006006}
6007
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006008static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6009{
6010 switch(task->tk_status) {
6011 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006012 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6013 return -EAGAIN;
6014 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006015 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006016 }
6017 return 0;
6018}
6019
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006020static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006021{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006022 struct nfs4_sequence_data *calldata = data;
6023 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006024
Trond Myklebust14516c32010-07-31 14:29:06 -04006025 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6026 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006027
6028 if (task->tk_status < 0) {
6029 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006030 if (atomic_read(&clp->cl_count) == 1)
6031 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006032
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006033 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6034 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006035 return;
6036 }
6037 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006038 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006039out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006040 dprintk("<-- %s\n", __func__);
6041}
6042
6043static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6044{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006045 struct nfs4_sequence_data *calldata = data;
6046 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006047 struct nfs4_sequence_args *args;
6048 struct nfs4_sequence_res *res;
6049
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006050 args = task->tk_msg.rpc_argp;
6051 res = task->tk_msg.rpc_resp;
6052
Trond Myklebust9d12b212012-01-17 22:04:25 -05006053 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006054 return;
6055 rpc_call_start(task);
6056}
6057
6058static const struct rpc_call_ops nfs41_sequence_ops = {
6059 .rpc_call_done = nfs41_sequence_call_done,
6060 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006061 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006062};
6063
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006064static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006065{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006066 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006067 struct rpc_message msg = {
6068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6069 .rpc_cred = cred,
6070 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006071 struct rpc_task_setup task_setup_data = {
6072 .rpc_client = clp->cl_rpcclient,
6073 .rpc_message = &msg,
6074 .callback_ops = &nfs41_sequence_ops,
6075 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6076 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006077
Alexandros Batsakis71358402010-02-05 03:45:05 -08006078 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006079 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006080 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006081 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006082 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006083 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006084 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006085 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006086 msg.rpc_argp = &calldata->args;
6087 msg.rpc_resp = &calldata->res;
6088 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006089 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006090
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006091 return rpc_run_task(&task_setup_data);
6092}
6093
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006094static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006095{
6096 struct rpc_task *task;
6097 int ret = 0;
6098
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006099 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6100 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006101 task = _nfs41_proc_sequence(clp, cred);
6102 if (IS_ERR(task))
6103 ret = PTR_ERR(task);
6104 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006105 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006106 dprintk("<-- %s status=%d\n", __func__, ret);
6107 return ret;
6108}
6109
6110static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6111{
6112 struct rpc_task *task;
6113 int ret;
6114
6115 task = _nfs41_proc_sequence(clp, cred);
6116 if (IS_ERR(task)) {
6117 ret = PTR_ERR(task);
6118 goto out;
6119 }
6120 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006121 if (!ret) {
6122 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6123
6124 if (task->tk_status == 0)
6125 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006126 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006127 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006128 rpc_put_task(task);
6129out:
6130 dprintk("<-- %s status=%d\n", __func__, ret);
6131 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006132}
6133
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006134struct nfs4_reclaim_complete_data {
6135 struct nfs_client *clp;
6136 struct nfs41_reclaim_complete_args arg;
6137 struct nfs41_reclaim_complete_res res;
6138};
6139
6140static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6141{
6142 struct nfs4_reclaim_complete_data *calldata = data;
6143
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006144 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006145 if (nfs41_setup_sequence(calldata->clp->cl_session,
6146 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006147 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006148 return;
6149
6150 rpc_call_start(task);
6151}
6152
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006153static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6154{
6155 switch(task->tk_status) {
6156 case 0:
6157 case -NFS4ERR_COMPLETE_ALREADY:
6158 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6159 break;
6160 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006161 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006162 /* fall through */
6163 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006164 return -EAGAIN;
6165 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006166 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006167 }
6168 return 0;
6169}
6170
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006171static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6172{
6173 struct nfs4_reclaim_complete_data *calldata = data;
6174 struct nfs_client *clp = calldata->clp;
6175 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6176
6177 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006178 if (!nfs41_sequence_done(task, res))
6179 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006180
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006181 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6182 rpc_restart_call_prepare(task);
6183 return;
6184 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006185 dprintk("<-- %s\n", __func__);
6186}
6187
6188static void nfs4_free_reclaim_complete_data(void *data)
6189{
6190 struct nfs4_reclaim_complete_data *calldata = data;
6191
6192 kfree(calldata);
6193}
6194
6195static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6196 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6197 .rpc_call_done = nfs4_reclaim_complete_done,
6198 .rpc_release = nfs4_free_reclaim_complete_data,
6199};
6200
6201/*
6202 * Issue a global reclaim complete.
6203 */
6204static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6205{
6206 struct nfs4_reclaim_complete_data *calldata;
6207 struct rpc_task *task;
6208 struct rpc_message msg = {
6209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6210 };
6211 struct rpc_task_setup task_setup_data = {
6212 .rpc_client = clp->cl_rpcclient,
6213 .rpc_message = &msg,
6214 .callback_ops = &nfs4_reclaim_complete_call_ops,
6215 .flags = RPC_TASK_ASYNC,
6216 };
6217 int status = -ENOMEM;
6218
6219 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006220 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006221 if (calldata == NULL)
6222 goto out;
6223 calldata->clp = clp;
6224 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006225
Trond Myklebust9d12b212012-01-17 22:04:25 -05006226 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006227 msg.rpc_argp = &calldata->arg;
6228 msg.rpc_resp = &calldata->res;
6229 task_setup_data.callback_data = calldata;
6230 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006231 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006232 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006233 goto out;
6234 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006235 status = nfs4_wait_for_completion_rpc_task(task);
6236 if (status == 0)
6237 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006238 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006239 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006240out:
6241 dprintk("<-- %s status=%d\n", __func__, status);
6242 return status;
6243}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006244
6245static void
6246nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6247{
6248 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006249 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006250
6251 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006252 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6253 * right now covering the LAYOUTGET we are about to send.
6254 * However, that is not so catastrophic, and there seems
6255 * to be no way to prevent it completely.
6256 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006257 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006258 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006259 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006260 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6261 NFS_I(lgp->args.inode)->layout,
6262 lgp->args.ctx->state)) {
6263 rpc_exit(task, NFS4_OK);
6264 return;
6265 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006266 rpc_call_start(task);
6267}
6268
6269static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6270{
6271 struct nfs4_layoutget *lgp = calldata;
6272 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6273
6274 dprintk("--> %s\n", __func__);
6275
6276 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6277 return;
6278
6279 switch (task->tk_status) {
6280 case 0:
6281 break;
6282 case -NFS4ERR_LAYOUTTRYLATER:
6283 case -NFS4ERR_RECALLCONFLICT:
6284 task->tk_status = -NFS4ERR_DELAY;
6285 /* Fall through */
6286 default:
6287 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6288 rpc_restart_call_prepare(task);
6289 return;
6290 }
6291 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006292 dprintk("<-- %s\n", __func__);
6293}
6294
Idan Kedar85541162012-08-02 11:47:10 +03006295static size_t max_response_pages(struct nfs_server *server)
6296{
6297 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6298 return nfs_page_array_len(0, max_resp_sz);
6299}
6300
6301static void nfs4_free_pages(struct page **pages, size_t size)
6302{
6303 int i;
6304
6305 if (!pages)
6306 return;
6307
6308 for (i = 0; i < size; i++) {
6309 if (!pages[i])
6310 break;
6311 __free_page(pages[i]);
6312 }
6313 kfree(pages);
6314}
6315
6316static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6317{
6318 struct page **pages;
6319 int i;
6320
6321 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6322 if (!pages) {
6323 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6324 return NULL;
6325 }
6326
6327 for (i = 0; i < size; i++) {
6328 pages[i] = alloc_page(gfp_flags);
6329 if (!pages[i]) {
6330 dprintk("%s: failed to allocate page\n", __func__);
6331 nfs4_free_pages(pages, size);
6332 return NULL;
6333 }
6334 }
6335
6336 return pages;
6337}
6338
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006339static void nfs4_layoutget_release(void *calldata)
6340{
6341 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006342 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6343 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006344
6345 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006346 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006347 put_nfs_open_context(lgp->args.ctx);
6348 kfree(calldata);
6349 dprintk("<-- %s\n", __func__);
6350}
6351
6352static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6353 .rpc_call_prepare = nfs4_layoutget_prepare,
6354 .rpc_call_done = nfs4_layoutget_done,
6355 .rpc_release = nfs4_layoutget_release,
6356};
6357
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006358struct pnfs_layout_segment *
6359nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006360{
6361 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006362 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006363 struct rpc_task *task;
6364 struct rpc_message msg = {
6365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6366 .rpc_argp = &lgp->args,
6367 .rpc_resp = &lgp->res,
6368 };
6369 struct rpc_task_setup task_setup_data = {
6370 .rpc_client = server->client,
6371 .rpc_message = &msg,
6372 .callback_ops = &nfs4_layoutget_call_ops,
6373 .callback_data = lgp,
6374 .flags = RPC_TASK_ASYNC,
6375 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006376 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006377 int status = 0;
6378
6379 dprintk("--> %s\n", __func__);
6380
Idan Kedar85541162012-08-02 11:47:10 +03006381 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6382 if (!lgp->args.layout.pages) {
6383 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006384 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006385 }
6386 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6387
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006388 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006389 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006390 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006391 task = rpc_run_task(&task_setup_data);
6392 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006393 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006394 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006395 if (status == 0)
6396 status = task->tk_status;
6397 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006398 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006399 rpc_put_task(task);
6400 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006401 if (status)
6402 return ERR_PTR(status);
6403 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006404}
6405
Benny Halevycbe82602011-05-22 19:52:37 +03006406static void
6407nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6408{
6409 struct nfs4_layoutreturn *lrp = calldata;
6410
6411 dprintk("--> %s\n", __func__);
6412 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006413 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006414 return;
6415 rpc_call_start(task);
6416}
6417
6418static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6419{
6420 struct nfs4_layoutreturn *lrp = calldata;
6421 struct nfs_server *server;
6422
6423 dprintk("--> %s\n", __func__);
6424
6425 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6426 return;
6427
6428 server = NFS_SERVER(lrp->args.inode);
6429 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006430 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006431 return;
6432 }
Benny Halevycbe82602011-05-22 19:52:37 +03006433 dprintk("<-- %s\n", __func__);
6434}
6435
6436static void nfs4_layoutreturn_release(void *calldata)
6437{
6438 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006439 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006440
6441 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006442 spin_lock(&lo->plh_inode->i_lock);
6443 if (lrp->res.lrs_present)
6444 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6445 lo->plh_block_lgets--;
6446 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006447 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006448 kfree(calldata);
6449 dprintk("<-- %s\n", __func__);
6450}
6451
6452static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6453 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6454 .rpc_call_done = nfs4_layoutreturn_done,
6455 .rpc_release = nfs4_layoutreturn_release,
6456};
6457
6458int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6459{
6460 struct rpc_task *task;
6461 struct rpc_message msg = {
6462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6463 .rpc_argp = &lrp->args,
6464 .rpc_resp = &lrp->res,
6465 };
6466 struct rpc_task_setup task_setup_data = {
6467 .rpc_client = lrp->clp->cl_rpcclient,
6468 .rpc_message = &msg,
6469 .callback_ops = &nfs4_layoutreturn_call_ops,
6470 .callback_data = lrp,
6471 };
6472 int status;
6473
6474 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006475 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006476 task = rpc_run_task(&task_setup_data);
6477 if (IS_ERR(task))
6478 return PTR_ERR(task);
6479 status = task->tk_status;
6480 dprintk("<-- %s status=%d\n", __func__, status);
6481 rpc_put_task(task);
6482 return status;
6483}
6484
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006485/*
6486 * Retrieve the list of Data Server devices from the MDS.
6487 */
6488static int _nfs4_getdevicelist(struct nfs_server *server,
6489 const struct nfs_fh *fh,
6490 struct pnfs_devicelist *devlist)
6491{
6492 struct nfs4_getdevicelist_args args = {
6493 .fh = fh,
6494 .layoutclass = server->pnfs_curr_ld->id,
6495 };
6496 struct nfs4_getdevicelist_res res = {
6497 .devlist = devlist,
6498 };
6499 struct rpc_message msg = {
6500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6501 .rpc_argp = &args,
6502 .rpc_resp = &res,
6503 };
6504 int status;
6505
6506 dprintk("--> %s\n", __func__);
6507 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6508 &res.seq_res, 0);
6509 dprintk("<-- %s status=%d\n", __func__, status);
6510 return status;
6511}
6512
6513int nfs4_proc_getdevicelist(struct nfs_server *server,
6514 const struct nfs_fh *fh,
6515 struct pnfs_devicelist *devlist)
6516{
6517 struct nfs4_exception exception = { };
6518 int err;
6519
6520 do {
6521 err = nfs4_handle_exception(server,
6522 _nfs4_getdevicelist(server, fh, devlist),
6523 &exception);
6524 } while (exception.retry);
6525
6526 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6527 err, devlist->num_devs);
6528
6529 return err;
6530}
6531EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6532
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006533static int
6534_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6535{
6536 struct nfs4_getdeviceinfo_args args = {
6537 .pdev = pdev,
6538 };
6539 struct nfs4_getdeviceinfo_res res = {
6540 .pdev = pdev,
6541 };
6542 struct rpc_message msg = {
6543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6544 .rpc_argp = &args,
6545 .rpc_resp = &res,
6546 };
6547 int status;
6548
6549 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006550 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006551 dprintk("<-- %s status=%d\n", __func__, status);
6552
6553 return status;
6554}
6555
6556int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6557{
6558 struct nfs4_exception exception = { };
6559 int err;
6560
6561 do {
6562 err = nfs4_handle_exception(server,
6563 _nfs4_proc_getdeviceinfo(server, pdev),
6564 &exception);
6565 } while (exception.retry);
6566 return err;
6567}
6568EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6569
Andy Adamson863a3c62011-03-23 13:27:54 +00006570static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6571{
6572 struct nfs4_layoutcommit_data *data = calldata;
6573 struct nfs_server *server = NFS_SERVER(data->args.inode);
6574
6575 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006576 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006577 return;
6578 rpc_call_start(task);
6579}
6580
6581static void
6582nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6583{
6584 struct nfs4_layoutcommit_data *data = calldata;
6585 struct nfs_server *server = NFS_SERVER(data->args.inode);
6586
6587 if (!nfs4_sequence_done(task, &data->res.seq_res))
6588 return;
6589
6590 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006591 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6592 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6593 case -NFS4ERR_BADLAYOUT: /* no layout */
6594 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006595 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006596 break;
6597 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006598 nfs_post_op_update_inode_force_wcc(data->args.inode,
6599 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006600 break;
6601 default:
6602 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6603 rpc_restart_call_prepare(task);
6604 return;
6605 }
6606 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006607}
6608
6609static void nfs4_layoutcommit_release(void *calldata)
6610{
6611 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006612 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006613 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006614
Andy Adamsondb29c082011-07-30 20:52:38 -04006615 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006616 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006617 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6618 list_del_init(&lseg->pls_lc_list);
6619 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6620 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006621 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006622 }
Peng Tao92407e72011-10-23 20:21:17 -07006623
6624 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6625 smp_mb__after_clear_bit();
6626 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6627
Andy Adamson863a3c62011-03-23 13:27:54 +00006628 put_rpccred(data->cred);
6629 kfree(data);
6630}
6631
6632static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6633 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6634 .rpc_call_done = nfs4_layoutcommit_done,
6635 .rpc_release = nfs4_layoutcommit_release,
6636};
6637
6638int
Andy Adamsonef311532011-03-12 02:58:10 -05006639nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006640{
6641 struct rpc_message msg = {
6642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6643 .rpc_argp = &data->args,
6644 .rpc_resp = &data->res,
6645 .rpc_cred = data->cred,
6646 };
6647 struct rpc_task_setup task_setup_data = {
6648 .task = &data->task,
6649 .rpc_client = NFS_CLIENT(data->args.inode),
6650 .rpc_message = &msg,
6651 .callback_ops = &nfs4_layoutcommit_ops,
6652 .callback_data = data,
6653 .flags = RPC_TASK_ASYNC,
6654 };
6655 struct rpc_task *task;
6656 int status = 0;
6657
6658 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6659 "lbw: %llu inode %lu\n",
6660 data->task.tk_pid, sync,
6661 data->args.lastbytewritten,
6662 data->args.inode->i_ino);
6663
Trond Myklebust9d12b212012-01-17 22:04:25 -05006664 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006665 task = rpc_run_task(&task_setup_data);
6666 if (IS_ERR(task))
6667 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006668 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006669 goto out;
6670 status = nfs4_wait_for_completion_rpc_task(task);
6671 if (status != 0)
6672 goto out;
6673 status = task->tk_status;
6674out:
6675 dprintk("%s: status %d\n", __func__, status);
6676 rpc_put_task(task);
6677 return status;
6678}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006679
6680static int
6681_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6682 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6683{
6684 struct nfs41_secinfo_no_name_args args = {
6685 .style = SECINFO_STYLE_CURRENT_FH,
6686 };
6687 struct nfs4_secinfo_res res = {
6688 .flavors = flavors,
6689 };
6690 struct rpc_message msg = {
6691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6692 .rpc_argp = &args,
6693 .rpc_resp = &res,
6694 };
6695 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6696}
6697
6698static int
6699nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6700 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6701{
6702 struct nfs4_exception exception = { };
6703 int err;
6704 do {
6705 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6706 switch (err) {
6707 case 0:
6708 case -NFS4ERR_WRONGSEC:
6709 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006710 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006711 default:
6712 err = nfs4_handle_exception(server, err, &exception);
6713 }
6714 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006715out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006716 return err;
6717}
6718
6719static int
6720nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6721 struct nfs_fsinfo *info)
6722{
6723 int err;
6724 struct page *page;
6725 rpc_authflavor_t flavor;
6726 struct nfs4_secinfo_flavors *flavors;
6727
6728 page = alloc_page(GFP_KERNEL);
6729 if (!page) {
6730 err = -ENOMEM;
6731 goto out;
6732 }
6733
6734 flavors = page_address(page);
6735 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6736
6737 /*
6738 * Fall back on "guess and check" method if
6739 * the server doesn't support SECINFO_NO_NAME
6740 */
6741 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6742 err = nfs4_find_root_sec(server, fhandle, info);
6743 goto out_freepage;
6744 }
6745 if (err)
6746 goto out_freepage;
6747
6748 flavor = nfs_find_best_sec(flavors);
6749 if (err == 0)
6750 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6751
6752out_freepage:
6753 put_page(page);
6754 if (err == -EACCES)
6755 return -EPERM;
6756out:
6757 return err;
6758}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006759
6760static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006761{
6762 int status;
6763 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006764 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006765 };
6766 struct nfs41_test_stateid_res res;
6767 struct rpc_message msg = {
6768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6769 .rpc_argp = &args,
6770 .rpc_resp = &res,
6771 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006772
Chuck Lever38527b12012-07-11 16:30:23 -04006773 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006774 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006775 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006776 if (status != NFS_OK) {
6777 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006778 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006779 }
6780 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006781 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006782}
6783
Chuck Lever38527b12012-07-11 16:30:23 -04006784/**
6785 * nfs41_test_stateid - perform a TEST_STATEID operation
6786 *
6787 * @server: server / transport on which to perform the operation
6788 * @stateid: state ID to test
6789 *
6790 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6791 * Otherwise a negative NFS4ERR value is returned if the operation
6792 * failed or the state ID is not currently valid.
6793 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006794static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006795{
6796 struct nfs4_exception exception = { };
6797 int err;
6798 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006799 err = _nfs41_test_stateid(server, stateid);
6800 if (err != -NFS4ERR_DELAY)
6801 break;
6802 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006803 } while (exception.retry);
6804 return err;
6805}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006806
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006807static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006808{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006809 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006810 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006811 };
6812 struct nfs41_free_stateid_res res;
6813 struct rpc_message msg = {
6814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6815 .rpc_argp = &args,
6816 .rpc_resp = &res,
6817 };
Chuck Lever38527b12012-07-11 16:30:23 -04006818 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006819
Chuck Lever38527b12012-07-11 16:30:23 -04006820 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006821 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006822 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006823 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006824 dprintk("NFS reply free_stateid: %d\n", status);
6825 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006826}
6827
Chuck Lever38527b12012-07-11 16:30:23 -04006828/**
6829 * nfs41_free_stateid - perform a FREE_STATEID operation
6830 *
6831 * @server: server / transport on which to perform the operation
6832 * @stateid: state ID to release
6833 *
6834 * Returns NFS_OK if the server freed "stateid". Otherwise a
6835 * negative NFS4ERR value is returned.
6836 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006837static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006838{
6839 struct nfs4_exception exception = { };
6840 int err;
6841 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006842 err = _nfs4_free_stateid(server, stateid);
6843 if (err != -NFS4ERR_DELAY)
6844 break;
6845 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006846 } while (exception.retry);
6847 return err;
6848}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006849
6850static bool nfs41_match_stateid(const nfs4_stateid *s1,
6851 const nfs4_stateid *s2)
6852{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006853 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006854 return false;
6855
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006856 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006857 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006858 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006859 return true;
6860
6861 return false;
6862}
6863
Andy Adamson557134a2009-04-01 09:21:53 -04006864#endif /* CONFIG_NFS_V4_1 */
6865
Trond Myklebust36281ca2012-03-04 18:13:56 -05006866static bool nfs4_match_stateid(const nfs4_stateid *s1,
6867 const nfs4_stateid *s2)
6868{
Trond Myklebustf597c532012-03-04 18:13:56 -05006869 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006870}
6871
6872
Trond Myklebust17280172012-03-11 13:11:00 -04006873static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006874 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006875 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876 .recover_open = nfs4_open_reclaim,
6877 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006878 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006879 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006880 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881};
6882
Andy Adamson591d71c2009-04-01 09:22:47 -04006883#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006884static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006885 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6886 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6887 .recover_open = nfs4_open_reclaim,
6888 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006889 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006890 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006891 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006892 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006893};
6894#endif /* CONFIG_NFS_V4_1 */
6895
Trond Myklebust17280172012-03-11 13:11:00 -04006896static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006897 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006898 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 .recover_open = nfs4_open_expired,
6900 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006901 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006902 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903};
6904
Andy Adamson591d71c2009-04-01 09:22:47 -04006905#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006906static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006907 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6908 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006909 .recover_open = nfs41_open_expired,
6910 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006911 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006912 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006913};
6914#endif /* CONFIG_NFS_V4_1 */
6915
Trond Myklebust17280172012-03-11 13:11:00 -04006916static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006917 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006918 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006919 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006920};
6921
6922#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006923static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006924 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006925 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006926 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006927};
6928#endif
6929
Trond Myklebust97dc1352010-06-16 09:52:26 -04006930static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6931 .minor_version = 0,
6932 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006933 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006934 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006935 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6936 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6937 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006938};
6939
6940#if defined(CONFIG_NFS_V4_1)
6941static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6942 .minor_version = 1,
6943 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006944 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006945 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006946 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6947 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6948 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006949};
6950#endif
6951
Trond Myklebust97dc1352010-06-16 09:52:26 -04006952const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6953 [0] = &nfs_v4_0_minor_ops,
6954#if defined(CONFIG_NFS_V4_1)
6955 [1] = &nfs_v4_1_minor_ops,
6956#endif
6957};
6958
Bryan Schumaker73a79702012-07-16 16:39:12 -04006959const struct inode_operations nfs4_dir_inode_operations = {
6960 .create = nfs_create,
6961 .lookup = nfs_lookup,
6962 .atomic_open = nfs_atomic_open,
6963 .link = nfs_link,
6964 .unlink = nfs_unlink,
6965 .symlink = nfs_symlink,
6966 .mkdir = nfs_mkdir,
6967 .rmdir = nfs_rmdir,
6968 .mknod = nfs_mknod,
6969 .rename = nfs_rename,
6970 .permission = nfs_permission,
6971 .getattr = nfs_getattr,
6972 .setattr = nfs_setattr,
6973 .getxattr = generic_getxattr,
6974 .setxattr = generic_setxattr,
6975 .listxattr = generic_listxattr,
6976 .removexattr = generic_removexattr,
6977};
6978
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006979static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006980 .permission = nfs_permission,
6981 .getattr = nfs_getattr,
6982 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006983 .getxattr = generic_getxattr,
6984 .setxattr = generic_setxattr,
6985 .listxattr = generic_listxattr,
6986 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006987};
6988
David Howells509de812006-08-22 20:06:11 -04006989const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 .version = 4, /* protocol version */
6991 .dentry_ops = &nfs4_dentry_operations,
6992 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006993 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006994 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006996 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006997 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 .getattr = nfs4_proc_getattr,
6999 .setattr = nfs4_proc_setattr,
7000 .lookup = nfs4_proc_lookup,
7001 .access = nfs4_proc_access,
7002 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 .create = nfs4_proc_create,
7004 .remove = nfs4_proc_remove,
7005 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007006 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 .unlink_done = nfs4_proc_unlink_done,
7008 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007009 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007010 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007011 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 .link = nfs4_proc_link,
7013 .symlink = nfs4_proc_symlink,
7014 .mkdir = nfs4_proc_mkdir,
7015 .rmdir = nfs4_proc_remove,
7016 .readdir = nfs4_proc_readdir,
7017 .mknod = nfs4_proc_mknod,
7018 .statfs = nfs4_proc_statfs,
7019 .fsinfo = nfs4_proc_fsinfo,
7020 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007021 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022 .decode_dirent = nfs4_decode_dirent,
7023 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007024 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007025 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007026 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007028 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007029 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007030 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007032 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007033 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007035 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007036 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007037 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007038 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007039 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007040 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007041 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007042 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007043 .create_server = nfs4_create_server,
7044 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045};
7046
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007047static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7048 .prefix = XATTR_NAME_NFSV4_ACL,
7049 .list = nfs4_xattr_list_nfs4_acl,
7050 .get = nfs4_xattr_get_nfs4_acl,
7051 .set = nfs4_xattr_set_nfs4_acl,
7052};
7053
7054const struct xattr_handler *nfs4_xattr_handlers[] = {
7055 &nfs4_xattr_nfs4_acl_handler,
7056 NULL
7057};
7058
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059/*
7060 * Local variables:
7061 * c-basic-offset: 8
7062 * End:
7063 */