blob: e10d66f5be0ad0dd5c83d627daaf745fc221eb42 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010058#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebust4ce79712005-06-22 17:16:21 +000060#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050062#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050063#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040064#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040065#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040066#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustcdd4e682006-01-03 09:55:12 +010075struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010076static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080077static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050079static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050080static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040081static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040082static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040083static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050091static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Trond Myklebust52567b02009-10-23 14:46:42 -040093 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
97 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000098 case -NFS4ERR_WRONGSEC:
99 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
102 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400103 case -NFS4ERR_SHARE_DENIED:
104 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400107 case -NFS4ERR_ACCESS:
108 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700111 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400114 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/*
118 * This is our standard bitmap for GETATTR requests.
119 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400120const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 FATTR4_WORD0_TYPE
122 | FATTR4_WORD0_CHANGE
123 | FATTR4_WORD0_SIZE
124 | FATTR4_WORD0_FSID
125 | FATTR4_WORD0_FILEID,
126 FATTR4_WORD1_MODE
127 | FATTR4_WORD1_NUMLINKS
128 | FATTR4_WORD1_OWNER
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
135};
136
Trond Myklebust1549210f2012-06-05 09:16:47 -0400137static const u32 nfs4_pnfs_open_bitmap[3] = {
138 FATTR4_WORD0_TYPE
139 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_SIZE
141 | FATTR4_WORD0_FSID
142 | FATTR4_WORD0_FILEID,
143 FATTR4_WORD1_MODE
144 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
153};
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155const u32 nfs4_statfs_bitmap[2] = {
156 FATTR4_WORD0_FILES_AVAIL
157 | FATTR4_WORD0_FILES_FREE
158 | FATTR4_WORD0_FILES_TOTAL,
159 FATTR4_WORD1_SPACE_AVAIL
160 | FATTR4_WORD1_SPACE_FREE
161 | FATTR4_WORD1_SPACE_TOTAL
162};
163
Trond Myklebust4ce79712005-06-22 17:16:21 +0000164const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 FATTR4_WORD0_MAXLINK
166 | FATTR4_WORD0_MAXNAME,
167 0
168};
169
Fred Isamandae100c2011-07-30 20:52:37 -0400170const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 | FATTR4_WORD0_MAXREAD
172 | FATTR4_WORD0_MAXWRITE
173 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700174 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400175 | FATTR4_WORD1_FS_LAYOUT_TYPES,
176 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Manoj Naik830b8e32006-06-09 09:34:25 -0400179const u32 nfs4_fs_locations_bitmap[2] = {
180 FATTR4_WORD0_TYPE
181 | FATTR4_WORD0_CHANGE
182 | FATTR4_WORD0_SIZE
183 | FATTR4_WORD0_FSID
184 | FATTR4_WORD0_FILEID
185 | FATTR4_WORD0_FS_LOCATIONS,
186 FATTR4_WORD1_MODE
187 | FATTR4_WORD1_NUMLINKS
188 | FATTR4_WORD1_OWNER
189 | FATTR4_WORD1_OWNER_GROUP
190 | FATTR4_WORD1_RAWDEV
191 | FATTR4_WORD1_SPACE_USED
192 | FATTR4_WORD1_TIME_ACCESS
193 | FATTR4_WORD1_TIME_METADATA
194 | FATTR4_WORD1_TIME_MODIFY
195 | FATTR4_WORD1_MOUNTED_ON_FILEID
196};
197
Al Virobc4785c2006-10-19 23:28:51 -0700198static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct nfs4_readdir_arg *readdir)
200{
Al Viro0dbb4c62006-10-19 23:28:49 -0700201 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 BUG_ON(readdir->count < 80);
204 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000205 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
207 return;
208 }
209
210 readdir->cookie = 0;
211 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
212 if (cookie == 2)
213 return;
214
215 /*
216 * NFSv4 servers do not return entries for '.' and '..'
217 * Therefore, we fake these entries here. We let '.'
218 * have cookie 0 and '..' have cookie 1. Note that
219 * when talking to the server, we always send cookie 0
220 * instead of 1 or 2.
221 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800222 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (cookie == 0) {
225 *p++ = xdr_one; /* next */
226 *p++ = xdr_zero; /* cookie, first word */
227 *p++ = xdr_one; /* cookie, second word */
228 *p++ = xdr_one; /* entry len */
229 memcpy(p, ".\0\0\0", 4); /* entry */
230 p++;
231 *p++ = xdr_one; /* bitmap length */
232 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
233 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400234 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
237 *p++ = xdr_one; /* next */
238 *p++ = xdr_zero; /* cookie, first word */
239 *p++ = xdr_two; /* cookie, second word */
240 *p++ = xdr_two; /* entry len */
241 memcpy(p, "..\0\0", 4); /* entry */
242 p++;
243 *p++ = xdr_one; /* bitmap length */
244 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
245 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400246 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 readdir->pgbase = (char *)p - (char *)start;
249 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800250 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Trond Myklebust65de8722008-12-23 15:21:44 -0500253static int nfs4_wait_clnt_recover(struct nfs_client *clp)
254{
255 int res;
256
257 might_sleep();
258
Trond Myklebuste005e802008-12-23 15:21:48 -0500259 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400260 nfs_wait_bit_killable, TASK_KILLABLE);
Chuck Lever46a87b82012-07-11 16:30:41 -0400261 if (res)
262 return res;
263
264 if (clp->cl_cons_state < 0)
265 return clp->cl_cons_state;
266 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500267}
268
269static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
270{
271 int res = 0;
272
273 might_sleep();
274
275 if (*timeout <= 0)
276 *timeout = NFS4_POLL_RETRY_MIN;
277 if (*timeout > NFS4_POLL_RETRY_MAX)
278 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100279 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500280 if (fatal_signal_pending(current))
281 res = -ERESTARTSYS;
282 *timeout <<= 1;
283 return res;
284}
285
286/* This is the error handling routine for processes that are allowed
287 * to sleep.
288 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800289static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500290{
291 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500292 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500293 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500294 int ret = errorcode;
295
296 exception->retry = 0;
297 switch(errorcode) {
298 case 0:
299 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500300 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400301 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400302 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500303 exception->retry = 1;
304 return 0;
305 }
306 if (state == NULL)
307 break;
308 nfs4_schedule_stateid_recovery(server, state);
309 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500310 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500311 case -NFS4ERR_ADMIN_REVOKED:
312 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500313 if (state == NULL)
314 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400315 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500316 nfs4_schedule_stateid_recovery(server, state);
317 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400318 case -NFS4ERR_EXPIRED:
319 if (state != NULL)
320 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500321 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500322 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500323 nfs4_schedule_lease_recovery(clp);
324 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500325#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400326 case -NFS4ERR_BADSESSION:
327 case -NFS4ERR_BADSLOT:
328 case -NFS4ERR_BAD_HIGH_SLOT:
329 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
330 case -NFS4ERR_DEADSESSION:
331 case -NFS4ERR_SEQ_FALSE_RETRY:
332 case -NFS4ERR_SEQ_MISORDERED:
333 dprintk("%s ERROR: %d Reset session\n", __func__,
334 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400335 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400336 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500337 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500338#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500339 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500340 if (exception->timeout > HZ) {
341 /* We have retried a decent amount, time to
342 * fail
343 */
344 ret = -EBUSY;
345 break;
346 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500347 case -NFS4ERR_GRACE:
348 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500349 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500350 ret = nfs4_delay(server->client, &exception->timeout);
351 if (ret != 0)
352 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400353 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500354 case -NFS4ERR_OLD_STATEID:
355 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800356 break;
357 case -NFS4ERR_BADOWNER:
358 /* The following works around a Linux server bug! */
359 case -NFS4ERR_BADNAME:
360 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
361 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
362 exception->retry = 1;
363 printk(KERN_WARNING "NFS: v4 server %s "
364 "does not accept raw "
365 "uid/gids. "
366 "Reenabling the idmapper.\n",
367 server->nfs_client->cl_hostname);
368 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500369 }
370 /* We failed to handle the error */
371 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500372wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500373 ret = nfs4_wait_clnt_recover(clp);
374 if (ret == 0)
375 exception->retry = 1;
376 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500377}
378
379
Trond Myklebust452e9352010-07-31 14:29:06 -0400380static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 spin_lock(&clp->cl_lock);
383 if (time_before(clp->cl_last_renewal,timestamp))
384 clp->cl_last_renewal = timestamp;
385 spin_unlock(&clp->cl_lock);
386}
387
Trond Myklebust452e9352010-07-31 14:29:06 -0400388static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
389{
390 do_renew_lease(server->nfs_client, timestamp);
391}
392
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400393#if defined(CONFIG_NFS_V4_1)
394
Benny Halevy510b8172009-04-01 09:22:14 -0400395/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400396 * nfs4_free_slot - free a slot and efficiently update slot table.
397 *
398 * freeing a slot is trivially done by clearing its respective bit
399 * in the bitmap.
400 * If the freed slotid equals highest_used_slotid we want to update it
401 * so that the server would be able to size down the slot table if needed,
402 * otherwise we know that the highest_used_slotid is still in use.
403 * When updating highest_used_slotid there may be "holes" in the bitmap
404 * so we need to scan down from highest_used_slotid to 0 looking for the now
405 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500406 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 *
408 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400409 */
410static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500411nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400412{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500413 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400414 /* clear used bit in bitmap */
415 __clear_bit(slotid, tbl->used_slots);
416
417 /* update highest_used_slotid when it is freed */
418 if (slotid == tbl->highest_used_slotid) {
419 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500420 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400421 tbl->highest_used_slotid = slotid;
422 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500423 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400424 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500425 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
426 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400427}
428
Trond Myklebust961a8282012-01-17 22:57:37 -0500429bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
430{
431 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
432 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400433}
434
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800435/*
Andy Adamson42acd022011-01-06 02:04:34 +0000436 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800437 */
Andy Adamson42acd022011-01-06 02:04:34 +0000438static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800439{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400440 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500441 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
442 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800443 return;
444 }
445
Trond Myklebust45d43c22012-02-06 19:38:51 -0500446 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800447 return;
448
Andy Adamson42acd022011-01-06 02:04:34 +0000449 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
450 complete(&ses->fc_slot_table.complete);
451}
452
453/*
454 * Signal state manager thread if session back channel is drained
455 */
456void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
457{
458 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500459 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000460 return;
461 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
462 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800463}
464
Trond Myklebustd185a332010-06-16 09:52:25 -0400465static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400466{
467 struct nfs4_slot_table *tbl;
468
Trond Myklebustd185a332010-06-16 09:52:25 -0400469 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400470 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400471 /* just wake up the next guy waiting since
472 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500473 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500474 return;
Andy Adamson13615872009-04-01 09:22:17 -0400475 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500476
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500477 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500478 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000479 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500480 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400481 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400482}
483
Trond Myklebust14516c32010-07-31 14:29:06 -0400484static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400485{
486 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400487 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400488
489 /*
490 * sr_status remains 1 if an RPC level error occurred. The server
491 * may or may not have processed the sequence operation..
492 * Proceed as if the server received and processed the sequence
493 * operation.
494 */
495 if (res->sr_status == 1)
496 res->sr_status = NFS_OK;
497
Bryan Schumaker468f8612011-04-18 15:57:32 -0400498 /* don't increment the sequence number if the task wasn't sent */
499 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400500 goto out;
501
Andy Adamson691daf32009-12-04 15:55:39 -0500502 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400503 switch (res->sr_status) {
504 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400505 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400506 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400507 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400508 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400509 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500510 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500511 if (res->sr_status_flags != 0)
512 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 break;
514 case -NFS4ERR_DELAY:
515 /* The server detected a resend of the RPC call and
516 * returned NFS4ERR_DELAY as per Section 2.10.6.2
517 * of RFC5661.
518 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200519 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400520 __func__,
521 res->sr_slot - res->sr_session->fc_slot_table.slots,
522 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400523 goto out_retry;
524 default:
525 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400526 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400527 }
528out:
529 /* The session may be reset by one of the error handlers. */
530 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400531 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400532 return 1;
533out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400534 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400535 goto out;
536 rpc_delay(task, NFS4_POLL_RETRY_MAX);
537 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400538}
539
Trond Myklebust14516c32010-07-31 14:29:06 -0400540static int nfs4_sequence_done(struct rpc_task *task,
541 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400542{
Trond Myklebust14516c32010-07-31 14:29:06 -0400543 if (res->sr_session == NULL)
544 return 1;
545 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400546}
547
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400548/*
Benny Halevy510b8172009-04-01 09:22:14 -0400549 * nfs4_find_slot - efficiently look for a free slot
550 *
551 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
552 * If found, we mark the slot as used, update the highest_used_slotid,
553 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500554 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555 *
556 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400557 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500558static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800559nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400560{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500561 u32 slotid;
562 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400563
Trond Myklebust45d43c22012-02-06 19:38:51 -0500564 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400565 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
566 tbl->max_slots);
567 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
568 if (slotid >= tbl->max_slots)
569 goto out;
570 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500571 if (slotid > tbl->highest_used_slotid ||
572 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400573 tbl->highest_used_slotid = slotid;
574 ret_id = slotid;
575out:
576 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
577 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400578 return ret_id;
579}
580
Trond Myklebust9d12b212012-01-17 22:04:25 -0500581static void nfs41_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
583{
584 args->sa_session = NULL;
585 args->sa_cache_this = 0;
586 if (cache_reply)
587 args->sa_cache_this = 1;
588 res->sr_session = NULL;
589 res->sr_slot = NULL;
590}
591
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000592int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct nfs4_sequence_args *args,
594 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595 struct rpc_task *task)
596{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400597 struct nfs4_slot *slot;
598 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500599 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600
601 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400603 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 return 0;
605
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400606 tbl = &session->fc_slot_table;
607
608 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400609 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800610 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500611 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500612 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400613 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500614 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500615 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400616 }
617
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800618 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
619 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
620 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
621 spin_unlock(&tbl->slot_tbl_lock);
622 dprintk("%s enforce FIFO order\n", __func__);
623 return -EAGAIN;
624 }
625
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800626 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500627 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400628 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
629 spin_unlock(&tbl->slot_tbl_lock);
630 dprintk("<-- %s: no free slots\n", __func__);
631 return -EAGAIN;
632 }
633 spin_unlock(&tbl->slot_tbl_lock);
634
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800635 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400636 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400637 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400638 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400639
640 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
641
Benny Halevy99fe60d2009-04-01 09:22:29 -0400642 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400643 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400644 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400645 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400646 /*
647 * sr_status is only set in decode_sequence, and so will remain
648 * set to 1 if an rpc level failure occurs.
649 */
650 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400651 return 0;
652}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000653EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654
Trond Myklebust035168a2010-06-16 09:52:26 -0400655int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 struct nfs4_sequence_args *args,
657 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 struct rpc_task *task)
659{
Trond Myklebust035168a2010-06-16 09:52:26 -0400660 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 int ret = 0;
662
Trond Myklebust9d12b212012-01-17 22:04:25 -0500663 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400665
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200666 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400667 __func__, session->clp, session, res->sr_slot ?
668 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400669
Trond Myklebust9d12b212012-01-17 22:04:25 -0500670 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671out:
672 dprintk("<-- %s status=%d\n", __func__, ret);
673 return ret;
674}
675
676struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400677 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678 struct nfs4_sequence_args *seq_args;
679 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680};
681
682static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
683{
684 struct nfs41_call_sync_data *data = calldata;
685
Trond Myklebust035168a2010-06-16 09:52:26 -0400686 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
687
688 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500689 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 return;
691 rpc_call_start(task);
692}
693
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800694static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
695{
696 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
697 nfs41_call_sync_prepare(task, calldata);
698}
699
Andy Adamson69ab40c2009-04-01 09:22:19 -0400700static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
701{
702 struct nfs41_call_sync_data *data = calldata;
703
Trond Myklebust14516c32010-07-31 14:29:06 -0400704 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400705}
706
Trond Myklebust17280172012-03-11 13:11:00 -0400707static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400709 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400710};
711
Trond Myklebust17280172012-03-11 13:11:00 -0400712static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800713 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
714 .rpc_call_done = nfs41_call_sync_done,
715};
716
Bryan Schumaker7c513052011-03-24 17:12:24 +0000717static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
718 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400719 struct rpc_message *msg,
720 struct nfs4_sequence_args *args,
721 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800722 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400723{
724 int ret;
725 struct rpc_task *task;
726 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400727 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400728 .seq_args = args,
729 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400730 };
731 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000732 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400733 .rpc_message = msg,
734 .callback_ops = &nfs41_call_sync_ops,
735 .callback_data = &data
736 };
737
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800738 if (privileged)
739 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400740 task = rpc_run_task(&task_setup);
741 if (IS_ERR(task))
742 ret = PTR_ERR(task);
743 else {
744 ret = task->tk_status;
745 rpc_put_task(task);
746 }
747 return ret;
748}
749
Bryan Schumaker7c513052011-03-24 17:12:24 +0000750int _nfs4_call_sync_session(struct rpc_clnt *clnt,
751 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400752 struct rpc_message *msg,
753 struct nfs4_sequence_args *args,
754 struct nfs4_sequence_res *res,
755 int cache_reply)
756{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500757 nfs41_init_sequence(args, res, cache_reply);
758 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400759}
760
Trond Myklebustdf896452010-06-16 09:52:26 -0400761#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500762static inline
763void nfs41_init_sequence(struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res, int cache_reply)
765{
766}
767
Trond Myklebust14516c32010-07-31 14:29:06 -0400768static int nfs4_sequence_done(struct rpc_task *task,
769 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400770{
Trond Myklebust14516c32010-07-31 14:29:06 -0400771 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400772}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400773#endif /* CONFIG_NFS_V4_1 */
774
Bryan Schumaker7c513052011-03-24 17:12:24 +0000775int _nfs4_call_sync(struct rpc_clnt *clnt,
776 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400777 struct rpc_message *msg,
778 struct nfs4_sequence_args *args,
779 struct nfs4_sequence_res *res,
780 int cache_reply)
781{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500782 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000783 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400784}
785
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000786static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000787int nfs4_call_sync(struct rpc_clnt *clnt,
788 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000789 struct rpc_message *msg,
790 struct nfs4_sequence_args *args,
791 struct nfs4_sequence_res *res,
792 int cache_reply)
793{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000794 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
795 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000796}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400797
Trond Myklebust38478b22006-05-25 01:40:57 -0400798static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Trond Myklebust38478b22006-05-25 01:40:57 -0400800 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Trond Myklebust38478b22006-05-25 01:40:57 -0400802 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400803 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700804 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400805 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700806 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400807 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400811 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 struct nfs_openargs o_arg;
813 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100814 struct nfs_open_confirmargs c_arg;
815 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500816 struct nfs4_string owner_name;
817 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400820 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400822 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100824 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400825 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100826 int rpc_status;
827 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828};
829
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400830
831static void nfs4_init_opendata_res(struct nfs4_opendata *p)
832{
833 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400834 p->o_res.seqid = p->o_arg.seqid;
835 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400836 p->o_res.server = p->o_arg.server;
837 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500838 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400839}
840
Al Viro82a2c1b2011-06-22 18:30:55 -0400841static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400843 const struct iattr *attrs,
844 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845{
Al Viro82a2c1b2011-06-22 18:30:55 -0400846 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847 struct inode *dir = parent->d_inode;
848 struct nfs_server *server = NFS_SERVER(dir);
849 struct nfs4_opendata *p;
850
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400851 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 if (p == NULL)
853 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400854 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 if (p->o_arg.seqid == NULL)
856 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400857 nfs_sb_active(dentry->d_sb);
858 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 p->dir = parent;
860 p->owner = sp;
861 atomic_inc(&sp->so_count);
862 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500863 p->o_arg.open_flags = flags;
864 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400865 /* ask server to check for all possible rights as results are cached */
866 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
867 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
David Howells7539bba2006-08-22 20:06:09 -0400868 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400869 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
870 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400871 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100872 p->o_arg.server = server;
873 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400874 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500876 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500877 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400878
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879 p->o_arg.u.attrs = &p->attrs;
880 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500881
882 verf[0] = jiffies;
883 verf[1] = current->pid;
884 memcpy(p->o_arg.u.verifier.data, verf,
885 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100887 p->c_arg.fh = &p->o_res.fh;
888 p->c_arg.stateid = &p->o_res.stateid;
889 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400890 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400891 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100892 return p;
893err_free:
894 kfree(p);
895err:
896 dput(parent);
897 return NULL;
898}
899
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400900static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100901{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400902 struct nfs4_opendata *p = container_of(kref,
903 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400904 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400905
906 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400907 if (p->state != NULL)
908 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400909 nfs4_put_state_owner(p->owner);
910 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400911 dput(p->dentry);
912 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500913 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400914 kfree(p);
915}
916
917static void nfs4_opendata_put(struct nfs4_opendata *p)
918{
919 if (p != NULL)
920 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100921}
922
Trond Myklebust06f814a2006-01-03 09:55:07 +0100923static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
924{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100925 int ret;
926
Trond Myklebust06f814a2006-01-03 09:55:07 +0100927 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100928 return ret;
929}
930
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400932{
933 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934
Trond Myklebust536e43d2012-01-17 22:04:26 -0500935 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936 goto out;
937 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500939 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
940 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400941 break;
942 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500943 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
944 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400945 break;
946 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500947 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
948 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400949 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400951 return ret;
952}
953
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400955{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500956 if (delegation == NULL)
957 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400959 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500960 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400961 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500962 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400963 return 1;
964}
965
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100967{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100969 case FMODE_WRITE:
970 state->n_wronly++;
971 break;
972 case FMODE_READ:
973 state->n_rdonly++;
974 break;
975 case FMODE_READ|FMODE_WRITE:
976 state->n_rdwr++;
977 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100979}
980
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400982{
983 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500984 nfs4_stateid_copy(&state->stateid, stateid);
985 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400987 case FMODE_READ:
988 set_bit(NFS_O_RDONLY_STATE, &state->flags);
989 break;
990 case FMODE_WRITE:
991 set_bit(NFS_O_WRONLY_STATE, &state->flags);
992 break;
993 case FMODE_READ|FMODE_WRITE:
994 set_bit(NFS_O_RDWR_STATE, &state->flags);
995 }
996}
997
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400999{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001000 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001002 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001003}
1004
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001007 /*
1008 * Protect the call to nfs4_state_set_mode_locked and
1009 * serialise the stateid update
1010 */
1011 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001012 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001013 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001014 set_bit(NFS_DELEGATED_STATE, &state->flags);
1015 }
1016 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001018 write_sequnlock(&state->seqlock);
1019 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001021 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001025{
1026 struct nfs_inode *nfsi = NFS_I(state->inode);
1027 struct nfs_delegation *deleg_cur;
1028 int ret = 0;
1029
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001031
1032 rcu_read_lock();
1033 deleg_cur = rcu_dereference(nfsi->delegation);
1034 if (deleg_cur == NULL)
1035 goto no_delegation;
1036
1037 spin_lock(&deleg_cur->lock);
1038 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001040 goto no_delegation_unlock;
1041
1042 if (delegation == NULL)
1043 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001044 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001045 goto no_delegation_unlock;
1046
Trond Myklebustb7391f42008-12-23 15:21:52 -05001047 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001049 ret = 1;
1050no_delegation_unlock:
1051 spin_unlock(&deleg_cur->lock);
1052no_delegation:
1053 rcu_read_unlock();
1054
1055 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001057 ret = 1;
1058 }
1059
1060 return ret;
1061}
1062
1063
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001064static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001065{
1066 struct nfs_delegation *delegation;
1067
1068 rcu_read_lock();
1069 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071 rcu_read_unlock();
1072 return;
1073 }
1074 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001075 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076}
1077
Trond Myklebust6ee41262007-07-08 14:11:36 -04001078static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001079{
1080 struct nfs4_state *state = opendata->state;
1081 struct nfs_inode *nfsi = NFS_I(state->inode);
1082 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001083 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 nfs4_stateid stateid;
1086 int ret = -EAGAIN;
1087
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001091 if (can_open_cached(state, fmode, open_mode)) {
1092 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001093 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001094 goto out_return_state;
1095 }
1096 spin_unlock(&state->owner->so_lock);
1097 }
Trond Myklebust34310432008-12-23 15:21:38 -05001098 rcu_read_lock();
1099 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001100 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001101 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001102 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001103 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001105 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001106 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001107 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108 if (ret != 0)
1109 goto out;
1110 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001111
1112 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001113 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001114 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001115 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116out:
1117 return ERR_PTR(ret);
1118out_return_state:
1119 atomic_inc(&state->count);
1120 return state;
1121}
1122
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001123static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1124{
1125 struct inode *inode;
1126 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001127 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001128 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129
Trond Myklebustaac00a82007-07-05 19:02:21 -04001130 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001131 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001132 goto out;
1133 }
1134
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001135 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001136 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001137 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001138 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001139 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001140 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001141 goto err;
1142 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001143 state = nfs4_get_open_state(inode, data->owner);
1144 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001145 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001146 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001147 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001148 int delegation_flags = 0;
1149
Trond Myklebust003707c2007-07-05 18:07:55 -04001150 rcu_read_lock();
1151 delegation = rcu_dereference(NFS_I(inode)->delegation);
1152 if (delegation)
1153 delegation_flags = delegation->flags;
1154 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001155 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1156 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1157 "returning a delegation for "
1158 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001159 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001160 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001161 nfs_inode_set_delegation(state->inode,
1162 data->owner->so_cred,
1163 &data->o_res);
1164 else
1165 nfs_inode_reclaim_delegation(state->inode,
1166 data->owner->so_cred,
1167 &data->o_res);
1168 }
Trond Myklebust34310432008-12-23 15:21:38 -05001169
1170 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001171 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001172 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001173out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001174 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001175err_put_inode:
1176 iput(inode);
1177err:
1178 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001179}
1180
Trond Myklebust864472e2006-01-03 09:55:15 +01001181static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1182{
1183 struct nfs_inode *nfsi = NFS_I(state->inode);
1184 struct nfs_open_context *ctx;
1185
1186 spin_lock(&state->inode->i_lock);
1187 list_for_each_entry(ctx, &nfsi->open_files, list) {
1188 if (ctx->state != state)
1189 continue;
1190 get_nfs_open_context(ctx);
1191 spin_unlock(&state->inode->i_lock);
1192 return ctx;
1193 }
1194 spin_unlock(&state->inode->i_lock);
1195 return ERR_PTR(-ENOENT);
1196}
1197
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001198static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1199{
1200 struct nfs4_opendata *opendata;
1201
Al Viro3d4ff432011-06-22 18:40:12 -04001202 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001203 if (opendata == NULL)
1204 return ERR_PTR(-ENOMEM);
1205 opendata->state = state;
1206 atomic_inc(&state->count);
1207 return opendata;
1208}
1209
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001211{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 int ret;
1214
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 opendata->o_arg.open_flags = 0;
1216 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001217 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1218 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1219 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001220 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001221 if (ret != 0)
1222 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001223 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001224 if (IS_ERR(newstate))
1225 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001226 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001227 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 return 0;
1229}
1230
1231static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1232{
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 int ret;
1235
1236 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001237 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001238 smp_rmb();
1239 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001240 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001241 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001242 if (ret != 0)
1243 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001244 if (newstate != state)
1245 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 }
1247 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001248 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001249 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001250 if (ret != 0)
1251 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001252 if (newstate != state)
1253 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001254 }
1255 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001256 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001257 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001258 if (ret != 0)
1259 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001260 if (newstate != state)
1261 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001262 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001263 /*
1264 * We may have performed cached opens for all three recoveries.
1265 * Check if we need to update the current stateid.
1266 */
1267 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001268 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001269 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001270 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001271 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001272 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001273 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001274 return 0;
1275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277/*
1278 * OPEN_RECLAIM:
1279 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001281static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001283 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001284 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001285 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int status;
1287
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001288 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1289 if (IS_ERR(opendata))
1290 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001291 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1292 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001293 rcu_read_lock();
1294 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001295 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001296 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001297 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001298 opendata->o_arg.u.delegation_type = delegation_type;
1299 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001300 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return status;
1302}
1303
Trond Myklebust539cd032007-06-05 11:46:42 -04001304static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 struct nfs_server *server = NFS_SERVER(state->inode);
1307 struct nfs4_exception exception = { };
1308 int err;
1309 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001310 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001311 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001312 break;
1313 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 } while (exception.retry);
1315 return err;
1316}
1317
Trond Myklebust864472e2006-01-03 09:55:15 +01001318static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1319{
1320 struct nfs_open_context *ctx;
1321 int ret;
1322
1323 ctx = nfs4_state_find_open_context(state);
1324 if (IS_ERR(ctx))
1325 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001326 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001327 put_nfs_open_context(ctx);
1328 return ret;
1329}
1330
Trond Myklebust13437e12007-07-06 15:10:43 -04001331static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001333 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001334 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001336 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1337 if (IS_ERR(opendata))
1338 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001339 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001340 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001341 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001342 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001343 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Trond Myklebust13437e12007-07-06 15:10:43 -04001346int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001349 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 int err;
1351 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001352 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 switch (err) {
1354 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001355 case -ENOENT:
1356 case -ESTALE:
1357 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001358 case -NFS4ERR_BADSESSION:
1359 case -NFS4ERR_BADSLOT:
1360 case -NFS4ERR_BAD_HIGH_SLOT:
1361 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1362 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001363 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 case -NFS4ERR_STALE_CLIENTID:
1366 case -NFS4ERR_STALE_STATEID:
1367 case -NFS4ERR_EXPIRED:
1368 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001369 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001370 goto out;
1371 case -ERESTARTSYS:
1372 /*
1373 * The show must go on: exit, but mark the
1374 * stateid as needing recovery.
1375 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001376 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001377 case -NFS4ERR_ADMIN_REVOKED:
1378 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001379 nfs_inode_find_state_and_recover(state->inode,
1380 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001381 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001382 case -EKEYEXPIRED:
1383 /*
1384 * User RPCSEC_GSS context has expired.
1385 * We cannot recover this stateid now, so
1386 * skip it and allow recovery thread to
1387 * proceed.
1388 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001389 case -ENOMEM:
1390 err = 0;
1391 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393 err = nfs4_handle_exception(server, err, &exception);
1394 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001395out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return err;
1397}
1398
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1400{
1401 struct nfs4_opendata *data = calldata;
1402
1403 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001404 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001405 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001406 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001407 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001408 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001409 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001410}
1411
1412static void nfs4_open_confirm_release(void *calldata)
1413{
1414 struct nfs4_opendata *data = calldata;
1415 struct nfs4_state *state = NULL;
1416
1417 /* If this request hasn't been cancelled, do nothing */
1418 if (data->cancelled == 0)
1419 goto out_free;
1420 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001421 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001422 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001423 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001424 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001425 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001426out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001427 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001428}
1429
1430static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001431 .rpc_call_done = nfs4_open_confirm_done,
1432 .rpc_release = nfs4_open_confirm_release,
1433};
1434
1435/*
1436 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1437 */
1438static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1439{
1440 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1441 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001442 struct rpc_message msg = {
1443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1444 .rpc_argp = &data->c_arg,
1445 .rpc_resp = &data->c_res,
1446 .rpc_cred = data->owner->so_cred,
1447 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001448 struct rpc_task_setup task_setup_data = {
1449 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001450 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001451 .callback_ops = &nfs4_open_confirm_ops,
1452 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001453 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001454 .flags = RPC_TASK_ASYNC,
1455 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 int status;
1457
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001458 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001459 data->rpc_done = 0;
1460 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001461 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001462 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001463 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001464 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001465 status = nfs4_wait_for_completion_rpc_task(task);
1466 if (status != 0) {
1467 data->cancelled = 1;
1468 smp_wmb();
1469 } else
1470 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001471 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return status;
1473}
1474
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001477 struct nfs4_opendata *data = calldata;
1478 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001479
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001480 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1481 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001482 /*
1483 * Check if we still need to send an OPEN call, or if we can use
1484 * a delegation instead.
1485 */
1486 if (data->state != NULL) {
1487 struct nfs_delegation *delegation;
1488
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001489 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001490 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001491 rcu_read_lock();
1492 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001493 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1494 can_open_delegated(delegation, data->o_arg.fmode))
1495 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001496 rcu_read_unlock();
1497 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001498 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001499 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001500 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001501 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001502 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1503 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001504 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001505 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001506 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001507 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001508 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001509 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001510 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001511unlock_no_action:
1512 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001513out_no_action:
1514 task->tk_action = NULL;
1515
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001518static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1519{
1520 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1521 nfs4_open_prepare(task, calldata);
1522}
1523
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524static void nfs4_open_done(struct rpc_task *task, void *calldata)
1525{
1526 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001529
Trond Myklebust14516c32010-07-31 14:29:06 -04001530 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1531 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001532
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 if (task->tk_status == 0) {
1534 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001535 case S_IFREG:
1536 break;
1537 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001539 break;
1540 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001541 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001542 break;
1543 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001545 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001546 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001547 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1548 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001549 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001550 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001552
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001553static void nfs4_open_release(void *calldata)
1554{
1555 struct nfs4_opendata *data = calldata;
1556 struct nfs4_state *state = NULL;
1557
1558 /* If this request hasn't been cancelled, do nothing */
1559 if (data->cancelled == 0)
1560 goto out_free;
1561 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001562 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001563 goto out_free;
1564 /* In case we need an open_confirm, no cleanup! */
1565 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1566 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001567 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001568 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001569 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001570out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001571 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001572}
1573
1574static const struct rpc_call_ops nfs4_open_ops = {
1575 .rpc_call_prepare = nfs4_open_prepare,
1576 .rpc_call_done = nfs4_open_done,
1577 .rpc_release = nfs4_open_release,
1578};
1579
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001580static const struct rpc_call_ops nfs4_recover_open_ops = {
1581 .rpc_call_prepare = nfs4_recover_open_prepare,
1582 .rpc_call_done = nfs4_open_done,
1583 .rpc_release = nfs4_open_release,
1584};
1585
1586static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001587{
1588 struct inode *dir = data->dir->d_inode;
1589 struct nfs_server *server = NFS_SERVER(dir);
1590 struct nfs_openargs *o_arg = &data->o_arg;
1591 struct nfs_openres *o_res = &data->o_res;
1592 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001593 struct rpc_message msg = {
1594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1595 .rpc_argp = o_arg,
1596 .rpc_resp = o_res,
1597 .rpc_cred = data->owner->so_cred,
1598 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001599 struct rpc_task_setup task_setup_data = {
1600 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001601 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001602 .callback_ops = &nfs4_open_ops,
1603 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001604 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001605 .flags = RPC_TASK_ASYNC,
1606 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001607 int status;
1608
Trond Myklebust9d12b212012-01-17 22:04:25 -05001609 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001610 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001611 data->rpc_done = 0;
1612 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001613 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001614 if (isrecover)
1615 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001616 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001617 if (IS_ERR(task))
1618 return PTR_ERR(task);
1619 status = nfs4_wait_for_completion_rpc_task(task);
1620 if (status != 0) {
1621 data->cancelled = 1;
1622 smp_wmb();
1623 } else
1624 status = data->rpc_status;
1625 rpc_put_task(task);
1626
1627 return status;
1628}
1629
1630static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1631{
1632 struct inode *dir = data->dir->d_inode;
1633 struct nfs_openres *o_res = &data->o_res;
1634 int status;
1635
1636 status = nfs4_run_open_task(data, 1);
1637 if (status != 0 || !data->rpc_done)
1638 return status;
1639
Trond Myklebust6926afd2012-01-07 13:22:46 -05001640 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1641
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001642 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1643 status = _nfs4_proc_open_confirm(data);
1644 if (status != 0)
1645 return status;
1646 }
1647
1648 return status;
1649}
1650
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001651static int nfs4_opendata_access(struct rpc_cred *cred,
1652 struct nfs4_opendata *opendata,
1653 struct nfs4_state *state, fmode_t fmode)
1654{
1655 struct nfs_access_entry cache;
1656 u32 mask;
1657
1658 /* access call failed or for some reason the server doesn't
1659 * support any access modes -- defer access call until later */
1660 if (opendata->o_res.access_supported == 0)
1661 return 0;
1662
1663 mask = 0;
1664 if (fmode & FMODE_READ)
1665 mask |= MAY_READ;
1666 if (fmode & FMODE_WRITE)
1667 mask |= MAY_WRITE;
1668 if (fmode & FMODE_EXEC)
1669 mask |= MAY_EXEC;
1670
1671 cache.cred = cred;
1672 cache.jiffies = jiffies;
1673 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1674 nfs_access_add_cache(state->inode, &cache);
1675
1676 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1677 return 0;
1678
1679 /* even though OPEN succeeded, access is denied. Close the file */
1680 nfs4_close_state(state, fmode);
1681 return -NFS4ERR_ACCESS;
1682}
1683
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001684/*
1685 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1686 */
1687static int _nfs4_proc_open(struct nfs4_opendata *data)
1688{
1689 struct inode *dir = data->dir->d_inode;
1690 struct nfs_server *server = NFS_SERVER(dir);
1691 struct nfs_openargs *o_arg = &data->o_arg;
1692 struct nfs_openres *o_res = &data->o_res;
1693 int status;
1694
1695 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001696 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001697 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001698 if (status != 0) {
1699 if (status == -NFS4ERR_BADNAME &&
1700 !(o_arg->open_flags & O_CREAT))
1701 return -ENOENT;
1702 return status;
1703 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001704
Trond Myklebust6926afd2012-01-07 13:22:46 -05001705 nfs_fattr_map_and_free_names(server, &data->f_attr);
1706
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001707 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001708 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001709 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1710 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001712 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001714 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001717 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Andy Adamsond83217c2011-03-01 01:34:17 +00001721static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001722{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001723 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001724 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001725
Trond Myklebusta78cb572009-08-09 15:06:19 -04001726 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001727 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001728 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001729 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001730 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1731 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001732 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001733 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001734 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001735 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001736 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001737}
1738
Andy Adamsond83217c2011-03-01 01:34:17 +00001739static int nfs4_recover_expired_lease(struct nfs_server *server)
1740{
1741 return nfs4_client_recover_expired_lease(server->nfs_client);
1742}
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744/*
1745 * OPEN_EXPIRED:
1746 * reclaim state on the server after a network partition.
1747 * Assumes caller holds the appropriate lock
1748 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001749static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001751 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001752 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001754 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1755 if (IS_ERR(opendata))
1756 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001757 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001758 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001759 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001760 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001761 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001764static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001765{
Trond Myklebust539cd032007-06-05 11:46:42 -04001766 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001767 struct nfs4_exception exception = { };
1768 int err;
1769
1770 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001771 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001772 switch (err) {
1773 default:
1774 goto out;
1775 case -NFS4ERR_GRACE:
1776 case -NFS4ERR_DELAY:
1777 nfs4_handle_exception(server, err, &exception);
1778 err = 0;
1779 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001780 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001781out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001782 return err;
1783}
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1786{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001788 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Trond Myklebust864472e2006-01-03 09:55:15 +01001790 ctx = nfs4_state_find_open_context(state);
1791 if (IS_ERR(ctx))
1792 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001793 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001794 put_nfs_open_context(ctx);
1795 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001798#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001799static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001800{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001801 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001802 nfs4_stateid *stateid = &state->stateid;
1803 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001804
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001805 /* If a state reset has been done, test_stateid is unneeded */
1806 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1807 return;
1808
1809 status = nfs41_test_stateid(server, stateid);
1810 if (status != NFS_OK) {
1811 /* Free the stateid unless the server explicitly
1812 * informs us the stateid is unrecognized. */
1813 if (status != -NFS4ERR_BAD_STATEID)
1814 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001815 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001816
Bryan Schumaker69388672012-09-26 15:25:52 -04001817 write_seqlock(&state->seqlock);
1818 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1819 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001820 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1821 }
1822}
1823
1824/**
1825 * nfs41_check_open_stateid - possibly free an open stateid
1826 *
1827 * @state: NFSv4 state for an inode
1828 *
1829 * Returns NFS_OK if recovery for this stateid is now finished.
1830 * Otherwise a negative NFS4ERR value is returned.
1831 */
1832static int nfs41_check_open_stateid(struct nfs4_state *state)
1833{
1834 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001835 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001836 int status;
1837
1838 /* If a state reset has been done, test_stateid is unneeded */
1839 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1840 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1841 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1842 return -NFS4ERR_BAD_STATEID;
1843
1844 status = nfs41_test_stateid(server, stateid);
1845 if (status != NFS_OK) {
1846 /* Free the stateid unless the server explicitly
1847 * informs us the stateid is unrecognized. */
1848 if (status != -NFS4ERR_BAD_STATEID)
1849 nfs41_free_stateid(server, stateid);
1850
1851 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1852 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1853 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001854 }
1855 return status;
1856}
1857
1858static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1859{
Chuck Levereb64cf92012-07-11 16:30:05 -04001860 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001861
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001862 nfs41_clear_delegation_stateid(state);
1863 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001864 if (status != NFS_OK)
1865 status = nfs4_open_expired(sp, state);
1866 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001867}
1868#endif
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001871 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1872 * fields corresponding to attributes that were used to store the verifier.
1873 * Make sure we clobber those fields in the later setattr call
1874 */
1875static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1876{
1877 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1878 !(sattr->ia_valid & ATTR_ATIME_SET))
1879 sattr->ia_valid |= ATTR_ATIME;
1880
1881 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1882 !(sattr->ia_valid & ATTR_MTIME_SET))
1883 sattr->ia_valid |= ATTR_MTIME;
1884}
1885
1886/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001887 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 */
Andy Adamson82be4172012-05-23 05:02:35 -04001889static int _nfs4_do_open(struct inode *dir,
1890 struct dentry *dentry,
1891 fmode_t fmode,
1892 int flags,
1893 struct iattr *sattr,
1894 struct rpc_cred *cred,
1895 struct nfs4_state **res,
1896 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct nfs4_state_owner *sp;
1899 struct nfs4_state *state = NULL;
1900 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001901 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001902 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001906 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1907 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1909 goto out_err;
1910 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001911 status = nfs4_recover_expired_lease(server);
1912 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001913 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001914 if (dentry->d_inode != NULL)
1915 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001916 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001917 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001918 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001919 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Andy Adamson82be4172012-05-23 05:02:35 -04001921 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1922 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1923 if (!opendata->f_attr.mdsthreshold)
1924 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001925 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001926 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001927 if (dentry->d_inode != NULL)
1928 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001929
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001930 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001932 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001934 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001935 status = PTR_ERR(state);
1936 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001937 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001938 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001939 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001940
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001941 status = nfs4_opendata_access(cred, opendata, state, fmode);
1942 if (status != 0)
1943 goto err_opendata_put;
1944
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001945 if (opendata->o_arg.open_flags & O_EXCL) {
1946 nfs4_exclusive_attrset(opendata, sattr);
1947
1948 nfs_fattr_init(opendata->o_res.f_attr);
1949 status = nfs4_do_setattr(state->inode, cred,
1950 opendata->o_res.f_attr, sattr,
1951 state);
1952 if (status == 0)
1953 nfs_setattr_update_inode(state->inode, sattr);
1954 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1955 }
Andy Adamson82be4172012-05-23 05:02:35 -04001956
1957 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1958 *ctx_th = opendata->f_attr.mdsthreshold;
1959 else
1960 kfree(opendata->f_attr.mdsthreshold);
1961 opendata->f_attr.mdsthreshold = NULL;
1962
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001963 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 *res = state;
1966 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001967err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001968 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001969 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001970err_put_state_owner:
1971 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 *res = NULL;
1974 return status;
1975}
1976
1977
Andy Adamson82be4172012-05-23 05:02:35 -04001978static struct nfs4_state *nfs4_do_open(struct inode *dir,
1979 struct dentry *dentry,
1980 fmode_t fmode,
1981 int flags,
1982 struct iattr *sattr,
1983 struct rpc_cred *cred,
1984 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 struct nfs4_exception exception = { };
1987 struct nfs4_state *res;
1988 int status;
1989
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001990 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001992 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1993 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (status == 0)
1995 break;
1996 /* NOTE: BAD_SEQID means the server and client disagree about the
1997 * book-keeping w.r.t. state-changing operations
1998 * (OPEN/CLOSE/LOCK/LOCKU...)
1999 * It is actually a sign of a bug on the client or on the server.
2000 *
2001 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002002 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 * have unhashed the old state_owner for us, and that we can
2004 * therefore safely retry using a new one. We should still warn
2005 * the user though...
2006 */
2007 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002008 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002009 " returned a bad sequence-id error!\n",
2010 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 exception.retry = 1;
2012 continue;
2013 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002014 /*
2015 * BAD_STATEID on OPEN means that the server cancelled our
2016 * state before it received the OPEN_CONFIRM.
2017 * Recover by retrying the request as per the discussion
2018 * on Page 181 of RFC3530.
2019 */
2020 if (status == -NFS4ERR_BAD_STATEID) {
2021 exception.retry = 1;
2022 continue;
2023 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002024 if (status == -EAGAIN) {
2025 /* We must have found a delegation */
2026 exception.retry = 1;
2027 continue;
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2030 status, &exception));
2031 } while (exception.retry);
2032 return res;
2033}
2034
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002035static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2036 struct nfs_fattr *fattr, struct iattr *sattr,
2037 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002039 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002041 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 .iap = sattr,
2043 .server = server,
2044 .bitmask = server->attr_bitmask,
2045 };
2046 struct nfs_setattrres res = {
2047 .fattr = fattr,
2048 .server = server,
2049 };
2050 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2052 .rpc_argp = &arg,
2053 .rpc_resp = &res,
2054 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002056 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002057 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Trond Myklebust0e574af2005-10-27 22:12:38 -04002059 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Trond Myklebust4fc87962012-03-08 17:42:01 -05002061 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002062 struct nfs_lockowner lockowner = {
2063 .l_owner = current->files,
2064 .l_pid = current->tgid,
2065 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002066 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002067 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002068 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2069 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002070 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002071 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002072 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Bryan Schumaker7c513052011-03-24 17:12:24 +00002074 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002075 if (status == 0 && state != NULL)
2076 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002077 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002080static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2081 struct nfs_fattr *fattr, struct iattr *sattr,
2082 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002084 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002085 struct nfs4_exception exception = {
2086 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002087 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002088 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 int err;
2090 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002091 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2092 switch (err) {
2093 case -NFS4ERR_OPENMODE:
2094 if (state && !(state->state & FMODE_WRITE)) {
2095 err = -EBADF;
2096 if (sattr->ia_valid & ATTR_OPEN)
2097 err = -EACCES;
2098 goto out;
2099 }
2100 }
2101 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002103out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return err;
2105}
2106
2107struct nfs4_closedata {
2108 struct inode *inode;
2109 struct nfs4_state *state;
2110 struct nfs_closeargs arg;
2111 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002112 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002113 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002114 bool roc;
2115 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116};
2117
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002118static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002119{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002120 struct nfs4_closedata *calldata = data;
2121 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002122 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002123
Fred Isamanf7e89172011-01-06 11:36:32 +00002124 if (calldata->roc)
2125 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002126 nfs4_put_open_state(calldata->state);
2127 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002128 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002129 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002130 kfree(calldata);
2131}
2132
Trond Myklebust88069f72009-12-08 08:33:16 -05002133static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2134 fmode_t fmode)
2135{
2136 spin_lock(&state->owner->so_lock);
2137 if (!(fmode & FMODE_READ))
2138 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2139 if (!(fmode & FMODE_WRITE))
2140 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2141 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2142 spin_unlock(&state->owner->so_lock);
2143}
2144
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002145static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002147 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 struct nfs_server *server = NFS_SERVER(calldata->inode);
2150
Chuck Levera3ca5652012-03-01 17:00:40 -05002151 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002152 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2153 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /* hmm. we are done with the inode, and in the process of freeing
2155 * the state_owner. we keep this around to process errors
2156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 switch (task->tk_status) {
2158 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002159 if (calldata->roc)
2160 pnfs_roc_set_barrier(state->inode,
2161 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002162 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002163 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002164 nfs4_close_clear_stateid_flags(state,
2165 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 break;
2167 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002168 case -NFS4ERR_OLD_STATEID:
2169 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002171 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002174 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2175 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002177 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002178 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002179 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002182static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002184 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002185 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002186 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002187 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002188
Chuck Levera3ca5652012-03-01 17:00:40 -05002189 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002190 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002191 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002192
Trond Myklebust88069f72009-12-08 08:33:16 -05002193 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2194 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002195 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002196 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002197 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002198 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002199 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2200 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2201 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002202 }
2203 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002204 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2205 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2206 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002207 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002208 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002209 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002210
2211 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002212 /* Note: exit _without_ calling nfs4_close_done */
2213 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002214 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002215 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002216
Fred Isamanf7e89172011-01-06 11:36:32 +00002217 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002218 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002219 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002220 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002221 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002222 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002223
Trond Myklebust516a6af2005-10-27 22:12:41 -04002224 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002225 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002226 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002227 &calldata->arg.seq_args,
2228 &calldata->res.seq_res,
2229 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002230 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002231 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002232out:
2233 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002236static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002237 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002238 .rpc_call_done = nfs4_close_done,
2239 .rpc_release = nfs4_free_closedata,
2240};
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242/*
2243 * It is possible for data to be read/written from a mem-mapped file
2244 * after the sys_close call (which hits the vfs layer as a flush).
2245 * This means that we can't safely call nfsv4 close on a file until
2246 * the inode is cleared. This in turn means that we are not good
2247 * NFSv4 citizens - we do not indicate to the server to update the file's
2248 * share state even when we are done with one of the three share
2249 * stateid's in the inode.
2250 *
2251 * NOTE: Caller must be holding the sp->so_owner semaphore!
2252 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002253int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002255 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002257 struct nfs4_state_owner *sp = state->owner;
2258 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002259 struct rpc_message msg = {
2260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2261 .rpc_cred = state->owner->so_cred,
2262 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002263 struct rpc_task_setup task_setup_data = {
2264 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002265 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002266 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002267 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002268 .flags = RPC_TASK_ASYNC,
2269 };
Trond Myklebust95121352005-10-18 14:20:12 -07002270 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002272 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002274 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002275 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002276 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002278 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002279 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002281 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002282 if (calldata->arg.seqid == NULL)
2283 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002284 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002285 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002286 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002287 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002288 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002289 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002290 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002291
Trond Myklebust1174dd12010-12-21 10:52:24 -05002292 msg.rpc_argp = &calldata->arg;
2293 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002294 task_setup_data.callback_data = calldata;
2295 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002296 if (IS_ERR(task))
2297 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002298 status = 0;
2299 if (wait)
2300 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002301 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002302 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002303out_free_calldata:
2304 kfree(calldata);
2305out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002306 nfs4_put_open_state(state);
2307 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002308 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Trond Myklebust2b484292010-09-17 10:56:51 -04002311static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002312nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 struct nfs4_state *state;
2315
Trond Myklebust565277f2007-10-15 18:17:53 -04002316 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002317 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2318 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002319 if (IS_ERR(state))
2320 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002321 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002322 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Trond Myklebust1185a552009-12-03 15:54:02 -05002325static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002326{
2327 if (ctx->state == NULL)
2328 return;
2329 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002330 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002331 else
Al Viro643168c2011-06-22 18:20:23 -04002332 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002333}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2336{
Benny Halevy43652ad2009-04-01 09:21:54 -04002337 struct nfs4_server_caps_arg args = {
2338 .fhandle = fhandle,
2339 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 struct nfs4_server_caps_res res = {};
2341 struct rpc_message msg = {
2342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002343 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 .rpc_resp = &res,
2345 };
2346 int status;
2347
Bryan Schumaker7c513052011-03-24 17:12:24 +00002348 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (status == 0) {
2350 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002351 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2352 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2353 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2354 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2355 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2357 server->caps |= NFS_CAP_ACLS;
2358 if (res.has_links != 0)
2359 server->caps |= NFS_CAP_HARDLINKS;
2360 if (res.has_symlinks != 0)
2361 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002362 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2363 server->caps |= NFS_CAP_FILEID;
2364 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2365 server->caps |= NFS_CAP_MODE;
2366 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2367 server->caps |= NFS_CAP_NLINK;
2368 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2369 server->caps |= NFS_CAP_OWNER;
2370 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2371 server->caps |= NFS_CAP_OWNER_GROUP;
2372 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2373 server->caps |= NFS_CAP_ATIME;
2374 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2375 server->caps |= NFS_CAP_CTIME;
2376 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2377 server->caps |= NFS_CAP_MTIME;
2378
Trond Myklebusta65318b2009-03-11 14:10:28 -04002379 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2380 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2381 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002383 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return status;
2387}
2388
Trond Myklebust55a97592006-06-09 09:34:19 -04002389int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
2391 struct nfs4_exception exception = { };
2392 int err;
2393 do {
2394 err = nfs4_handle_exception(server,
2395 _nfs4_server_capabilities(server, fhandle),
2396 &exception);
2397 } while (exception.retry);
2398 return err;
2399}
2400
2401static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2402 struct nfs_fsinfo *info)
2403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 struct nfs4_lookup_root_arg args = {
2405 .bitmask = nfs4_fattr_bitmap,
2406 };
2407 struct nfs4_lookup_res res = {
2408 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002409 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 .fh = fhandle,
2411 };
2412 struct rpc_message msg = {
2413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2414 .rpc_argp = &args,
2415 .rpc_resp = &res,
2416 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002417
Trond Myklebust0e574af2005-10-27 22:12:38 -04002418 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002419 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
2422static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2423 struct nfs_fsinfo *info)
2424{
2425 struct nfs4_exception exception = { };
2426 int err;
2427 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002428 err = _nfs4_lookup_root(server, fhandle, info);
2429 switch (err) {
2430 case 0:
2431 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002432 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002433 default:
2434 err = nfs4_handle_exception(server, err, &exception);
2435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002437out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return err;
2439}
2440
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002441static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2442 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2443{
2444 struct rpc_auth *auth;
2445 int ret;
2446
2447 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002448 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002449 ret = -EIO;
2450 goto out;
2451 }
2452 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002453out:
2454 return ret;
2455}
2456
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002457static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002458 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002460 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002461 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002463 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2464 BUG_ON(len < 0);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002465
2466 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002467 /* AUTH_UNIX is the default flavor if none was specified,
2468 * thus has already been tried. */
2469 if (flav_array[i] == RPC_AUTH_UNIX)
2470 continue;
2471
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002472 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002473 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002474 continue;
2475 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002476 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002477 /*
2478 * -EACCESS could mean that the user doesn't have correct permissions
2479 * to access the mount. It could also mean that we tried to mount
2480 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2481 * existing mount programs don't handle -EACCES very well so it should
2482 * be mapped to -EPERM instead.
2483 */
2484 if (status == -EACCES)
2485 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002486 return status;
2487}
2488
2489/*
2490 * get the file handle for the "/" directory on the server
2491 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002492int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2493 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002494{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002495 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002496 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002497 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2498 /*
2499 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2500 * by nfs4_map_errors() as this function exits.
2501 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002502 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (status == 0)
2504 status = nfs4_server_capabilities(server, fhandle);
2505 if (status == 0)
2506 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002507 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
Bryan Schumakerbae36242012-05-10 15:07:31 -04002510static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2511 struct nfs_fsinfo *info)
2512{
2513 int error;
2514 struct nfs_fattr *fattr = info->fattr;
2515
2516 error = nfs4_server_capabilities(server, mntfh);
2517 if (error < 0) {
2518 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2519 return error;
2520 }
2521
2522 error = nfs4_proc_getattr(server, mntfh, fattr);
2523 if (error < 0) {
2524 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2525 return error;
2526 }
2527
2528 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2529 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2530 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2531
2532 return error;
2533}
2534
Manoj Naik6b97fd32006-06-09 09:34:29 -04002535/*
2536 * Get locations and (maybe) other attributes of a referral.
2537 * Note that we'll actually follow the referral later when
2538 * we detect fsid mismatch in inode revalidation
2539 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002540static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2541 const struct qstr *name, struct nfs_fattr *fattr,
2542 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002543{
2544 int status = -ENOMEM;
2545 struct page *page = NULL;
2546 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002547
2548 page = alloc_page(GFP_KERNEL);
2549 if (page == NULL)
2550 goto out;
2551 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2552 if (locations == NULL)
2553 goto out;
2554
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002555 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002556 if (status != 0)
2557 goto out;
2558 /* Make sure server returned a different fsid for the referral */
2559 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002560 dprintk("%s: server did not return a different fsid for"
2561 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002562 status = -EIO;
2563 goto out;
2564 }
Andy Adamson533eb462011-06-13 18:25:56 -04002565 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2566 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002567
Andy Adamson533eb462011-06-13 18:25:56 -04002568 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002569 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002570 memset(fhandle, 0, sizeof(struct nfs_fh));
2571out:
2572 if (page)
2573 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002574 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002575 return status;
2576}
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2579{
2580 struct nfs4_getattr_arg args = {
2581 .fh = fhandle,
2582 .bitmask = server->attr_bitmask,
2583 };
2584 struct nfs4_getattr_res res = {
2585 .fattr = fattr,
2586 .server = server,
2587 };
2588 struct rpc_message msg = {
2589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2590 .rpc_argp = &args,
2591 .rpc_resp = &res,
2592 };
2593
Trond Myklebust0e574af2005-10-27 22:12:38 -04002594 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002595 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
2598static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2599{
2600 struct nfs4_exception exception = { };
2601 int err;
2602 do {
2603 err = nfs4_handle_exception(server,
2604 _nfs4_proc_getattr(server, fhandle, fattr),
2605 &exception);
2606 } while (exception.retry);
2607 return err;
2608}
2609
2610/*
2611 * The file is not closed if it is opened due to the a request to change
2612 * the size of the file. The open call will not be needed once the
2613 * VFS layer lookup-intents are implemented.
2614 *
2615 * Close is called when the inode is destroyed.
2616 * If we haven't opened the file for O_WRONLY, we
2617 * need to in the size_change case to obtain a stateid.
2618 *
2619 * Got race?
2620 * Because OPEN is always done by name in nfsv4, it is
2621 * possible that we opened a different file by the same
2622 * name. We can recognize this race condition, but we
2623 * can't do anything about it besides returning an error.
2624 *
2625 * This will be fixed with VFS changes (lookup-intent).
2626 */
2627static int
2628nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2629 struct iattr *sattr)
2630{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002631 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002632 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002633 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 int status;
2635
Benny Halevy8a1636c2010-07-14 15:43:57 -04002636 if (pnfs_ld_layoutret_on_setattr(inode))
2637 pnfs_return_layout(inode);
2638
Trond Myklebust0e574af2005-10-27 22:12:38 -04002639 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Andy Adamson26699402012-05-30 16:12:24 -04002641 /* Deal with open(O_TRUNC) */
2642 if (sattr->ia_valid & ATTR_OPEN)
2643 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2644
2645 /* Optimization: if the end result is no change, don't RPC */
2646 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2647 return 0;
2648
Trond Myklebustd5308382005-11-04 15:33:38 -05002649 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002650 if (sattr->ia_valid & ATTR_FILE) {
2651 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002652
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002653 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002654 if (ctx) {
2655 cred = ctx->cred;
2656 state = ctx->state;
2657 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002658 }
2659
2660 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002661 if (status == 0)
2662 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return status;
2664}
2665
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002666static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2667 const struct qstr *name, struct nfs_fh *fhandle,
2668 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002669{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002670 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002671 int status;
2672 struct nfs4_lookup_arg args = {
2673 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002674 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002675 .name = name,
2676 };
2677 struct nfs4_lookup_res res = {
2678 .server = server,
2679 .fattr = fattr,
2680 .fh = fhandle,
2681 };
2682 struct rpc_message msg = {
2683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2684 .rpc_argp = &args,
2685 .rpc_resp = &res,
2686 };
2687
2688 nfs_fattr_init(fattr);
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002691 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 dprintk("NFS reply lookup: %d\n", status);
2693 return status;
2694}
2695
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002696static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002697{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002698 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002699 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002700 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2701 fattr->nlink = 2;
2702}
2703
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002704static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2705 struct qstr *name, struct nfs_fh *fhandle,
2706 struct nfs_fattr *fattr)
2707{
2708 struct nfs4_exception exception = { };
2709 struct rpc_clnt *client = *clnt;
2710 int err;
2711 do {
2712 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2713 switch (err) {
2714 case -NFS4ERR_BADNAME:
2715 err = -ENOENT;
2716 goto out;
2717 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002718 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002719 goto out;
2720 case -NFS4ERR_WRONGSEC:
2721 err = -EPERM;
2722 if (client != *clnt)
2723 goto out;
2724
2725 client = nfs4_create_sec_client(client, dir, name);
2726 if (IS_ERR(client))
2727 return PTR_ERR(client);
2728
2729 exception.retry = 1;
2730 break;
2731 default:
2732 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2733 }
2734 } while (exception.retry);
2735
2736out:
2737 if (err == 0)
2738 *clnt = client;
2739 else if (client != *clnt)
2740 rpc_shutdown_client(client);
2741
2742 return err;
2743}
2744
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002745static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002746 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002748 int status;
2749 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002750
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002751 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2752 if (client != NFS_CLIENT(dir)) {
2753 rpc_shutdown_client(client);
2754 nfs_fixup_secinfo_attributes(fattr);
2755 }
2756 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002759struct rpc_clnt *
2760nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2761 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2762{
2763 int status;
2764 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2765
2766 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2767 if (status < 0) {
2768 rpc_shutdown_client(client);
2769 return ERR_PTR(status);
2770 }
2771 return client;
2772}
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2775{
Trond Myklebust76b32992007-08-10 17:45:11 -04002776 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 struct nfs4_accessargs args = {
2778 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002779 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002781 struct nfs4_accessres res = {
2782 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002783 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 struct rpc_message msg = {
2785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2786 .rpc_argp = &args,
2787 .rpc_resp = &res,
2788 .rpc_cred = entry->cred,
2789 };
2790 int mode = entry->mask;
2791 int status;
2792
2793 /*
2794 * Determine which access bits we want to ask for...
2795 */
2796 if (mode & MAY_READ)
2797 args.access |= NFS4_ACCESS_READ;
2798 if (S_ISDIR(inode->i_mode)) {
2799 if (mode & MAY_WRITE)
2800 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2801 if (mode & MAY_EXEC)
2802 args.access |= NFS4_ACCESS_LOOKUP;
2803 } else {
2804 if (mode & MAY_WRITE)
2805 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2806 if (mode & MAY_EXEC)
2807 args.access |= NFS4_ACCESS_EXECUTE;
2808 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002809
2810 res.fattr = nfs_alloc_fattr();
2811 if (res.fattr == NULL)
2812 return -ENOMEM;
2813
Bryan Schumaker7c513052011-03-24 17:12:24 +00002814 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002816 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002817 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002819 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return status;
2821}
2822
2823static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2824{
2825 struct nfs4_exception exception = { };
2826 int err;
2827 do {
2828 err = nfs4_handle_exception(NFS_SERVER(inode),
2829 _nfs4_proc_access(inode, entry),
2830 &exception);
2831 } while (exception.retry);
2832 return err;
2833}
2834
2835/*
2836 * TODO: For the time being, we don't try to get any attributes
2837 * along with any of the zero-copy operations READ, READDIR,
2838 * READLINK, WRITE.
2839 *
2840 * In the case of the first three, we want to put the GETATTR
2841 * after the read-type operation -- this is because it is hard
2842 * to predict the length of a GETATTR response in v4, and thus
2843 * align the READ data correctly. This means that the GETATTR
2844 * may end up partially falling into the page cache, and we should
2845 * shift it into the 'tail' of the xdr_buf before processing.
2846 * To do this efficiently, we need to know the total length
2847 * of data received, which doesn't seem to be available outside
2848 * of the RPC layer.
2849 *
2850 * In the case of WRITE, we also want to put the GETATTR after
2851 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002852 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 *
2854 * Both of these changes to the XDR layer would in fact be quite
2855 * minor, but I decided to leave them for a subsequent patch.
2856 */
2857static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2858 unsigned int pgbase, unsigned int pglen)
2859{
2860 struct nfs4_readlink args = {
2861 .fh = NFS_FH(inode),
2862 .pgbase = pgbase,
2863 .pglen = pglen,
2864 .pages = &page,
2865 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002866 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 struct rpc_message msg = {
2868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2869 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002870 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 };
2872
Bryan Schumaker7c513052011-03-24 17:12:24 +00002873 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874}
2875
2876static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2877 unsigned int pgbase, unsigned int pglen)
2878{
2879 struct nfs4_exception exception = { };
2880 int err;
2881 do {
2882 err = nfs4_handle_exception(NFS_SERVER(inode),
2883 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2884 &exception);
2885 } while (exception.retry);
2886 return err;
2887}
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002890 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892static int
2893nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002894 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002896 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 int status = 0;
2899
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002900 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2901 if (IS_ERR(ctx))
2902 return PTR_ERR(ctx);
2903
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002904 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002905 state = nfs4_do_open(dir, dentry, ctx->mode,
2906 flags, sattr, ctx->cred,
2907 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002908 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (IS_ERR(state)) {
2910 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002911 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002913 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002914 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002915 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002917 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return status;
2919}
2920
2921static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2922{
Trond Myklebust16e42952005-10-27 22:12:44 -04002923 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002924 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002926 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002928 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002929 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002930 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2933 .rpc_argp = &args,
2934 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002936 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002937
Bryan Schumaker7c513052011-03-24 17:12:24 +00002938 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002939 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002940 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return status;
2942}
2943
2944static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2945{
2946 struct nfs4_exception exception = { };
2947 int err;
2948 do {
2949 err = nfs4_handle_exception(NFS_SERVER(dir),
2950 _nfs4_proc_remove(dir, name),
2951 &exception);
2952 } while (exception.retry);
2953 return err;
2954}
2955
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002956static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002958 struct nfs_server *server = NFS_SERVER(dir);
2959 struct nfs_removeargs *args = msg->rpc_argp;
2960 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002962 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002964 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002967static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2968{
2969 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2970 &data->args.seq_args,
2971 &data->res.seq_res,
2972 task))
2973 return;
2974 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975}
2976
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002977static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002979 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2980
Trond Myklebust14516c32010-07-31 14:29:06 -04002981 if (!nfs4_sequence_done(task, &res->seq_res))
2982 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002983 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002984 return 0;
2985 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002986 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
2988
Jeff Laytond3d41522010-09-17 17:31:57 -04002989static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2990{
2991 struct nfs_server *server = NFS_SERVER(dir);
2992 struct nfs_renameargs *arg = msg->rpc_argp;
2993 struct nfs_renameres *res = msg->rpc_resp;
2994
2995 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002996 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002997 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002998}
2999
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003000static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3001{
3002 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3003 &data->args.seq_args,
3004 &data->res.seq_res,
3005 task))
3006 return;
3007 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003008}
3009
3010static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3011 struct inode *new_dir)
3012{
3013 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3014
3015 if (!nfs4_sequence_done(task, &res->seq_res))
3016 return 0;
3017 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3018 return 0;
3019
3020 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003021 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003022 return 1;
3023}
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3026 struct inode *new_dir, struct qstr *new_name)
3027{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003028 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003029 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 .old_dir = NFS_FH(old_dir),
3031 .new_dir = NFS_FH(new_dir),
3032 .old_name = old_name,
3033 .new_name = new_name,
3034 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003035 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003036 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003037 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 struct rpc_message msg = {
3039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3040 .rpc_argp = &arg,
3041 .rpc_resp = &res,
3042 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003043 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Bryan Schumaker7c513052011-03-24 17:12:24 +00003045 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 if (!status) {
3047 update_changeattr(old_dir, &res.old_cinfo);
3048 update_changeattr(new_dir, &res.new_cinfo);
3049 }
3050 return status;
3051}
3052
3053static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3054 struct inode *new_dir, struct qstr *new_name)
3055{
3056 struct nfs4_exception exception = { };
3057 int err;
3058 do {
3059 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3060 _nfs4_proc_rename(old_dir, old_name,
3061 new_dir, new_name),
3062 &exception);
3063 } while (exception.retry);
3064 return err;
3065}
3066
3067static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3068{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003069 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 struct nfs4_link_arg arg = {
3071 .fh = NFS_FH(inode),
3072 .dir_fh = NFS_FH(dir),
3073 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003074 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003076 struct nfs4_link_res res = {
3077 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003078 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 struct rpc_message msg = {
3080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3081 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003082 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003084 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Trond Myklebust136f2622010-04-16 16:22:49 -04003086 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003087 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003088 goto out;
3089
Bryan Schumaker7c513052011-03-24 17:12:24 +00003090 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003091 if (!status) {
3092 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003093 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003094 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003095out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003096 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return status;
3098}
3099
3100static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3101{
3102 struct nfs4_exception exception = { };
3103 int err;
3104 do {
3105 err = nfs4_handle_exception(NFS_SERVER(inode),
3106 _nfs4_proc_link(inode, dir, name),
3107 &exception);
3108 } while (exception.retry);
3109 return err;
3110}
3111
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112struct nfs4_createdata {
3113 struct rpc_message msg;
3114 struct nfs4_create_arg arg;
3115 struct nfs4_create_res res;
3116 struct nfs_fh fh;
3117 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003118};
3119
3120static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3121 struct qstr *name, struct iattr *sattr, u32 ftype)
3122{
3123 struct nfs4_createdata *data;
3124
3125 data = kzalloc(sizeof(*data), GFP_KERNEL);
3126 if (data != NULL) {
3127 struct nfs_server *server = NFS_SERVER(dir);
3128
3129 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3130 data->msg.rpc_argp = &data->arg;
3131 data->msg.rpc_resp = &data->res;
3132 data->arg.dir_fh = NFS_FH(dir);
3133 data->arg.server = server;
3134 data->arg.name = name;
3135 data->arg.attrs = sattr;
3136 data->arg.ftype = ftype;
3137 data->arg.bitmask = server->attr_bitmask;
3138 data->res.server = server;
3139 data->res.fh = &data->fh;
3140 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003141 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003142 }
3143 return data;
3144}
3145
3146static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3147{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003148 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003149 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003150 if (status == 0) {
3151 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003152 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3153 }
3154 return status;
3155}
3156
3157static void nfs4_free_createdata(struct nfs4_createdata *data)
3158{
3159 kfree(data);
3160}
3161
Chuck Lever4f390c12006-08-22 20:06:22 -04003162static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003163 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003165 struct nfs4_createdata *data;
3166 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Chuck Lever94a6d752006-08-22 20:06:23 -04003168 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003169 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003170
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003171 status = -ENOMEM;
3172 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3173 if (data == NULL)
3174 goto out;
3175
3176 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3177 data->arg.u.symlink.pages = &page;
3178 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003180 status = nfs4_do_create(dir, dentry, data);
3181
3182 nfs4_free_createdata(data);
3183out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return status;
3185}
3186
Chuck Lever4f390c12006-08-22 20:06:22 -04003187static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003188 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
3190 struct nfs4_exception exception = { };
3191 int err;
3192 do {
3193 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003194 _nfs4_proc_symlink(dir, dentry, page,
3195 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 &exception);
3197 } while (exception.retry);
3198 return err;
3199}
3200
3201static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3202 struct iattr *sattr)
3203{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003204 struct nfs4_createdata *data;
3205 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003207 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3208 if (data == NULL)
3209 goto out;
3210
3211 status = nfs4_do_create(dir, dentry, data);
3212
3213 nfs4_free_createdata(data);
3214out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 return status;
3216}
3217
3218static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3219 struct iattr *sattr)
3220{
3221 struct nfs4_exception exception = { };
3222 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003223
3224 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 do {
3226 err = nfs4_handle_exception(NFS_SERVER(dir),
3227 _nfs4_proc_mkdir(dir, dentry, sattr),
3228 &exception);
3229 } while (exception.retry);
3230 return err;
3231}
3232
3233static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003234 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
3236 struct inode *dir = dentry->d_inode;
3237 struct nfs4_readdir_arg args = {
3238 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003239 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 .pgbase = 0,
3241 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003242 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003243 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 };
3245 struct nfs4_readdir_res res;
3246 struct rpc_message msg = {
3247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3248 .rpc_argp = &args,
3249 .rpc_resp = &res,
3250 .rpc_cred = cred,
3251 };
3252 int status;
3253
Harvey Harrison3110ff82008-05-02 13:42:44 -07003254 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003255 dentry->d_parent->d_name.name,
3256 dentry->d_name.name,
3257 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003258 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003260 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05003261 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003262 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003263 status += args.pgbase;
3264 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003265
3266 nfs_invalidate_atime(dir);
3267
Harvey Harrison3110ff82008-05-02 13:42:44 -07003268 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return status;
3270}
3271
3272static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003273 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
3275 struct nfs4_exception exception = { };
3276 int err;
3277 do {
3278 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3279 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003280 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 &exception);
3282 } while (exception.retry);
3283 return err;
3284}
3285
3286static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3287 struct iattr *sattr, dev_t rdev)
3288{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003289 struct nfs4_createdata *data;
3290 int mode = sattr->ia_mode;
3291 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
3293 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3294 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003295
3296 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3297 if (data == NULL)
3298 goto out;
3299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003301 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003303 data->arg.ftype = NF4BLK;
3304 data->arg.u.device.specdata1 = MAJOR(rdev);
3305 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
3307 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003308 data->arg.ftype = NF4CHR;
3309 data->arg.u.device.specdata1 = MAJOR(rdev);
3310 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003313 status = nfs4_do_create(dir, dentry, data);
3314
3315 nfs4_free_createdata(data);
3316out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 return status;
3318}
3319
3320static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3321 struct iattr *sattr, dev_t rdev)
3322{
3323 struct nfs4_exception exception = { };
3324 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003325
3326 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 do {
3328 err = nfs4_handle_exception(NFS_SERVER(dir),
3329 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3330 &exception);
3331 } while (exception.retry);
3332 return err;
3333}
3334
3335static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3336 struct nfs_fsstat *fsstat)
3337{
3338 struct nfs4_statfs_arg args = {
3339 .fh = fhandle,
3340 .bitmask = server->attr_bitmask,
3341 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003342 struct nfs4_statfs_res res = {
3343 .fsstat = fsstat,
3344 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 struct rpc_message msg = {
3346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3347 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003348 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 };
3350
Trond Myklebust0e574af2005-10-27 22:12:38 -04003351 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003352 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
3355static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3356{
3357 struct nfs4_exception exception = { };
3358 int err;
3359 do {
3360 err = nfs4_handle_exception(server,
3361 _nfs4_proc_statfs(server, fhandle, fsstat),
3362 &exception);
3363 } while (exception.retry);
3364 return err;
3365}
3366
3367static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3368 struct nfs_fsinfo *fsinfo)
3369{
3370 struct nfs4_fsinfo_arg args = {
3371 .fh = fhandle,
3372 .bitmask = server->attr_bitmask,
3373 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003374 struct nfs4_fsinfo_res res = {
3375 .fsinfo = fsinfo,
3376 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 struct rpc_message msg = {
3378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3379 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003380 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 };
3382
Bryan Schumaker7c513052011-03-24 17:12:24 +00003383 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384}
3385
3386static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3387{
3388 struct nfs4_exception exception = { };
3389 int err;
3390
3391 do {
3392 err = nfs4_handle_exception(server,
3393 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3394 &exception);
3395 } while (exception.retry);
3396 return err;
3397}
3398
3399static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3400{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003401 int error;
3402
Trond Myklebust0e574af2005-10-27 22:12:38 -04003403 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003404 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003405 if (error == 0) {
3406 /* block layout checks this! */
3407 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003408 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003409 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003410
3411 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412}
3413
3414static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3415 struct nfs_pathconf *pathconf)
3416{
3417 struct nfs4_pathconf_arg args = {
3418 .fh = fhandle,
3419 .bitmask = server->attr_bitmask,
3420 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003421 struct nfs4_pathconf_res res = {
3422 .pathconf = pathconf,
3423 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 struct rpc_message msg = {
3425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3426 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003427 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 };
3429
3430 /* None of the pathconf attributes are mandatory to implement */
3431 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3432 memset(pathconf, 0, sizeof(*pathconf));
3433 return 0;
3434 }
3435
Trond Myklebust0e574af2005-10-27 22:12:38 -04003436 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003437 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
3440static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3441 struct nfs_pathconf *pathconf)
3442{
3443 struct nfs4_exception exception = { };
3444 int err;
3445
3446 do {
3447 err = nfs4_handle_exception(server,
3448 _nfs4_proc_pathconf(server, fhandle, pathconf),
3449 &exception);
3450 } while (exception.retry);
3451 return err;
3452}
3453
Benny Halevyd20581a2011-05-22 19:52:03 +03003454void __nfs4_read_done_cb(struct nfs_read_data *data)
3455{
Fred Isamancd841602012-04-20 14:47:44 -04003456 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003457}
3458
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003459static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
Fred Isamancd841602012-04-20 14:47:44 -04003461 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003463 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003464 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003465 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003467
Benny Halevyd20581a2011-05-22 19:52:03 +03003468 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003470 renew_lease(server, data->timestamp);
3471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472}
3473
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003474static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3475{
3476
3477 dprintk("--> %s\n", __func__);
3478
3479 if (!nfs4_sequence_done(task, &data->res.seq_res))
3480 return -EAGAIN;
3481
Benny Halevyd20581a2011-05-22 19:52:03 +03003482 return data->read_done_cb ? data->read_done_cb(task, data) :
3483 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003484}
3485
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003486static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003489 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003490 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003491 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003494static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3495{
Fred Isamancd841602012-04-20 14:47:44 -04003496 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003497 &data->args.seq_args,
3498 &data->res.seq_res,
3499 task))
3500 return;
3501 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
Fred Isamanb029bc92011-03-03 15:13:42 +00003504static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505{
Fred Isamancd841602012-04-20 14:47:44 -04003506 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003508 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003509 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003510 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003512 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003514 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003515 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
Fred Isamanb029bc92011-03-03 15:13:42 +00003519static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3520{
3521 if (!nfs4_sequence_done(task, &data->res.seq_res))
3522 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003523 return data->write_done_cb ? data->write_done_cb(task, data) :
3524 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003525}
3526
Trond Myklebust5a37f852012-04-28 14:55:16 -04003527static
3528bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003529{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003530 const struct nfs_pgio_header *hdr = data->header;
3531
3532 /* Don't request attributes for pNFS or O_DIRECT writes */
3533 if (data->ds_clp != NULL || hdr->dreq != NULL)
3534 return false;
3535 /* Otherwise, request attributes if and only if we don't hold
3536 * a delegation
3537 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003538 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003539}
Fred Isamana69aef12011-03-03 15:13:47 +00003540
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003541static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
Fred Isamancd841602012-04-20 14:47:44 -04003543 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003544
Trond Myklebust5a37f852012-04-28 14:55:16 -04003545 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003546 data->args.bitmask = NULL;
3547 data->res.fattr = NULL;
3548 } else
3549 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003550
Fred Isamanb029bc92011-03-03 15:13:42 +00003551 if (!data->write_done_cb)
3552 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003553 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 data->timestamp = jiffies;
3555
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003556 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003557 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558}
3559
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003560static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3561{
Fred Isamancd841602012-04-20 14:47:44 -04003562 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003563 &data->args.seq_args,
3564 &data->res.seq_res,
3565 task))
3566 return;
3567 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Fred Isaman0b7c0152012-04-20 14:47:39 -04003570static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3571{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3573 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003574 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 return;
3577 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003578}
3579
Fred Isaman0b7c0152012-04-20 14:47:39 -04003580static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
3582 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003583
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003584 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003585 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003586 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Fred Isaman0b7c0152012-04-20 14:47:39 -04003591static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003592{
3593 if (!nfs4_sequence_done(task, &data->res.seq_res))
3594 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003595 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003596}
3597
Fred Isaman0b7c0152012-04-20 14:47:39 -04003598static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003600 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003601
Fred Isaman0b7c0152012-04-20 14:47:39 -04003602 if (data->commit_done_cb == NULL)
3603 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003604 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003605 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003606 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
3608
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003609struct nfs4_renewdata {
3610 struct nfs_client *client;
3611 unsigned long timestamp;
3612};
3613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614/*
3615 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3616 * standalone procedure for queueing an asynchronous RENEW.
3617 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003618static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003619{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003620 struct nfs4_renewdata *data = calldata;
3621 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003622
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003623 if (atomic_read(&clp->cl_count) > 1)
3624 nfs4_schedule_state_renewal(clp);
3625 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003626 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003627}
3628
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003629static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003631 struct nfs4_renewdata *data = calldata;
3632 struct nfs_client *clp = data->client;
3633 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
3635 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003636 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003637 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3638 return;
3639 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003640 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003641 return;
3642 }
3643 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003645 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646}
3647
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003648static const struct rpc_call_ops nfs4_renew_ops = {
3649 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003650 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003651};
3652
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003653static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
3655 struct rpc_message msg = {
3656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3657 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003658 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003660 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003662 if (renew_flags == 0)
3663 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003664 if (!atomic_inc_not_zero(&clp->cl_count))
3665 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003666 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003667 if (data == NULL)
3668 return -ENOMEM;
3669 data->client = clp;
3670 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003672 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673}
3674
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003675static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
3677 struct rpc_message msg = {
3678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3679 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003680 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 };
3682 unsigned long now = jiffies;
3683 int status;
3684
3685 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3686 if (status < 0)
3687 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003688 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return 0;
3690}
3691
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003692static inline int nfs4_server_supports_acls(struct nfs_server *server)
3693{
3694 return (server->caps & NFS_CAP_ACLS)
3695 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3696 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3697}
3698
Trond Myklebust21f498c2012-08-24 10:59:25 -04003699/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3700 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003701 * the stack.
3702 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003703#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003704
Neil Hormane9e3d722011-03-04 19:26:03 -05003705static int buf_to_pages_noslab(const void *buf, size_t buflen,
3706 struct page **pages, unsigned int *pgbase)
3707{
3708 struct page *newpage, **spages;
3709 int rc = 0;
3710 size_t len;
3711 spages = pages;
3712
3713 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003714 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003715 newpage = alloc_page(GFP_KERNEL);
3716
3717 if (newpage == NULL)
3718 goto unwind;
3719 memcpy(page_address(newpage), buf, len);
3720 buf += len;
3721 buflen -= len;
3722 *pages++ = newpage;
3723 rc++;
3724 } while (buflen != 0);
3725
3726 return rc;
3727
3728unwind:
3729 for(; rc > 0; rc--)
3730 __free_page(spages[rc-1]);
3731 return -ENOMEM;
3732}
3733
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003734struct nfs4_cached_acl {
3735 int cached;
3736 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003737 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003738};
3739
3740static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003741{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003742 struct nfs_inode *nfsi = NFS_I(inode);
3743
3744 spin_lock(&inode->i_lock);
3745 kfree(nfsi->nfs4_acl);
3746 nfsi->nfs4_acl = acl;
3747 spin_unlock(&inode->i_lock);
3748}
3749
3750static void nfs4_zap_acl_attr(struct inode *inode)
3751{
3752 nfs4_set_cached_acl(inode, NULL);
3753}
3754
3755static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3756{
3757 struct nfs_inode *nfsi = NFS_I(inode);
3758 struct nfs4_cached_acl *acl;
3759 int ret = -ENOENT;
3760
3761 spin_lock(&inode->i_lock);
3762 acl = nfsi->nfs4_acl;
3763 if (acl == NULL)
3764 goto out;
3765 if (buf == NULL) /* user is just asking for length */
3766 goto out_len;
3767 if (acl->cached == 0)
3768 goto out;
3769 ret = -ERANGE; /* see getxattr(2) man page */
3770 if (acl->len > buflen)
3771 goto out;
3772 memcpy(buf, acl->data, acl->len);
3773out_len:
3774 ret = acl->len;
3775out:
3776 spin_unlock(&inode->i_lock);
3777 return ret;
3778}
3779
Sachin Prabhu5794d212012-04-17 14:36:40 +01003780static 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 +00003781{
3782 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003783 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003784
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003785 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003786 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003787 if (acl == NULL)
3788 goto out;
3789 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003790 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003791 } else {
3792 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3793 if (acl == NULL)
3794 goto out;
3795 acl->cached = 0;
3796 }
3797 acl->len = acl_len;
3798out:
3799 nfs4_set_cached_acl(inode, acl);
3800}
3801
Andy Adamsonbf118a32011-12-07 11:55:27 -05003802/*
3803 * The getxattr API returns the required buffer length when called with a
3804 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3805 * the required buf. On a NULL buf, we send a page of data to the server
3806 * guessing that the ACL request can be serviced by a page. If so, we cache
3807 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3808 * the cache. If not so, we throw away the page, and cache the required
3809 * length. The next getxattr call will then produce another round trip to
3810 * the server, this time with the input buf of the required size.
3811 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003812static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003813{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003814 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003815 struct nfs_getaclargs args = {
3816 .fh = NFS_FH(inode),
3817 .acl_pages = pages,
3818 .acl_len = buflen,
3819 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003820 struct nfs_getaclres res = {
3821 .acl_len = buflen,
3822 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003823 struct rpc_message msg = {
3824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3825 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003826 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003827 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003828 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3829 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003830
Andy Adamsonbf118a32011-12-07 11:55:27 -05003831 /* As long as we're doing a round trip to the server anyway,
3832 * let's be prepared for a page of acl data. */
3833 if (npages == 0)
3834 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003835 if (npages > ARRAY_SIZE(pages))
3836 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003837
Andy Adamsonbf118a32011-12-07 11:55:27 -05003838 for (i = 0; i < npages; i++) {
3839 pages[i] = alloc_page(GFP_KERNEL);
3840 if (!pages[i])
3841 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003842 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003843
3844 /* for decoding across pages */
3845 res.acl_scratch = alloc_page(GFP_KERNEL);
3846 if (!res.acl_scratch)
3847 goto out_free;
3848
Andy Adamsonbf118a32011-12-07 11:55:27 -05003849 args.acl_len = npages * PAGE_SIZE;
3850 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003851
Peng Taode040be2012-01-10 22:42:47 +08003852 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003853 __func__, buf, buflen, npages, args.acl_len);
3854 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3855 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003856 if (ret)
3857 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003858
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003859 /* Handle the case where the passed-in buffer is too short */
3860 if (res.acl_flags & NFS4_ACL_TRUNC) {
3861 /* Did the user only issue a request for the acl length? */
3862 if (buf == NULL)
3863 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003864 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003865 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003866 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003867 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3868 if (buf)
3869 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3870out_ok:
3871 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003872out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003873 for (i = 0; i < npages; i++)
3874 if (pages[i])
3875 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003876 if (res.acl_scratch)
3877 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003878 return ret;
3879}
3880
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003881static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3882{
3883 struct nfs4_exception exception = { };
3884 ssize_t ret;
3885 do {
3886 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3887 if (ret >= 0)
3888 break;
3889 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3890 } while (exception.retry);
3891 return ret;
3892}
3893
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003894static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3895{
3896 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003897 int ret;
3898
3899 if (!nfs4_server_supports_acls(server))
3900 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003901 ret = nfs_revalidate_inode(server, inode);
3902 if (ret < 0)
3903 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003904 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3905 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003906 ret = nfs4_read_cached_acl(inode, buf, buflen);
3907 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003908 /* -ENOENT is returned if there is no ACL or if there is an ACL
3909 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003910 return ret;
3911 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003912}
3913
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003914static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003915{
3916 struct nfs_server *server = NFS_SERVER(inode);
3917 struct page *pages[NFS4ACL_MAXPAGES];
3918 struct nfs_setaclargs arg = {
3919 .fh = NFS_FH(inode),
3920 .acl_pages = pages,
3921 .acl_len = buflen,
3922 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003923 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003924 struct rpc_message msg = {
3925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3926 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003927 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003928 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003929 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05003930 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003931
3932 if (!nfs4_server_supports_acls(server))
3933 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003934 if (npages > ARRAY_SIZE(pages))
3935 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05003936 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3937 if (i < 0)
3938 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04003939 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003940 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003941
3942 /*
3943 * Free each page after tx, so the only ref left is
3944 * held by the network stack
3945 */
3946 for (; i > 0; i--)
3947 put_page(pages[i-1]);
3948
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003949 /*
3950 * Acl update can result in inode attribute update.
3951 * so mark the attribute cache invalid.
3952 */
3953 spin_lock(&inode->i_lock);
3954 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3955 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003956 nfs_access_zap_cache(inode);
3957 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003958 return ret;
3959}
3960
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003961static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3962{
3963 struct nfs4_exception exception = { };
3964 int err;
3965 do {
3966 err = nfs4_handle_exception(NFS_SERVER(inode),
3967 __nfs4_proc_set_acl(inode, buf, buflen),
3968 &exception);
3969 } while (exception.retry);
3970 return err;
3971}
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003974nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003976 struct nfs_client *clp = server->nfs_client;
3977
3978 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 return 0;
3980 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003981 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003982 case -NFS4ERR_ADMIN_REVOKED:
3983 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003984 if (state == NULL)
3985 break;
3986 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003987 case -NFS4ERR_OPENMODE:
3988 if (state == NULL)
3989 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003990 nfs4_schedule_stateid_recovery(server, state);
3991 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003992 case -NFS4ERR_EXPIRED:
3993 if (state != NULL)
3994 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003996 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003997 nfs4_schedule_lease_recovery(clp);
3998 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003999#if defined(CONFIG_NFS_V4_1)
4000 case -NFS4ERR_BADSESSION:
4001 case -NFS4ERR_BADSLOT:
4002 case -NFS4ERR_BAD_HIGH_SLOT:
4003 case -NFS4ERR_DEADSESSION:
4004 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4005 case -NFS4ERR_SEQ_FALSE_RETRY:
4006 case -NFS4ERR_SEQ_MISORDERED:
4007 dprintk("%s ERROR %d, Reset session\n", __func__,
4008 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004009 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004010 task->tk_status = 0;
4011 return -EAGAIN;
4012#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004014 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004015 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004016 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4018 task->tk_status = 0;
4019 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004020 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 case -NFS4ERR_OLD_STATEID:
4022 task->tk_status = 0;
4023 return -EAGAIN;
4024 }
4025 task->tk_status = nfs4_map_errors(task->tk_status);
4026 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004027wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004028 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004029 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4030 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4031 task->tk_status = 0;
4032 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
4034
Chuck Leverf0920752012-05-21 22:45:41 -04004035static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4036 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004037{
4038 __be32 verf[2];
4039
Chuck Lever2c820d92012-05-21 22:45:33 -04004040 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4041 /* An impossible timestamp guarantees this value
4042 * will never match a generated boot time. */
4043 verf[0] = 0;
4044 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4045 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004046 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4047 verf[0] = (__be32)nn->boot_time.tv_sec;
4048 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004049 }
Chuck Levercd937102012-03-02 17:14:31 -05004050 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4051}
4052
Chuck Levere984a552012-09-14 17:24:21 -04004053static unsigned int
4054nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4055 char *buf, size_t len)
4056{
4057 unsigned int result;
4058
4059 rcu_read_lock();
4060 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4061 clp->cl_ipaddr,
4062 rpc_peeraddr2str(clp->cl_rpcclient,
4063 RPC_DISPLAY_ADDR),
4064 rpc_peeraddr2str(clp->cl_rpcclient,
4065 RPC_DISPLAY_PROTO));
4066 rcu_read_unlock();
4067 return result;
4068}
4069
4070static unsigned int
4071nfs4_init_uniform_client_string(const struct nfs_client *clp,
4072 char *buf, size_t len)
4073{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004074 char *nodename = clp->cl_rpcclient->cl_nodename;
4075
4076 if (nfs4_client_id_uniquifier[0] != '\0')
4077 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004078 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4079 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004080 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004081}
4082
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004083/**
4084 * nfs4_proc_setclientid - Negotiate client ID
4085 * @clp: state data structure
4086 * @program: RPC program for NFSv4 callback service
4087 * @port: IP port number for NFS4 callback service
4088 * @cred: RPC credential to use for this call
4089 * @res: where to place the result
4090 *
4091 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4092 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004093int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4094 unsigned short port, struct rpc_cred *cred,
4095 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096{
4097 nfs4_verifier sc_verifier;
4098 struct nfs4_setclientid setclientid = {
4099 .sc_verifier = &sc_verifier,
4100 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004101 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 };
4103 struct rpc_message msg = {
4104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4105 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004106 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004107 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004109 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Chuck Leverde734832012-07-11 16:30:50 -04004111 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004112 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004113 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4114 setclientid.sc_name_len =
4115 nfs4_init_uniform_client_string(clp,
4116 setclientid.sc_name,
4117 sizeof(setclientid.sc_name));
4118 else
4119 setclientid.sc_name_len =
4120 nfs4_init_nonuniform_client_string(clp,
4121 setclientid.sc_name,
4122 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004123 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004124 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004125 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004126 sizeof(setclientid.sc_netid),
4127 rpc_peeraddr2str(clp->cl_rpcclient,
4128 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004129 rcu_read_unlock();
4130 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004131 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 clp->cl_ipaddr, port >> 8, port & 255);
4133
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004134 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4135 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4136 setclientid.sc_name_len, setclientid.sc_name);
4137 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4138 dprintk("NFS reply setclientid: %d\n", status);
4139 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140}
4141
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004142/**
4143 * nfs4_proc_setclientid_confirm - Confirm client ID
4144 * @clp: state data structure
4145 * @res: result of a previous SETCLIENTID
4146 * @cred: RPC credential to use for this call
4147 *
4148 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4149 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004150int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004151 struct nfs4_setclientid_res *arg,
4152 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
4154 struct nfs_fsinfo fsinfo;
4155 struct rpc_message msg = {
4156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004157 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004159 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 };
4161 unsigned long now;
4162 int status;
4163
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004164 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4165 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4166 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004168 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 if (status == 0) {
4170 spin_lock(&clp->cl_lock);
4171 clp->cl_lease_time = fsinfo.lease_time * HZ;
4172 clp->cl_last_renewal = now;
4173 spin_unlock(&clp->cl_lock);
4174 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004175 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 return status;
4177}
4178
Trond Myklebustfe650402006-01-03 09:55:18 +01004179struct nfs4_delegreturndata {
4180 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004181 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004182 struct nfs_fh fh;
4183 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004184 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004185 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004186 int rpc_status;
4187};
4188
Trond Myklebustfe650402006-01-03 09:55:18 +01004189static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4190{
4191 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004192
Trond Myklebust14516c32010-07-31 14:29:06 -04004193 if (!nfs4_sequence_done(task, &data->res.seq_res))
4194 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004195
Ricardo Labiaga79708862009-12-07 09:23:21 -05004196 switch (task->tk_status) {
4197 case -NFS4ERR_STALE_STATEID:
4198 case -NFS4ERR_EXPIRED:
4199 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004200 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004201 break;
4202 default:
4203 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4204 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004205 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004206 return;
4207 }
4208 }
4209 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004210}
4211
4212static void nfs4_delegreturn_release(void *calldata)
4213{
Trond Myklebustfe650402006-01-03 09:55:18 +01004214 kfree(calldata);
4215}
4216
Andy Adamson938e1012009-04-01 09:22:28 -04004217#if defined(CONFIG_NFS_V4_1)
4218static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4219{
4220 struct nfs4_delegreturndata *d_data;
4221
4222 d_data = (struct nfs4_delegreturndata *)data;
4223
Trond Myklebust035168a2010-06-16 09:52:26 -04004224 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004225 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004226 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004227 return;
4228 rpc_call_start(task);
4229}
4230#endif /* CONFIG_NFS_V4_1 */
4231
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004232static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004233#if defined(CONFIG_NFS_V4_1)
4234 .rpc_call_prepare = nfs4_delegreturn_prepare,
4235#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004236 .rpc_call_done = nfs4_delegreturn_done,
4237 .rpc_release = nfs4_delegreturn_release,
4238};
4239
Trond Myklebuste6f81072008-01-24 18:14:34 -05004240static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004241{
4242 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004243 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004244 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004245 struct rpc_message msg = {
4246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4247 .rpc_cred = cred,
4248 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004249 struct rpc_task_setup task_setup_data = {
4250 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004251 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004252 .callback_ops = &nfs4_delegreturn_ops,
4253 .flags = RPC_TASK_ASYNC,
4254 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004255 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004256
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004257 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004258 if (data == NULL)
4259 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004260 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004261 data->args.fhandle = &data->fh;
4262 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004263 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004264 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004265 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004266 data->res.fattr = &data->fattr;
4267 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004268 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004269 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004270 data->rpc_status = 0;
4271
Trond Myklebustc970aa82007-07-14 15:39:59 -04004272 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004273 msg.rpc_argp = &data->args;
4274 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004275 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004276 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004277 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004278 if (!issync)
4279 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004280 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004281 if (status != 0)
4282 goto out;
4283 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004284 if (status == 0)
4285 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4286 else
4287 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004288out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004289 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004290 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291}
4292
Trond Myklebuste6f81072008-01-24 18:14:34 -05004293int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
4295 struct nfs_server *server = NFS_SERVER(inode);
4296 struct nfs4_exception exception = { };
4297 int err;
4298 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004299 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 switch (err) {
4301 case -NFS4ERR_STALE_STATEID:
4302 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 case 0:
4304 return 0;
4305 }
4306 err = nfs4_handle_exception(server, err, &exception);
4307 } while (exception.retry);
4308 return err;
4309}
4310
4311#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4312#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4313
4314/*
4315 * sleep, with exponential backoff, and retry the LOCK operation.
4316 */
4317static unsigned long
4318nfs4_set_lock_task_retry(unsigned long timeout)
4319{
Jeff Laytond3103102011-12-01 22:44:39 +01004320 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 timeout <<= 1;
4322 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4323 return NFS4_LOCK_MAXTIMEOUT;
4324 return timeout;
4325}
4326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4328{
4329 struct inode *inode = state->inode;
4330 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004331 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004332 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004334 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004336 struct nfs_lockt_res res = {
4337 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 };
4339 struct rpc_message msg = {
4340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4341 .rpc_argp = &arg,
4342 .rpc_resp = &res,
4343 .rpc_cred = state->owner->so_cred,
4344 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 struct nfs4_lock_state *lsp;
4346 int status;
4347
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004348 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004349 status = nfs4_set_lock_state(state, request);
4350 if (status != 0)
4351 goto out;
4352 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004353 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004354 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004355 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004356 switch (status) {
4357 case 0:
4358 request->fl_type = F_UNLCK;
4359 break;
4360 case -NFS4ERR_DENIED:
4361 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004363 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004364out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return status;
4366}
4367
4368static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4369{
4370 struct nfs4_exception exception = { };
4371 int err;
4372
4373 do {
4374 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4375 _nfs4_proc_getlk(state, cmd, request),
4376 &exception);
4377 } while (exception.retry);
4378 return err;
4379}
4380
4381static int do_vfs_lock(struct file *file, struct file_lock *fl)
4382{
4383 int res = 0;
4384 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4385 case FL_POSIX:
4386 res = posix_lock_file_wait(file, fl);
4387 break;
4388 case FL_FLOCK:
4389 res = flock_lock_file_wait(file, fl);
4390 break;
4391 default:
4392 BUG();
4393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 return res;
4395}
4396
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004397struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004398 struct nfs_locku_args arg;
4399 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004400 struct nfs4_lock_state *lsp;
4401 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004402 struct file_lock fl;
4403 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004404 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004405};
4406
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004407static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4408 struct nfs_open_context *ctx,
4409 struct nfs4_lock_state *lsp,
4410 struct nfs_seqid *seqid)
4411{
4412 struct nfs4_unlockdata *p;
4413 struct inode *inode = lsp->ls_state->inode;
4414
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004415 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004416 if (p == NULL)
4417 return NULL;
4418 p->arg.fh = NFS_FH(inode);
4419 p->arg.fl = &p->fl;
4420 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004421 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004422 p->arg.stateid = &lsp->ls_stateid;
4423 p->lsp = lsp;
4424 atomic_inc(&lsp->ls_count);
4425 /* Ensure we don't close file until we're done freeing locks! */
4426 p->ctx = get_nfs_open_context(ctx);
4427 memcpy(&p->fl, fl, sizeof(p->fl));
4428 p->server = NFS_SERVER(inode);
4429 return p;
4430}
4431
Trond Myklebust06f814a2006-01-03 09:55:07 +01004432static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004433{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004434 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004435 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004436 nfs4_put_lock_state(calldata->lsp);
4437 put_nfs_open_context(calldata->ctx);
4438 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004439}
4440
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004441static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004442{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004443 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004444
Trond Myklebust14516c32010-07-31 14:29:06 -04004445 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4446 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004447 switch (task->tk_status) {
4448 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004449 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4450 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004451 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004452 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004453 case -NFS4ERR_BAD_STATEID:
4454 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004455 case -NFS4ERR_STALE_STATEID:
4456 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004457 break;
4458 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004459 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004460 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004461 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004462}
4463
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004464static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004465{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004466 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004468 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004469 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004470 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004471 /* Note: exit _without_ running nfs4_locku_done */
4472 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004473 return;
4474 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004475 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004476 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004477 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004478 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004479 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004480 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481}
4482
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004483static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004484 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004485 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004486 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004487};
4488
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004489static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4490 struct nfs_open_context *ctx,
4491 struct nfs4_lock_state *lsp,
4492 struct nfs_seqid *seqid)
4493{
4494 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004495 struct rpc_message msg = {
4496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4497 .rpc_cred = ctx->cred,
4498 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004499 struct rpc_task_setup task_setup_data = {
4500 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004501 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004502 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004503 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004504 .flags = RPC_TASK_ASYNC,
4505 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004506
Frank Filz137d6ac2007-07-09 15:32:29 -07004507 /* Ensure this is an unlock - when canceling a lock, the
4508 * canceled lock is passed in, and it won't be an unlock.
4509 */
4510 fl->fl_type = F_UNLCK;
4511
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004512 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4513 if (data == NULL) {
4514 nfs_free_seqid(seqid);
4515 return ERR_PTR(-ENOMEM);
4516 }
4517
Trond Myklebust9d12b212012-01-17 22:04:25 -05004518 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004519 msg.rpc_argp = &data->arg;
4520 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004521 task_setup_data.callback_data = data;
4522 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523}
4524
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4526{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004527 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004528 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004529 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004530 struct rpc_task *task;
4531 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004532 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
Trond Myklebust9b073572006-06-29 16:38:34 -04004534 status = nfs4_set_lock_state(state, request);
4535 /* Unlock _before_ we do the RPC call */
4536 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004537 down_read(&nfsi->rwsem);
4538 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4539 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004540 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004541 }
4542 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004543 if (status != 0)
4544 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004545 /* Is this a delegated lock? */
4546 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004547 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004548 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004549 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004550 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004551 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004552 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004553 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004554 status = PTR_ERR(task);
4555 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004556 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004557 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004558 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004559out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004560 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004561 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004564struct nfs4_lockdata {
4565 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004566 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567 struct nfs4_lock_state *lsp;
4568 struct nfs_open_context *ctx;
4569 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004570 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004571 int rpc_status;
4572 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004573 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004574};
4575
4576static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004577 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4578 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004579{
4580 struct nfs4_lockdata *p;
4581 struct inode *inode = lsp->ls_state->inode;
4582 struct nfs_server *server = NFS_SERVER(inode);
4583
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004584 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004585 if (p == NULL)
4586 return NULL;
4587
4588 p->arg.fh = NFS_FH(inode);
4589 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004590 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004591 if (p->arg.open_seqid == NULL)
4592 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004593 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004594 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004595 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004596 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004597 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004598 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004599 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004600 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004601 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004602 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004603 atomic_inc(&lsp->ls_count);
4604 p->ctx = get_nfs_open_context(ctx);
4605 memcpy(&p->fl, fl, sizeof(p->fl));
4606 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004607out_free_seqid:
4608 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004609out_free:
4610 kfree(p);
4611 return NULL;
4612}
4613
4614static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4615{
4616 struct nfs4_lockdata *data = calldata;
4617 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Harvey Harrison3110ff82008-05-02 13:42:44 -07004619 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004620 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4621 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004622 /* Do we need to do an open_to_lock_owner? */
4623 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004624 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4625 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004626 data->arg.open_stateid = &state->stateid;
4627 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004628 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004629 } else
4630 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004631 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004632 if (nfs4_setup_sequence(data->server,
4633 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004634 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004635 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004636 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004637 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004638}
4639
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004640static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4641{
4642 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4643 nfs4_lock_prepare(task, calldata);
4644}
4645
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004646static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4647{
4648 struct nfs4_lockdata *data = calldata;
4649
Harvey Harrison3110ff82008-05-02 13:42:44 -07004650 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004651
Trond Myklebust14516c32010-07-31 14:29:06 -04004652 if (!nfs4_sequence_done(task, &data->res.seq_res))
4653 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004654
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004655 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004656 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004657 if (data->rpc_status == 0)
4658 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4659 else
4660 goto out;
4661 }
4662 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004663 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004664 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004665 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004666 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004667out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004668 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004669}
4670
4671static void nfs4_lock_release(void *calldata)
4672{
4673 struct nfs4_lockdata *data = calldata;
4674
Harvey Harrison3110ff82008-05-02 13:42:44 -07004675 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004676 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004677 if (data->cancelled != 0) {
4678 struct rpc_task *task;
4679 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4680 data->arg.lock_seqid);
4681 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004682 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004683 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004684 } else
4685 nfs_free_seqid(data->arg.lock_seqid);
4686 nfs4_put_lock_state(data->lsp);
4687 put_nfs_open_context(data->ctx);
4688 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004689 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004690}
4691
4692static const struct rpc_call_ops nfs4_lock_ops = {
4693 .rpc_call_prepare = nfs4_lock_prepare,
4694 .rpc_call_done = nfs4_lock_done,
4695 .rpc_release = nfs4_lock_release,
4696};
4697
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004698static const struct rpc_call_ops nfs4_recover_lock_ops = {
4699 .rpc_call_prepare = nfs4_recover_lock_prepare,
4700 .rpc_call_done = nfs4_lock_done,
4701 .rpc_release = nfs4_lock_release,
4702};
4703
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004704static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4705{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004706 switch (error) {
4707 case -NFS4ERR_ADMIN_REVOKED:
4708 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004709 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004710 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004711 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004712 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004713 break;
4714 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004715 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004716 case -NFS4ERR_EXPIRED:
4717 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004718 };
4719}
4720
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004721static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004722{
4723 struct nfs4_lockdata *data;
4724 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004725 struct rpc_message msg = {
4726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4727 .rpc_cred = state->owner->so_cred,
4728 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004729 struct rpc_task_setup task_setup_data = {
4730 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004731 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004732 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004733 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004734 .flags = RPC_TASK_ASYNC,
4735 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004736 int ret;
4737
Harvey Harrison3110ff82008-05-02 13:42:44 -07004738 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004739 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004740 fl->fl_u.nfs4_fl.owner,
4741 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004742 if (data == NULL)
4743 return -ENOMEM;
4744 if (IS_SETLKW(cmd))
4745 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004746 if (recovery_type > NFS_LOCK_NEW) {
4747 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004748 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004749 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4750 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004751 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004752 msg.rpc_argp = &data->arg;
4753 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004754 task_setup_data.callback_data = data;
4755 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004756 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004757 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004758 ret = nfs4_wait_for_completion_rpc_task(task);
4759 if (ret == 0) {
4760 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004761 if (ret)
4762 nfs4_handle_setlk_error(data->server, data->lsp,
4763 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004764 } else
4765 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004766 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004767 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004768 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769}
4770
4771static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4772{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004773 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004774 struct nfs4_exception exception = {
4775 .inode = state->inode,
4776 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004777 int err;
4778
4779 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004780 /* Cache the lock if possible... */
4781 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4782 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004783 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004784 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004785 break;
4786 nfs4_handle_exception(server, err, &exception);
4787 } while (exception.retry);
4788 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
4790
4791static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4792{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004793 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004794 struct nfs4_exception exception = {
4795 .inode = state->inode,
4796 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004797 int err;
4798
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004799 err = nfs4_set_lock_state(state, request);
4800 if (err != 0)
4801 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004802 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004803 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4804 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004805 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004806 switch (err) {
4807 default:
4808 goto out;
4809 case -NFS4ERR_GRACE:
4810 case -NFS4ERR_DELAY:
4811 nfs4_handle_exception(server, err, &exception);
4812 err = 0;
4813 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004814 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004815out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004816 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817}
4818
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004819#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004820/**
4821 * nfs41_check_expired_locks - possibly free a lock stateid
4822 *
4823 * @state: NFSv4 state for an inode
4824 *
4825 * Returns NFS_OK if recovery for this stateid is now finished.
4826 * Otherwise a negative NFS4ERR value is returned.
4827 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004828static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004829{
Chuck Levereb64cf92012-07-11 16:30:05 -04004830 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004831 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004832 struct nfs_server *server = NFS_SERVER(state->inode);
4833
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004834 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004835 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004836 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4837 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004838 /* Free the stateid unless the server
4839 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004840 if (status != -NFS4ERR_BAD_STATEID)
4841 nfs41_free_stateid(server,
4842 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004843 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004844 ret = status;
4845 }
4846 }
4847 };
4848
4849 return ret;
4850}
4851
4852static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4853{
4854 int status = NFS_OK;
4855
4856 if (test_bit(LK_STATE_IN_USE, &state->flags))
4857 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004858 if (status != NFS_OK)
4859 status = nfs4_lock_expired(state, request);
4860 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004861}
4862#endif
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4865{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004866 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004867 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004868 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004870 if ((fl_flags & FL_POSIX) &&
4871 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4872 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004873 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004874 status = nfs4_set_lock_state(state, request);
4875 if (status != 0)
4876 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004877 request->fl_flags |= FL_ACCESS;
4878 status = do_vfs_lock(request->fl_file, request);
4879 if (status < 0)
4880 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004881 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004882 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004883 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004884 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004885 request->fl_flags = fl_flags & ~FL_SLEEP;
4886 status = do_vfs_lock(request->fl_file, request);
4887 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004888 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004889 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004890 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004891 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004892 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004893 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004894 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004895 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4896 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004897out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004898 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004899out:
4900 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 return status;
4902}
4903
4904static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4905{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004906 struct nfs4_exception exception = {
4907 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004908 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004909 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 int err;
4911
4912 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004913 err = _nfs4_proc_setlk(state, cmd, request);
4914 if (err == -NFS4ERR_DENIED)
4915 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004917 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 } while (exception.retry);
4919 return err;
4920}
4921
4922static int
4923nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4924{
4925 struct nfs_open_context *ctx;
4926 struct nfs4_state *state;
4927 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4928 int status;
4929
4930 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004931 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 state = ctx->state;
4933
4934 if (request->fl_start < 0 || request->fl_end < 0)
4935 return -EINVAL;
4936
Trond Myklebustd9531262009-07-21 19:22:38 -04004937 if (IS_GETLK(cmd)) {
4938 if (state != NULL)
4939 return nfs4_proc_getlk(state, F_GETLK, request);
4940 return 0;
4941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
4943 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4944 return -EINVAL;
4945
Trond Myklebustd9531262009-07-21 19:22:38 -04004946 if (request->fl_type == F_UNLCK) {
4947 if (state != NULL)
4948 return nfs4_proc_unlck(state, cmd, request);
4949 return 0;
4950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Trond Myklebustd9531262009-07-21 19:22:38 -04004952 if (state == NULL)
4953 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004954 /*
4955 * Don't rely on the VFS having checked the file open mode,
4956 * since it won't do this for flock() locks.
4957 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04004958 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04004959 case F_RDLCK:
4960 if (!(filp->f_mode & FMODE_READ))
4961 return -EBADF;
4962 break;
4963 case F_WRLCK:
4964 if (!(filp->f_mode & FMODE_WRITE))
4965 return -EBADF;
4966 }
4967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 do {
4969 status = nfs4_proc_setlk(state, cmd, request);
4970 if ((status != -EAGAIN) || IS_SETLK(cmd))
4971 break;
4972 timeout = nfs4_set_lock_task_retry(timeout);
4973 status = -ERESTARTSYS;
4974 if (signalled())
4975 break;
4976 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 return status;
4978}
4979
Trond Myklebust888e6942005-11-04 15:38:11 -05004980int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4981{
4982 struct nfs_server *server = NFS_SERVER(state->inode);
4983 struct nfs4_exception exception = { };
4984 int err;
4985
4986 err = nfs4_set_lock_state(state, fl);
4987 if (err != 0)
4988 goto out;
4989 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004990 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004991 switch (err) {
4992 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004993 printk(KERN_ERR "NFS: %s: unhandled error "
4994 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004995 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004996 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004997 goto out;
4998 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004999 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005000 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005001 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005002 nfs4_schedule_lease_recovery(server->nfs_client);
5003 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005004 case -NFS4ERR_BADSESSION:
5005 case -NFS4ERR_BADSLOT:
5006 case -NFS4ERR_BAD_HIGH_SLOT:
5007 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5008 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005009 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005010 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005011 case -ERESTARTSYS:
5012 /*
5013 * The show must go on: exit, but mark the
5014 * stateid as needing recovery.
5015 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005016 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005017 case -NFS4ERR_ADMIN_REVOKED:
5018 case -NFS4ERR_BAD_STATEID:
5019 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005020 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005021 err = 0;
5022 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005023 case -EKEYEXPIRED:
5024 /*
5025 * User RPCSEC_GSS context has expired.
5026 * We cannot recover this stateid now, so
5027 * skip it and allow recovery thread to
5028 * proceed.
5029 */
5030 err = 0;
5031 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005032 case -ENOMEM:
5033 case -NFS4ERR_DENIED:
5034 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5035 err = 0;
5036 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005037 case -NFS4ERR_DELAY:
5038 break;
5039 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005040 err = nfs4_handle_exception(server, err, &exception);
5041 } while (exception.retry);
5042out:
5043 return err;
5044}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005045
Trond Myklebustcf470c32012-03-07 13:49:12 -05005046struct nfs_release_lockowner_data {
5047 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005048 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005049 struct nfs_release_lockowner_args args;
5050};
5051
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005052static void nfs4_release_lockowner_release(void *calldata)
5053{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005054 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005055 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005056 kfree(calldata);
5057}
5058
Trond Myklebust17280172012-03-11 13:11:00 -04005059static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005060 .rpc_release = nfs4_release_lockowner_release,
5061};
5062
Trond Myklebustcf470c32012-03-07 13:49:12 -05005063int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005064{
5065 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005066 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005067 struct rpc_message msg = {
5068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5069 };
5070
5071 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005072 return -EINVAL;
5073 data = kmalloc(sizeof(*data), GFP_NOFS);
5074 if (!data)
5075 return -ENOMEM;
5076 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005077 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005078 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5079 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5080 data->args.lock_owner.s_dev = server->s_dev;
5081 msg.rpc_argp = &data->args;
5082 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5083 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005084}
5085
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005086#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5087
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005088static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5089 const void *buf, size_t buflen,
5090 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005091{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005092 if (strcmp(key, "") != 0)
5093 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005094
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005095 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005096}
5097
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005098static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5099 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005100{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005101 if (strcmp(key, "") != 0)
5102 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005103
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005104 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005105}
5106
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005107static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5108 size_t list_len, const char *name,
5109 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005110{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005111 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005112
J. Bruce Fields096455a2006-03-20 23:23:42 -05005113 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5114 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005115
5116 if (list && len <= list_len)
5117 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005118 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005119}
5120
Andy Adamson533eb462011-06-13 18:25:56 -04005121/*
5122 * nfs_fhget will use either the mounted_on_fileid or the fileid
5123 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005124static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5125{
Andy Adamson533eb462011-06-13 18:25:56 -04005126 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5127 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5128 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005129 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005130 return;
5131
5132 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005133 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005134 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5135 fattr->nlink = 2;
5136}
5137
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005138static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5139 const struct qstr *name,
5140 struct nfs4_fs_locations *fs_locations,
5141 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005142{
5143 struct nfs_server *server = NFS_SERVER(dir);
5144 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005145 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005146 };
5147 struct nfs4_fs_locations_arg args = {
5148 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005149 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005150 .page = page,
5151 .bitmask = bitmask,
5152 };
Benny Halevy22958462009-04-01 09:22:02 -04005153 struct nfs4_fs_locations_res res = {
5154 .fs_locations = fs_locations,
5155 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005156 struct rpc_message msg = {
5157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5158 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005159 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005160 };
5161 int status;
5162
Harvey Harrison3110ff82008-05-02 13:42:44 -07005163 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005164
5165 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5166 * is not supported */
5167 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5168 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5169 else
5170 bitmask[0] |= FATTR4_WORD0_FILEID;
5171
Trond Myklebustc228fd32007-01-13 02:28:11 -05005172 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005173 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005174 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005175 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005176 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005177 return status;
5178}
5179
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005180int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5181 const struct qstr *name,
5182 struct nfs4_fs_locations *fs_locations,
5183 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005184{
5185 struct nfs4_exception exception = { };
5186 int err;
5187 do {
5188 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005189 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005190 &exception);
5191 } while (exception.retry);
5192 return err;
5193}
5194
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005195static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5196{
5197 int status;
5198 struct nfs4_secinfo_arg args = {
5199 .dir_fh = NFS_FH(dir),
5200 .name = name,
5201 };
5202 struct nfs4_secinfo_res res = {
5203 .flavors = flavors,
5204 };
5205 struct rpc_message msg = {
5206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5207 .rpc_argp = &args,
5208 .rpc_resp = &res,
5209 };
5210
5211 dprintk("NFS call secinfo %s\n", name->name);
5212 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5213 dprintk("NFS reply secinfo: %d\n", status);
5214 return status;
5215}
5216
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005217int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5218 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005219{
5220 struct nfs4_exception exception = { };
5221 int err;
5222 do {
5223 err = nfs4_handle_exception(NFS_SERVER(dir),
5224 _nfs4_proc_secinfo(dir, name, flavors),
5225 &exception);
5226 } while (exception.retry);
5227 return err;
5228}
5229
Andy Adamson557134a2009-04-01 09:21:53 -04005230#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005231/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005232 * Check the exchange flags returned by the server for invalid flags, having
5233 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5234 * DS flags set.
5235 */
5236static int nfs4_check_cl_exchange_flags(u32 flags)
5237{
5238 if (flags & ~EXCHGID4_FLAG_MASK_R)
5239 goto out_inval;
5240 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5241 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5242 goto out_inval;
5243 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5244 goto out_inval;
5245 return NFS_OK;
5246out_inval:
5247 return -NFS4ERR_INVAL;
5248}
5249
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005250static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005251nfs41_same_server_scope(struct nfs41_server_scope *a,
5252 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005253{
5254 if (a->server_scope_sz == b->server_scope_sz &&
5255 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5256 return true;
5257
5258 return false;
5259}
5260
Andy Adamson357f54d2010-12-14 10:11:57 -05005261/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005262 * nfs4_proc_bind_conn_to_session()
5263 *
5264 * The 4.1 client currently uses the same TCP connection for the
5265 * fore and backchannel.
5266 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005267int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005268{
5269 int status;
5270 struct nfs41_bind_conn_to_session_res res;
5271 struct rpc_message msg = {
5272 .rpc_proc =
5273 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5274 .rpc_argp = clp,
5275 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005276 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005277 };
5278
5279 dprintk("--> %s\n", __func__);
5280 BUG_ON(clp == NULL);
5281
5282 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5283 if (unlikely(res.session == NULL)) {
5284 status = -ENOMEM;
5285 goto out;
5286 }
5287
5288 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5289 if (status == 0) {
5290 if (memcmp(res.session->sess_id.data,
5291 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5292 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5293 status = -EIO;
5294 goto out_session;
5295 }
5296 if (res.dir != NFS4_CDFS4_BOTH) {
5297 dprintk("NFS: %s: Unexpected direction from server\n",
5298 __func__);
5299 status = -EIO;
5300 goto out_session;
5301 }
5302 if (res.use_conn_in_rdma_mode) {
5303 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5304 __func__);
5305 status = -EIO;
5306 goto out_session;
5307 }
5308 }
5309out_session:
5310 kfree(res.session);
5311out:
5312 dprintk("<-- %s status= %d\n", __func__, status);
5313 return status;
5314}
5315
5316/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005317 * nfs4_proc_exchange_id()
5318 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005319 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5320 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005321 * Since the clientid has expired, all compounds using sessions
5322 * associated with the stale clientid will be returning
5323 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5324 * be in some phase of session reset.
5325 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005326int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005327{
5328 nfs4_verifier verifier;
5329 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005330 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005331 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005332 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005333 };
5334 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005335 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005336 };
5337 int status;
5338 struct rpc_message msg = {
5339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5340 .rpc_argp = &args,
5341 .rpc_resp = &res,
5342 .rpc_cred = cred,
5343 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005344
Chuck Leverf0920752012-05-21 22:45:41 -04005345 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005346 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5347 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005348 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5349 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5350 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005351
Chuck Leveracdeb692012-05-21 22:46:16 -04005352 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005353 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005354 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005355 status = -ENOMEM;
5356 goto out;
5357 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005358
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005359 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005360 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005361 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005362 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005363 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005364 }
5365
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005366 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005367 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005368 status = -ENOMEM;
5369 goto out_server_scope;
5370 }
5371
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005372 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005373 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005374 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005375
Chuck Lever177313f2012-05-21 22:44:58 -04005376 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005377 clp->cl_clientid = res.clientid;
5378 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5379 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5380 clp->cl_seqid = res.seqid;
5381
Chuck Leveracdeb692012-05-21 22:46:16 -04005382 kfree(clp->cl_serverowner);
5383 clp->cl_serverowner = res.server_owner;
5384 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005385
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005386 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005387 kfree(clp->cl_implid);
5388 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005389
Chuck Lever177313f2012-05-21 22:44:58 -04005390 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005391 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005392 res.server_scope)) {
5393 dprintk("%s: server_scope mismatch detected\n",
5394 __func__);
5395 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005396 kfree(clp->cl_serverscope);
5397 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005398 }
5399
Chuck Lever177313f2012-05-21 22:44:58 -04005400 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005401 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005402 goto out;
5403 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005404 } else
5405 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005406
Chuck Leveracdeb692012-05-21 22:46:16 -04005407out_server_owner:
5408 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005409out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005410 kfree(res.server_scope);
5411out:
Chuck Lever177313f2012-05-21 22:44:58 -04005412 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005413 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005414 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005415 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005416 clp->cl_implid->date.seconds,
5417 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005418 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005419 return status;
5420}
5421
Trond Myklebust66245532012-05-25 17:18:09 -04005422static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5423 struct rpc_cred *cred)
5424{
5425 struct rpc_message msg = {
5426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5427 .rpc_argp = clp,
5428 .rpc_cred = cred,
5429 };
5430 int status;
5431
5432 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5433 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005434 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005435 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5436 return status;
5437}
5438
5439static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5440 struct rpc_cred *cred)
5441{
5442 unsigned int loop;
5443 int ret;
5444
5445 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5446 ret = _nfs4_proc_destroy_clientid(clp, cred);
5447 switch (ret) {
5448 case -NFS4ERR_DELAY:
5449 case -NFS4ERR_CLIENTID_BUSY:
5450 ssleep(1);
5451 break;
5452 default:
5453 return ret;
5454 }
5455 }
5456 return 0;
5457}
5458
5459int nfs4_destroy_clientid(struct nfs_client *clp)
5460{
5461 struct rpc_cred *cred;
5462 int ret = 0;
5463
5464 if (clp->cl_mvops->minor_version < 1)
5465 goto out;
5466 if (clp->cl_exchange_flags == 0)
5467 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005468 if (clp->cl_preserve_clid)
5469 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005470 cred = nfs4_get_exchange_id_cred(clp);
5471 ret = nfs4_proc_destroy_clientid(clp, cred);
5472 if (cred)
5473 put_rpccred(cred);
5474 switch (ret) {
5475 case 0:
5476 case -NFS4ERR_STALE_CLIENTID:
5477 clp->cl_exchange_flags = 0;
5478 }
5479out:
5480 return ret;
5481}
5482
Andy Adamson2050f0c2009-04-01 09:22:30 -04005483struct nfs4_get_lease_time_data {
5484 struct nfs4_get_lease_time_args *args;
5485 struct nfs4_get_lease_time_res *res;
5486 struct nfs_client *clp;
5487};
5488
5489static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5490 void *calldata)
5491{
5492 int ret;
5493 struct nfs4_get_lease_time_data *data =
5494 (struct nfs4_get_lease_time_data *)calldata;
5495
5496 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005497 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005498 /* just setup sequence, do not trigger session recovery
5499 since we're invoked within one */
5500 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005501 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005502 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005503
5504 BUG_ON(ret == -EAGAIN);
5505 rpc_call_start(task);
5506 dprintk("<-- %s\n", __func__);
5507}
5508
5509/*
5510 * Called from nfs4_state_manager thread for session setup, so don't recover
5511 * from sequence operation or clientid errors.
5512 */
5513static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5514{
5515 struct nfs4_get_lease_time_data *data =
5516 (struct nfs4_get_lease_time_data *)calldata;
5517
5518 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005519 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5520 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005521 switch (task->tk_status) {
5522 case -NFS4ERR_DELAY:
5523 case -NFS4ERR_GRACE:
5524 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5525 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5526 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005527 /* fall through */
5528 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005529 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005530 return;
5531 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005532 dprintk("<-- %s\n", __func__);
5533}
5534
Trond Myklebust17280172012-03-11 13:11:00 -04005535static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005536 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5537 .rpc_call_done = nfs4_get_lease_time_done,
5538};
5539
5540int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5541{
5542 struct rpc_task *task;
5543 struct nfs4_get_lease_time_args args;
5544 struct nfs4_get_lease_time_res res = {
5545 .lr_fsinfo = fsinfo,
5546 };
5547 struct nfs4_get_lease_time_data data = {
5548 .args = &args,
5549 .res = &res,
5550 .clp = clp,
5551 };
5552 struct rpc_message msg = {
5553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5554 .rpc_argp = &args,
5555 .rpc_resp = &res,
5556 };
5557 struct rpc_task_setup task_setup = {
5558 .rpc_client = clp->cl_rpcclient,
5559 .rpc_message = &msg,
5560 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005561 .callback_data = &data,
5562 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005563 };
5564 int status;
5565
Trond Myklebust9d12b212012-01-17 22:04:25 -05005566 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005567 dprintk("--> %s\n", __func__);
5568 task = rpc_run_task(&task_setup);
5569
5570 if (IS_ERR(task))
5571 status = PTR_ERR(task);
5572 else {
5573 status = task->tk_status;
5574 rpc_put_task(task);
5575 }
5576 dprintk("<-- %s return %d\n", __func__, status);
5577
5578 return status;
5579}
5580
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005581static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5582{
5583 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5584}
5585
5586static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5587 struct nfs4_slot *new,
5588 u32 max_slots,
5589 u32 ivalue)
5590{
5591 struct nfs4_slot *old = NULL;
5592 u32 i;
5593
5594 spin_lock(&tbl->slot_tbl_lock);
5595 if (new) {
5596 old = tbl->slots;
5597 tbl->slots = new;
5598 tbl->max_slots = max_slots;
5599 }
5600 tbl->highest_used_slotid = -1; /* no slot is currently used */
5601 for (i = 0; i < tbl->max_slots; i++)
5602 tbl->slots[i].seq_nr = ivalue;
5603 spin_unlock(&tbl->slot_tbl_lock);
5604 kfree(old);
5605}
5606
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005607/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005608 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005609 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005610static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5611 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005612{
Andy Adamson104aeba2010-01-14 17:45:10 -05005613 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005614 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005615
Andy Adamson104aeba2010-01-14 17:45:10 -05005616 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5617 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005618
Andy Adamson104aeba2010-01-14 17:45:10 -05005619 /* Does the newly negotiated max_reqs match the existing slot table? */
5620 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005621 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005622 if (!new)
5623 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005624 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005625 ret = 0;
5626
5627 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005628 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5629 tbl, tbl->slots, tbl->max_slots);
5630out:
5631 dprintk("<-- %s: return %d\n", __func__, ret);
5632 return ret;
5633}
5634
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005635/* Destroy the slot table */
5636static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5637{
5638 if (session->fc_slot_table.slots != NULL) {
5639 kfree(session->fc_slot_table.slots);
5640 session->fc_slot_table.slots = NULL;
5641 }
5642 if (session->bc_slot_table.slots != NULL) {
5643 kfree(session->bc_slot_table.slots);
5644 session->bc_slot_table.slots = NULL;
5645 }
5646 return;
5647}
5648
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005649/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005650 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005651 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005652static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005653{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005654 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005655 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005656
Andy Adamsonaacd5532011-11-09 13:58:21 -05005657 dprintk("--> %s\n", __func__);
5658 /* Fore channel */
5659 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005660 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5661 if (status) /* -ENOMEM */
5662 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005663 /* Back channel */
5664 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005665 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5666 if (status && tbl->slots == NULL)
5667 /* Fore and back channel share a connection so get
5668 * both slot tables or neither */
5669 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005670 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005671}
5672
5673struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5674{
5675 struct nfs4_session *session;
5676 struct nfs4_slot_table *tbl;
5677
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005678 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005679 if (!session)
5680 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005681
Andy Adamson557134a2009-04-01 09:21:53 -04005682 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005683 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005684 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005685 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005686 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005687
5688 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005689 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005690 spin_lock_init(&tbl->slot_tbl_lock);
5691 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005692 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005693
Trond Myklebust1055d762010-06-16 09:52:27 -04005694 session->session_state = 1<<NFS4_SESSION_INITING;
5695
Andy Adamson557134a2009-04-01 09:21:53 -04005696 session->clp = clp;
5697 return session;
5698}
5699
5700void nfs4_destroy_session(struct nfs4_session *session)
5701{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005702 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005703 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005704
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005705 cred = nfs4_get_exchange_id_cred(session->clp);
5706 nfs4_proc_destroy_session(session, cred);
5707 if (cred)
5708 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005709
5710 rcu_read_lock();
5711 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5712 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005713 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005714 __func__, xprt);
5715 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005716 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005717 kfree(session);
5718}
5719
Andy Adamsonfc931582009-04-01 09:22:31 -04005720/*
5721 * Initialize the values to be used by the client in CREATE_SESSION
5722 * If nfs4_init_session set the fore channel request and response sizes,
5723 * use them.
5724 *
5725 * Set the back channel max_resp_sz_cached to zero to force the client to
5726 * always set csa_cachethis to FALSE because the current implementation
5727 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5728 */
5729static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5730{
5731 struct nfs4_session *session = args->client->cl_session;
5732 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5733 mxresp_sz = session->fc_attrs.max_resp_sz;
5734
5735 if (mxrqst_sz == 0)
5736 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5737 if (mxresp_sz == 0)
5738 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5739 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005740 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5741 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005742 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005743 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005744
5745 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005746 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005747 __func__,
5748 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005749 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005750
5751 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005752 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5753 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5754 args->bc_attrs.max_resp_sz_cached = 0;
5755 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5756 args->bc_attrs.max_reqs = 1;
5757
5758 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5759 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5760 __func__,
5761 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5762 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5763 args->bc_attrs.max_reqs);
5764}
5765
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005766static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005767{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005768 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5769 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5770
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005771 if (rcvd->max_resp_sz > sent->max_resp_sz)
5772 return -EINVAL;
5773 /*
5774 * Our requested max_ops is the minimum we need; we're not
5775 * prepared to break up compounds into smaller pieces than that.
5776 * So, no point even trying to continue if the server won't
5777 * cooperate:
5778 */
5779 if (rcvd->max_ops < sent->max_ops)
5780 return -EINVAL;
5781 if (rcvd->max_reqs == 0)
5782 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005783 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5784 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005785 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005786}
5787
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005788static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5789{
5790 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5791 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005792
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005793 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5794 return -EINVAL;
5795 if (rcvd->max_resp_sz < sent->max_resp_sz)
5796 return -EINVAL;
5797 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5798 return -EINVAL;
5799 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005800 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005801 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005802 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005803 return -EINVAL;
5804 return 0;
5805}
Andy Adamson8d353012009-04-01 09:22:32 -04005806
Andy Adamson8d353012009-04-01 09:22:32 -04005807static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5808 struct nfs4_session *session)
5809{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005810 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005811
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005812 ret = nfs4_verify_fore_channel_attrs(args, session);
5813 if (ret)
5814 return ret;
5815 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005816}
5817
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005818static int _nfs4_proc_create_session(struct nfs_client *clp,
5819 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005820{
5821 struct nfs4_session *session = clp->cl_session;
5822 struct nfs41_create_session_args args = {
5823 .client = clp,
5824 .cb_program = NFS4_CALLBACK,
5825 };
5826 struct nfs41_create_session_res res = {
5827 .client = clp,
5828 };
5829 struct rpc_message msg = {
5830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5831 .rpc_argp = &args,
5832 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005833 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005834 };
5835 int status;
5836
5837 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005838 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005839
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005840 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005841
Andy Adamson8d353012009-04-01 09:22:32 -04005842 if (!status)
5843 /* Verify the session's negotiated channel_attrs values */
5844 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005845 if (!status) {
5846 /* Increment the clientid slot sequence id */
5847 clp->cl_seqid++;
5848 }
5849
5850 return status;
5851}
5852
5853/*
5854 * Issues a CREATE_SESSION operation to the server.
5855 * It is the responsibility of the caller to verify the session is
5856 * expired before calling this routine.
5857 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005858int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005859{
5860 int status;
5861 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005862 struct nfs4_session *session = clp->cl_session;
5863
5864 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5865
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005866 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005867 if (status)
5868 goto out;
5869
Andy Adamsonaacd5532011-11-09 13:58:21 -05005870 /* Init or reset the session slot tables */
5871 status = nfs4_setup_session_slot_tables(session);
5872 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005873 if (status)
5874 goto out;
5875
5876 ptr = (unsigned *)&session->sess_id.data[0];
5877 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5878 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005879out:
5880 dprintk("<-- %s\n", __func__);
5881 return status;
5882}
5883
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005884/*
5885 * Issue the over-the-wire RPC DESTROY_SESSION.
5886 * The caller must serialize access to this routine.
5887 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005888int nfs4_proc_destroy_session(struct nfs4_session *session,
5889 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005890{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005891 struct rpc_message msg = {
5892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5893 .rpc_argp = session,
5894 .rpc_cred = cred,
5895 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005896 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005897
5898 dprintk("--> nfs4_proc_destroy_session\n");
5899
5900 /* session is still being setup */
5901 if (session->clp->cl_cons_state != NFS_CS_READY)
5902 return status;
5903
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005904 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005905
5906 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005907 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005908 "Session has been destroyed regardless...\n", status);
5909
5910 dprintk("<-- nfs4_proc_destroy_session\n");
5911 return status;
5912}
5913
Trond Myklebust7b38c362012-05-23 13:23:31 -04005914/*
5915 * With sessions, the client is not marked ready until after a
5916 * successful EXCHANGE_ID and CREATE_SESSION.
5917 *
5918 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5919 * other versions of NFS can be tried.
5920 */
5921static int nfs41_check_session_ready(struct nfs_client *clp)
5922{
5923 int ret;
5924
5925 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5926 ret = nfs4_client_recover_expired_lease(clp);
5927 if (ret)
5928 return ret;
5929 }
5930 if (clp->cl_cons_state < NFS_CS_READY)
5931 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005932 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005933 return 0;
5934}
5935
Trond Myklebustfccba802009-07-21 16:48:07 -04005936int nfs4_init_session(struct nfs_server *server)
5937{
5938 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005939 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005940 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005941
5942 if (!nfs4_has_session(clp))
5943 return 0;
5944
Trond Myklebust1055d762010-06-16 09:52:27 -04005945 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005946 spin_lock(&clp->cl_lock);
5947 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005948
Trond Myklebust7b38c362012-05-23 13:23:31 -04005949 rsize = server->rsize;
5950 if (rsize == 0)
5951 rsize = NFS_MAX_FILE_IO_SIZE;
5952 wsize = server->wsize;
5953 if (wsize == 0)
5954 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005955
Trond Myklebust7b38c362012-05-23 13:23:31 -04005956 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5957 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5958 }
5959 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005960
Trond Myklebust7b38c362012-05-23 13:23:31 -04005961 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005962}
5963
Trond Myklebust7b38c362012-05-23 13:23:31 -04005964int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005965{
5966 struct nfs4_session *session = clp->cl_session;
5967 int ret;
5968
Trond Myklebust7b38c362012-05-23 13:23:31 -04005969 spin_lock(&clp->cl_lock);
5970 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5971 /*
5972 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5973 * DS lease to be equal to the MDS lease.
5974 */
5975 clp->cl_lease_time = lease_time;
5976 clp->cl_last_renewal = jiffies;
5977 }
5978 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005979
Trond Myklebust7b38c362012-05-23 13:23:31 -04005980 ret = nfs41_check_session_ready(clp);
5981 if (ret)
5982 return ret;
5983 /* Test for the DS role */
5984 if (!is_ds_client(clp))
5985 return -ENODEV;
5986 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005987}
5988EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5989
5990
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005991/*
5992 * Renew the cl_session lease.
5993 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005994struct nfs4_sequence_data {
5995 struct nfs_client *clp;
5996 struct nfs4_sequence_args args;
5997 struct nfs4_sequence_res res;
5998};
5999
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006000static void nfs41_sequence_release(void *data)
6001{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006002 struct nfs4_sequence_data *calldata = data;
6003 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006004
Alexandros Batsakis71358402010-02-05 03:45:05 -08006005 if (atomic_read(&clp->cl_count) > 1)
6006 nfs4_schedule_state_renewal(clp);
6007 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006008 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006009}
6010
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006011static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6012{
6013 switch(task->tk_status) {
6014 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006015 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6016 return -EAGAIN;
6017 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006018 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006019 }
6020 return 0;
6021}
6022
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006023static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006024{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006025 struct nfs4_sequence_data *calldata = data;
6026 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006027
Trond Myklebust14516c32010-07-31 14:29:06 -04006028 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6029 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006030
6031 if (task->tk_status < 0) {
6032 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006033 if (atomic_read(&clp->cl_count) == 1)
6034 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006035
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006036 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6037 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006038 return;
6039 }
6040 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006041 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006042out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006043 dprintk("<-- %s\n", __func__);
6044}
6045
6046static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6047{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006048 struct nfs4_sequence_data *calldata = data;
6049 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006050 struct nfs4_sequence_args *args;
6051 struct nfs4_sequence_res *res;
6052
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006053 args = task->tk_msg.rpc_argp;
6054 res = task->tk_msg.rpc_resp;
6055
Trond Myklebust9d12b212012-01-17 22:04:25 -05006056 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006057 return;
6058 rpc_call_start(task);
6059}
6060
6061static const struct rpc_call_ops nfs41_sequence_ops = {
6062 .rpc_call_done = nfs41_sequence_call_done,
6063 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006064 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006065};
6066
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006067static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006068{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006069 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006070 struct rpc_message msg = {
6071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6072 .rpc_cred = cred,
6073 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006074 struct rpc_task_setup task_setup_data = {
6075 .rpc_client = clp->cl_rpcclient,
6076 .rpc_message = &msg,
6077 .callback_ops = &nfs41_sequence_ops,
6078 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6079 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006080
Alexandros Batsakis71358402010-02-05 03:45:05 -08006081 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006082 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006083 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006084 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006085 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006086 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006087 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006088 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006089 msg.rpc_argp = &calldata->args;
6090 msg.rpc_resp = &calldata->res;
6091 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006092 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006093
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006094 return rpc_run_task(&task_setup_data);
6095}
6096
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006097static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006098{
6099 struct rpc_task *task;
6100 int ret = 0;
6101
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006102 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6103 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006104 task = _nfs41_proc_sequence(clp, cred);
6105 if (IS_ERR(task))
6106 ret = PTR_ERR(task);
6107 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006108 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006109 dprintk("<-- %s status=%d\n", __func__, ret);
6110 return ret;
6111}
6112
6113static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6114{
6115 struct rpc_task *task;
6116 int ret;
6117
6118 task = _nfs41_proc_sequence(clp, cred);
6119 if (IS_ERR(task)) {
6120 ret = PTR_ERR(task);
6121 goto out;
6122 }
6123 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006124 if (!ret) {
6125 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6126
6127 if (task->tk_status == 0)
6128 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006129 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006130 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006131 rpc_put_task(task);
6132out:
6133 dprintk("<-- %s status=%d\n", __func__, ret);
6134 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006135}
6136
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006137struct nfs4_reclaim_complete_data {
6138 struct nfs_client *clp;
6139 struct nfs41_reclaim_complete_args arg;
6140 struct nfs41_reclaim_complete_res res;
6141};
6142
6143static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6144{
6145 struct nfs4_reclaim_complete_data *calldata = data;
6146
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006147 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006148 if (nfs41_setup_sequence(calldata->clp->cl_session,
6149 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006150 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006151 return;
6152
6153 rpc_call_start(task);
6154}
6155
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006156static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6157{
6158 switch(task->tk_status) {
6159 case 0:
6160 case -NFS4ERR_COMPLETE_ALREADY:
6161 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6162 break;
6163 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006164 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006165 /* fall through */
6166 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006167 return -EAGAIN;
6168 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006169 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006170 }
6171 return 0;
6172}
6173
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006174static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6175{
6176 struct nfs4_reclaim_complete_data *calldata = data;
6177 struct nfs_client *clp = calldata->clp;
6178 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6179
6180 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006181 if (!nfs41_sequence_done(task, res))
6182 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006183
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006184 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6185 rpc_restart_call_prepare(task);
6186 return;
6187 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006188 dprintk("<-- %s\n", __func__);
6189}
6190
6191static void nfs4_free_reclaim_complete_data(void *data)
6192{
6193 struct nfs4_reclaim_complete_data *calldata = data;
6194
6195 kfree(calldata);
6196}
6197
6198static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6199 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6200 .rpc_call_done = nfs4_reclaim_complete_done,
6201 .rpc_release = nfs4_free_reclaim_complete_data,
6202};
6203
6204/*
6205 * Issue a global reclaim complete.
6206 */
6207static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6208{
6209 struct nfs4_reclaim_complete_data *calldata;
6210 struct rpc_task *task;
6211 struct rpc_message msg = {
6212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6213 };
6214 struct rpc_task_setup task_setup_data = {
6215 .rpc_client = clp->cl_rpcclient,
6216 .rpc_message = &msg,
6217 .callback_ops = &nfs4_reclaim_complete_call_ops,
6218 .flags = RPC_TASK_ASYNC,
6219 };
6220 int status = -ENOMEM;
6221
6222 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006223 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006224 if (calldata == NULL)
6225 goto out;
6226 calldata->clp = clp;
6227 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006228
Trond Myklebust9d12b212012-01-17 22:04:25 -05006229 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006230 msg.rpc_argp = &calldata->arg;
6231 msg.rpc_resp = &calldata->res;
6232 task_setup_data.callback_data = calldata;
6233 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006234 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006235 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006236 goto out;
6237 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006238 status = nfs4_wait_for_completion_rpc_task(task);
6239 if (status == 0)
6240 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006241 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006242 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006243out:
6244 dprintk("<-- %s status=%d\n", __func__, status);
6245 return status;
6246}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006247
6248static void
6249nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6250{
6251 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006252 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006253
6254 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006255 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6256 * right now covering the LAYOUTGET we are about to send.
6257 * However, that is not so catastrophic, and there seems
6258 * to be no way to prevent it completely.
6259 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006260 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006261 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006262 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006263 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6264 NFS_I(lgp->args.inode)->layout,
6265 lgp->args.ctx->state)) {
6266 rpc_exit(task, NFS4_OK);
6267 return;
6268 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006269 rpc_call_start(task);
6270}
6271
6272static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6273{
6274 struct nfs4_layoutget *lgp = calldata;
6275 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6276
6277 dprintk("--> %s\n", __func__);
6278
6279 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6280 return;
6281
6282 switch (task->tk_status) {
6283 case 0:
6284 break;
6285 case -NFS4ERR_LAYOUTTRYLATER:
6286 case -NFS4ERR_RECALLCONFLICT:
6287 task->tk_status = -NFS4ERR_DELAY;
6288 /* Fall through */
6289 default:
6290 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6291 rpc_restart_call_prepare(task);
6292 return;
6293 }
6294 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006295 dprintk("<-- %s\n", __func__);
6296}
6297
Idan Kedar85541162012-08-02 11:47:10 +03006298static size_t max_response_pages(struct nfs_server *server)
6299{
6300 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6301 return nfs_page_array_len(0, max_resp_sz);
6302}
6303
6304static void nfs4_free_pages(struct page **pages, size_t size)
6305{
6306 int i;
6307
6308 if (!pages)
6309 return;
6310
6311 for (i = 0; i < size; i++) {
6312 if (!pages[i])
6313 break;
6314 __free_page(pages[i]);
6315 }
6316 kfree(pages);
6317}
6318
6319static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6320{
6321 struct page **pages;
6322 int i;
6323
6324 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6325 if (!pages) {
6326 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6327 return NULL;
6328 }
6329
6330 for (i = 0; i < size; i++) {
6331 pages[i] = alloc_page(gfp_flags);
6332 if (!pages[i]) {
6333 dprintk("%s: failed to allocate page\n", __func__);
6334 nfs4_free_pages(pages, size);
6335 return NULL;
6336 }
6337 }
6338
6339 return pages;
6340}
6341
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006342static void nfs4_layoutget_release(void *calldata)
6343{
6344 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006345 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6346 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006347
6348 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006349 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006350 put_nfs_open_context(lgp->args.ctx);
6351 kfree(calldata);
6352 dprintk("<-- %s\n", __func__);
6353}
6354
6355static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6356 .rpc_call_prepare = nfs4_layoutget_prepare,
6357 .rpc_call_done = nfs4_layoutget_done,
6358 .rpc_release = nfs4_layoutget_release,
6359};
6360
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006361struct pnfs_layout_segment *
6362nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006363{
6364 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006365 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006366 struct rpc_task *task;
6367 struct rpc_message msg = {
6368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6369 .rpc_argp = &lgp->args,
6370 .rpc_resp = &lgp->res,
6371 };
6372 struct rpc_task_setup task_setup_data = {
6373 .rpc_client = server->client,
6374 .rpc_message = &msg,
6375 .callback_ops = &nfs4_layoutget_call_ops,
6376 .callback_data = lgp,
6377 .flags = RPC_TASK_ASYNC,
6378 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006379 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006380 int status = 0;
6381
6382 dprintk("--> %s\n", __func__);
6383
Idan Kedar85541162012-08-02 11:47:10 +03006384 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6385 if (!lgp->args.layout.pages) {
6386 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006387 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006388 }
6389 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6390
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006391 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006392 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006393 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006394 task = rpc_run_task(&task_setup_data);
6395 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006396 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006397 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006398 if (status == 0)
6399 status = task->tk_status;
6400 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006401 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006402 rpc_put_task(task);
6403 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006404 if (status)
6405 return ERR_PTR(status);
6406 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006407}
6408
Benny Halevycbe82602011-05-22 19:52:37 +03006409static void
6410nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6411{
6412 struct nfs4_layoutreturn *lrp = calldata;
6413
6414 dprintk("--> %s\n", __func__);
6415 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006416 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006417 return;
6418 rpc_call_start(task);
6419}
6420
6421static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6422{
6423 struct nfs4_layoutreturn *lrp = calldata;
6424 struct nfs_server *server;
6425
6426 dprintk("--> %s\n", __func__);
6427
6428 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6429 return;
6430
6431 server = NFS_SERVER(lrp->args.inode);
6432 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006433 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006434 return;
6435 }
Benny Halevycbe82602011-05-22 19:52:37 +03006436 dprintk("<-- %s\n", __func__);
6437}
6438
6439static void nfs4_layoutreturn_release(void *calldata)
6440{
6441 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006442 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006443
6444 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006445 spin_lock(&lo->plh_inode->i_lock);
6446 if (lrp->res.lrs_present)
6447 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6448 lo->plh_block_lgets--;
6449 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006450 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006451 kfree(calldata);
6452 dprintk("<-- %s\n", __func__);
6453}
6454
6455static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6456 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6457 .rpc_call_done = nfs4_layoutreturn_done,
6458 .rpc_release = nfs4_layoutreturn_release,
6459};
6460
6461int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6462{
6463 struct rpc_task *task;
6464 struct rpc_message msg = {
6465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6466 .rpc_argp = &lrp->args,
6467 .rpc_resp = &lrp->res,
6468 };
6469 struct rpc_task_setup task_setup_data = {
6470 .rpc_client = lrp->clp->cl_rpcclient,
6471 .rpc_message = &msg,
6472 .callback_ops = &nfs4_layoutreturn_call_ops,
6473 .callback_data = lrp,
6474 };
6475 int status;
6476
6477 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006478 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006479 task = rpc_run_task(&task_setup_data);
6480 if (IS_ERR(task))
6481 return PTR_ERR(task);
6482 status = task->tk_status;
6483 dprintk("<-- %s status=%d\n", __func__, status);
6484 rpc_put_task(task);
6485 return status;
6486}
6487
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006488/*
6489 * Retrieve the list of Data Server devices from the MDS.
6490 */
6491static int _nfs4_getdevicelist(struct nfs_server *server,
6492 const struct nfs_fh *fh,
6493 struct pnfs_devicelist *devlist)
6494{
6495 struct nfs4_getdevicelist_args args = {
6496 .fh = fh,
6497 .layoutclass = server->pnfs_curr_ld->id,
6498 };
6499 struct nfs4_getdevicelist_res res = {
6500 .devlist = devlist,
6501 };
6502 struct rpc_message msg = {
6503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6504 .rpc_argp = &args,
6505 .rpc_resp = &res,
6506 };
6507 int status;
6508
6509 dprintk("--> %s\n", __func__);
6510 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6511 &res.seq_res, 0);
6512 dprintk("<-- %s status=%d\n", __func__, status);
6513 return status;
6514}
6515
6516int nfs4_proc_getdevicelist(struct nfs_server *server,
6517 const struct nfs_fh *fh,
6518 struct pnfs_devicelist *devlist)
6519{
6520 struct nfs4_exception exception = { };
6521 int err;
6522
6523 do {
6524 err = nfs4_handle_exception(server,
6525 _nfs4_getdevicelist(server, fh, devlist),
6526 &exception);
6527 } while (exception.retry);
6528
6529 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6530 err, devlist->num_devs);
6531
6532 return err;
6533}
6534EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6535
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006536static int
6537_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6538{
6539 struct nfs4_getdeviceinfo_args args = {
6540 .pdev = pdev,
6541 };
6542 struct nfs4_getdeviceinfo_res res = {
6543 .pdev = pdev,
6544 };
6545 struct rpc_message msg = {
6546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6547 .rpc_argp = &args,
6548 .rpc_resp = &res,
6549 };
6550 int status;
6551
6552 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006553 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006554 dprintk("<-- %s status=%d\n", __func__, status);
6555
6556 return status;
6557}
6558
6559int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6560{
6561 struct nfs4_exception exception = { };
6562 int err;
6563
6564 do {
6565 err = nfs4_handle_exception(server,
6566 _nfs4_proc_getdeviceinfo(server, pdev),
6567 &exception);
6568 } while (exception.retry);
6569 return err;
6570}
6571EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6572
Andy Adamson863a3c62011-03-23 13:27:54 +00006573static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6574{
6575 struct nfs4_layoutcommit_data *data = calldata;
6576 struct nfs_server *server = NFS_SERVER(data->args.inode);
6577
6578 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006579 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006580 return;
6581 rpc_call_start(task);
6582}
6583
6584static void
6585nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6586{
6587 struct nfs4_layoutcommit_data *data = calldata;
6588 struct nfs_server *server = NFS_SERVER(data->args.inode);
6589
6590 if (!nfs4_sequence_done(task, &data->res.seq_res))
6591 return;
6592
6593 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006594 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6595 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6596 case -NFS4ERR_BADLAYOUT: /* no layout */
6597 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006598 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006599 break;
6600 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006601 nfs_post_op_update_inode_force_wcc(data->args.inode,
6602 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006603 break;
6604 default:
6605 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6606 rpc_restart_call_prepare(task);
6607 return;
6608 }
6609 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006610}
6611
6612static void nfs4_layoutcommit_release(void *calldata)
6613{
6614 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006615 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006616 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006617
Andy Adamsondb29c082011-07-30 20:52:38 -04006618 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006619 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006620 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6621 list_del_init(&lseg->pls_lc_list);
6622 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6623 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006624 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006625 }
Peng Tao92407e72011-10-23 20:21:17 -07006626
6627 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6628 smp_mb__after_clear_bit();
6629 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6630
Andy Adamson863a3c62011-03-23 13:27:54 +00006631 put_rpccred(data->cred);
6632 kfree(data);
6633}
6634
6635static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6636 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6637 .rpc_call_done = nfs4_layoutcommit_done,
6638 .rpc_release = nfs4_layoutcommit_release,
6639};
6640
6641int
Andy Adamsonef311532011-03-12 02:58:10 -05006642nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006643{
6644 struct rpc_message msg = {
6645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6646 .rpc_argp = &data->args,
6647 .rpc_resp = &data->res,
6648 .rpc_cred = data->cred,
6649 };
6650 struct rpc_task_setup task_setup_data = {
6651 .task = &data->task,
6652 .rpc_client = NFS_CLIENT(data->args.inode),
6653 .rpc_message = &msg,
6654 .callback_ops = &nfs4_layoutcommit_ops,
6655 .callback_data = data,
6656 .flags = RPC_TASK_ASYNC,
6657 };
6658 struct rpc_task *task;
6659 int status = 0;
6660
6661 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6662 "lbw: %llu inode %lu\n",
6663 data->task.tk_pid, sync,
6664 data->args.lastbytewritten,
6665 data->args.inode->i_ino);
6666
Trond Myklebust9d12b212012-01-17 22:04:25 -05006667 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006668 task = rpc_run_task(&task_setup_data);
6669 if (IS_ERR(task))
6670 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006671 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006672 goto out;
6673 status = nfs4_wait_for_completion_rpc_task(task);
6674 if (status != 0)
6675 goto out;
6676 status = task->tk_status;
6677out:
6678 dprintk("%s: status %d\n", __func__, status);
6679 rpc_put_task(task);
6680 return status;
6681}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006682
6683static int
6684_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6685 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6686{
6687 struct nfs41_secinfo_no_name_args args = {
6688 .style = SECINFO_STYLE_CURRENT_FH,
6689 };
6690 struct nfs4_secinfo_res res = {
6691 .flavors = flavors,
6692 };
6693 struct rpc_message msg = {
6694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6695 .rpc_argp = &args,
6696 .rpc_resp = &res,
6697 };
6698 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6699}
6700
6701static int
6702nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6703 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6704{
6705 struct nfs4_exception exception = { };
6706 int err;
6707 do {
6708 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6709 switch (err) {
6710 case 0:
6711 case -NFS4ERR_WRONGSEC:
6712 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006713 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006714 default:
6715 err = nfs4_handle_exception(server, err, &exception);
6716 }
6717 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006718out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006719 return err;
6720}
6721
6722static int
6723nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6724 struct nfs_fsinfo *info)
6725{
6726 int err;
6727 struct page *page;
6728 rpc_authflavor_t flavor;
6729 struct nfs4_secinfo_flavors *flavors;
6730
6731 page = alloc_page(GFP_KERNEL);
6732 if (!page) {
6733 err = -ENOMEM;
6734 goto out;
6735 }
6736
6737 flavors = page_address(page);
6738 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6739
6740 /*
6741 * Fall back on "guess and check" method if
6742 * the server doesn't support SECINFO_NO_NAME
6743 */
6744 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6745 err = nfs4_find_root_sec(server, fhandle, info);
6746 goto out_freepage;
6747 }
6748 if (err)
6749 goto out_freepage;
6750
6751 flavor = nfs_find_best_sec(flavors);
6752 if (err == 0)
6753 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6754
6755out_freepage:
6756 put_page(page);
6757 if (err == -EACCES)
6758 return -EPERM;
6759out:
6760 return err;
6761}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006762
6763static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006764{
6765 int status;
6766 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006767 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006768 };
6769 struct nfs41_test_stateid_res res;
6770 struct rpc_message msg = {
6771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6772 .rpc_argp = &args,
6773 .rpc_resp = &res,
6774 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006775
Chuck Lever38527b12012-07-11 16:30:23 -04006776 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006777 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006778 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006779 if (status != NFS_OK) {
6780 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006781 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006782 }
6783 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006784 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006785}
6786
Chuck Lever38527b12012-07-11 16:30:23 -04006787/**
6788 * nfs41_test_stateid - perform a TEST_STATEID operation
6789 *
6790 * @server: server / transport on which to perform the operation
6791 * @stateid: state ID to test
6792 *
6793 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6794 * Otherwise a negative NFS4ERR value is returned if the operation
6795 * failed or the state ID is not currently valid.
6796 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006797static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006798{
6799 struct nfs4_exception exception = { };
6800 int err;
6801 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006802 err = _nfs41_test_stateid(server, stateid);
6803 if (err != -NFS4ERR_DELAY)
6804 break;
6805 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006806 } while (exception.retry);
6807 return err;
6808}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006809
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006810static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006811{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006812 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006813 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006814 };
6815 struct nfs41_free_stateid_res res;
6816 struct rpc_message msg = {
6817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6818 .rpc_argp = &args,
6819 .rpc_resp = &res,
6820 };
Chuck Lever38527b12012-07-11 16:30:23 -04006821 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006822
Chuck Lever38527b12012-07-11 16:30:23 -04006823 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006824 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006825 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006826 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006827 dprintk("NFS reply free_stateid: %d\n", status);
6828 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006829}
6830
Chuck Lever38527b12012-07-11 16:30:23 -04006831/**
6832 * nfs41_free_stateid - perform a FREE_STATEID operation
6833 *
6834 * @server: server / transport on which to perform the operation
6835 * @stateid: state ID to release
6836 *
6837 * Returns NFS_OK if the server freed "stateid". Otherwise a
6838 * negative NFS4ERR value is returned.
6839 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006840static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006841{
6842 struct nfs4_exception exception = { };
6843 int err;
6844 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006845 err = _nfs4_free_stateid(server, stateid);
6846 if (err != -NFS4ERR_DELAY)
6847 break;
6848 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006849 } while (exception.retry);
6850 return err;
6851}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006852
6853static bool nfs41_match_stateid(const nfs4_stateid *s1,
6854 const nfs4_stateid *s2)
6855{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006856 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006857 return false;
6858
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006859 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006860 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006861 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006862 return true;
6863
6864 return false;
6865}
6866
Andy Adamson557134a2009-04-01 09:21:53 -04006867#endif /* CONFIG_NFS_V4_1 */
6868
Trond Myklebust36281ca2012-03-04 18:13:56 -05006869static bool nfs4_match_stateid(const nfs4_stateid *s1,
6870 const nfs4_stateid *s2)
6871{
Trond Myklebustf597c532012-03-04 18:13:56 -05006872 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006873}
6874
6875
Trond Myklebust17280172012-03-11 13:11:00 -04006876static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006877 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006878 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 .recover_open = nfs4_open_reclaim,
6880 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006881 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006882 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006883 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884};
6885
Andy Adamson591d71c2009-04-01 09:22:47 -04006886#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006887static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006888 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6889 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6890 .recover_open = nfs4_open_reclaim,
6891 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006892 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006893 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006894 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006895 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006896};
6897#endif /* CONFIG_NFS_V4_1 */
6898
Trond Myklebust17280172012-03-11 13:11:00 -04006899static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006900 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006901 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 .recover_open = nfs4_open_expired,
6903 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006904 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006905 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906};
6907
Andy Adamson591d71c2009-04-01 09:22:47 -04006908#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006909static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006910 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6911 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006912 .recover_open = nfs41_open_expired,
6913 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006914 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006915 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006916};
6917#endif /* CONFIG_NFS_V4_1 */
6918
Trond Myklebust17280172012-03-11 13:11:00 -04006919static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006920 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006921 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006922 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006923};
6924
6925#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006926static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006927 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006928 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006929 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006930};
6931#endif
6932
Trond Myklebust97dc1352010-06-16 09:52:26 -04006933static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6934 .minor_version = 0,
6935 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006936 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006937 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006938 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6939 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6940 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006941};
6942
6943#if defined(CONFIG_NFS_V4_1)
6944static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6945 .minor_version = 1,
6946 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006947 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006948 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006949 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6950 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6951 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006952};
6953#endif
6954
Trond Myklebust97dc1352010-06-16 09:52:26 -04006955const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6956 [0] = &nfs_v4_0_minor_ops,
6957#if defined(CONFIG_NFS_V4_1)
6958 [1] = &nfs_v4_1_minor_ops,
6959#endif
6960};
6961
Bryan Schumaker73a79702012-07-16 16:39:12 -04006962const struct inode_operations nfs4_dir_inode_operations = {
6963 .create = nfs_create,
6964 .lookup = nfs_lookup,
6965 .atomic_open = nfs_atomic_open,
6966 .link = nfs_link,
6967 .unlink = nfs_unlink,
6968 .symlink = nfs_symlink,
6969 .mkdir = nfs_mkdir,
6970 .rmdir = nfs_rmdir,
6971 .mknod = nfs_mknod,
6972 .rename = nfs_rename,
6973 .permission = nfs_permission,
6974 .getattr = nfs_getattr,
6975 .setattr = nfs_setattr,
6976 .getxattr = generic_getxattr,
6977 .setxattr = generic_setxattr,
6978 .listxattr = generic_listxattr,
6979 .removexattr = generic_removexattr,
6980};
6981
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006982static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006983 .permission = nfs_permission,
6984 .getattr = nfs_getattr,
6985 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006986 .getxattr = generic_getxattr,
6987 .setxattr = generic_setxattr,
6988 .listxattr = generic_listxattr,
6989 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006990};
6991
David Howells509de812006-08-22 20:06:11 -04006992const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 .version = 4, /* protocol version */
6994 .dentry_ops = &nfs4_dentry_operations,
6995 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006996 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006997 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006999 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007000 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 .getattr = nfs4_proc_getattr,
7002 .setattr = nfs4_proc_setattr,
7003 .lookup = nfs4_proc_lookup,
7004 .access = nfs4_proc_access,
7005 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 .create = nfs4_proc_create,
7007 .remove = nfs4_proc_remove,
7008 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007009 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 .unlink_done = nfs4_proc_unlink_done,
7011 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007012 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007013 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007014 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 .link = nfs4_proc_link,
7016 .symlink = nfs4_proc_symlink,
7017 .mkdir = nfs4_proc_mkdir,
7018 .rmdir = nfs4_proc_remove,
7019 .readdir = nfs4_proc_readdir,
7020 .mknod = nfs4_proc_mknod,
7021 .statfs = nfs4_proc_statfs,
7022 .fsinfo = nfs4_proc_fsinfo,
7023 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007024 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 .decode_dirent = nfs4_decode_dirent,
7026 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007027 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007028 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007029 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007031 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007032 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007033 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007035 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007036 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007038 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007039 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007040 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007041 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007042 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007043 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007044 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007045 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007046 .create_server = nfs4_create_server,
7047 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048};
7049
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007050static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7051 .prefix = XATTR_NAME_NFSV4_ACL,
7052 .list = nfs4_xattr_list_nfs4_acl,
7053 .get = nfs4_xattr_get_nfs4_acl,
7054 .set = nfs4_xattr_set_nfs4_acl,
7055};
7056
7057const struct xattr_handler *nfs4_xattr_handlers[] = {
7058 &nfs4_xattr_nfs4_acl_handler,
7059 NULL
7060};
7061
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062/*
7063 * Local variables:
7064 * c-basic-offset: 8
7065 * End:
7066 */