blob: 6c41a34e34b4f227238c0949f4fc661b99d8f60c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010058#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebust4ce79712005-06-22 17:16:21 +000060#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050062#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050063#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040064#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040065#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040066#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustcdd4e682006-01-03 09:55:12 +010075struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010076static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080077static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050079static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050080static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040081static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040082static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040083static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050091static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Trond Myklebust52567b02009-10-23 14:46:42 -040093 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
97 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000098 case -NFS4ERR_WRONGSEC:
99 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
102 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400103 case -NFS4ERR_SHARE_DENIED:
104 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400107 case -NFS4ERR_ACCESS:
108 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700111 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400114 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/*
118 * This is our standard bitmap for GETATTR requests.
119 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400120const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 FATTR4_WORD0_TYPE
122 | FATTR4_WORD0_CHANGE
123 | FATTR4_WORD0_SIZE
124 | FATTR4_WORD0_FSID
125 | FATTR4_WORD0_FILEID,
126 FATTR4_WORD1_MODE
127 | FATTR4_WORD1_NUMLINKS
128 | FATTR4_WORD1_OWNER
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
135};
136
Trond Myklebust1549210f2012-06-05 09:16:47 -0400137static const u32 nfs4_pnfs_open_bitmap[3] = {
138 FATTR4_WORD0_TYPE
139 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_SIZE
141 | FATTR4_WORD0_FSID
142 | FATTR4_WORD0_FILEID,
143 FATTR4_WORD1_MODE
144 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
153};
154
Andy Adamsone23008e2012-10-02 21:07:32 -0400155static const u32 nfs4_open_noattr_bitmap[3] = {
156 FATTR4_WORD0_TYPE
157 | FATTR4_WORD0_CHANGE
158 | FATTR4_WORD0_FILEID,
159};
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161const u32 nfs4_statfs_bitmap[2] = {
162 FATTR4_WORD0_FILES_AVAIL
163 | FATTR4_WORD0_FILES_FREE
164 | FATTR4_WORD0_FILES_TOTAL,
165 FATTR4_WORD1_SPACE_AVAIL
166 | FATTR4_WORD1_SPACE_FREE
167 | FATTR4_WORD1_SPACE_TOTAL
168};
169
Trond Myklebust4ce79712005-06-22 17:16:21 +0000170const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 FATTR4_WORD0_MAXLINK
172 | FATTR4_WORD0_MAXNAME,
173 0
174};
175
Fred Isamandae100c2011-07-30 20:52:37 -0400176const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 | FATTR4_WORD0_MAXREAD
178 | FATTR4_WORD0_MAXWRITE
179 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700180 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400181 | FATTR4_WORD1_FS_LAYOUT_TYPES,
182 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184
Manoj Naik830b8e32006-06-09 09:34:25 -0400185const u32 nfs4_fs_locations_bitmap[2] = {
186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID
191 | FATTR4_WORD0_FS_LOCATIONS,
192 FATTR4_WORD1_MODE
193 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER
195 | FATTR4_WORD1_OWNER_GROUP
196 | FATTR4_WORD1_RAWDEV
197 | FATTR4_WORD1_SPACE_USED
198 | FATTR4_WORD1_TIME_ACCESS
199 | FATTR4_WORD1_TIME_METADATA
200 | FATTR4_WORD1_TIME_MODIFY
201 | FATTR4_WORD1_MOUNTED_ON_FILEID
202};
203
Al Virobc4785c2006-10-19 23:28:51 -0700204static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct nfs4_readdir_arg *readdir)
206{
Al Viro0dbb4c62006-10-19 23:28:49 -0700207 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000210 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
212 return;
213 }
214
215 readdir->cookie = 0;
216 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
217 if (cookie == 2)
218 return;
219
220 /*
221 * NFSv4 servers do not return entries for '.' and '..'
222 * Therefore, we fake these entries here. We let '.'
223 * have cookie 0 and '..' have cookie 1. Note that
224 * when talking to the server, we always send cookie 0
225 * instead of 1 or 2.
226 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800227 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (cookie == 0) {
230 *p++ = xdr_one; /* next */
231 *p++ = xdr_zero; /* cookie, first word */
232 *p++ = xdr_one; /* cookie, second word */
233 *p++ = xdr_one; /* entry len */
234 memcpy(p, ".\0\0\0", 4); /* entry */
235 p++;
236 *p++ = xdr_one; /* bitmap length */
237 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
238 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400239 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
242 *p++ = xdr_one; /* next */
243 *p++ = xdr_zero; /* cookie, first word */
244 *p++ = xdr_two; /* cookie, second word */
245 *p++ = xdr_two; /* entry len */
246 memcpy(p, "..\0\0", 4); /* entry */
247 p++;
248 *p++ = xdr_one; /* bitmap length */
249 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
250 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400251 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 readdir->pgbase = (char *)p - (char *)start;
254 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800255 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Trond Myklebust65de8722008-12-23 15:21:44 -0500258static int nfs4_wait_clnt_recover(struct nfs_client *clp)
259{
260 int res;
261
262 might_sleep();
263
Trond Myklebuste005e802008-12-23 15:21:48 -0500264 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400265 nfs_wait_bit_killable, TASK_KILLABLE);
Chuck Lever46a87b82012-07-11 16:30:41 -0400266 if (res)
267 return res;
268
269 if (clp->cl_cons_state < 0)
270 return clp->cl_cons_state;
271 return 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500272}
273
274static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
275{
276 int res = 0;
277
278 might_sleep();
279
280 if (*timeout <= 0)
281 *timeout = NFS4_POLL_RETRY_MIN;
282 if (*timeout > NFS4_POLL_RETRY_MAX)
283 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100284 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500285 if (fatal_signal_pending(current))
286 res = -ERESTARTSYS;
287 *timeout <<= 1;
288 return res;
289}
290
291/* This is the error handling routine for processes that are allowed
292 * to sleep.
293 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800294static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500295{
296 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500297 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500298 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500299 int ret = errorcode;
300
301 exception->retry = 0;
302 switch(errorcode) {
303 case 0:
304 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500305 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400306 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400307 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500308 exception->retry = 1;
309 return 0;
310 }
311 if (state == NULL)
312 break;
313 nfs4_schedule_stateid_recovery(server, state);
314 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500315 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500316 case -NFS4ERR_ADMIN_REVOKED:
317 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500318 if (state == NULL)
319 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400320 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500321 nfs4_schedule_stateid_recovery(server, state);
322 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400323 case -NFS4ERR_EXPIRED:
324 if (state != NULL)
325 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500327 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500328 nfs4_schedule_lease_recovery(clp);
329 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500330#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400331 case -NFS4ERR_BADSESSION:
332 case -NFS4ERR_BADSLOT:
333 case -NFS4ERR_BAD_HIGH_SLOT:
334 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
335 case -NFS4ERR_DEADSESSION:
336 case -NFS4ERR_SEQ_FALSE_RETRY:
337 case -NFS4ERR_SEQ_MISORDERED:
338 dprintk("%s ERROR: %d Reset session\n", __func__,
339 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400340 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400341 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500342#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500343 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500344 if (exception->timeout > HZ) {
345 /* We have retried a decent amount, time to
346 * fail
347 */
348 ret = -EBUSY;
349 break;
350 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500351 case -NFS4ERR_GRACE:
352 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500353 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500354 ret = nfs4_delay(server->client, &exception->timeout);
355 if (ret != 0)
356 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400357 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500358 case -NFS4ERR_OLD_STATEID:
359 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800360 break;
361 case -NFS4ERR_BADOWNER:
362 /* The following works around a Linux server bug! */
363 case -NFS4ERR_BADNAME:
364 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
365 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
366 exception->retry = 1;
367 printk(KERN_WARNING "NFS: v4 server %s "
368 "does not accept raw "
369 "uid/gids. "
370 "Reenabling the idmapper.\n",
371 server->nfs_client->cl_hostname);
372 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500373 }
374 /* We failed to handle the error */
375 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500376wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500377 ret = nfs4_wait_clnt_recover(clp);
378 if (ret == 0)
379 exception->retry = 1;
380 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500381}
382
383
Trond Myklebust452e9352010-07-31 14:29:06 -0400384static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_lock(&clp->cl_lock);
387 if (time_before(clp->cl_last_renewal,timestamp))
388 clp->cl_last_renewal = timestamp;
389 spin_unlock(&clp->cl_lock);
390}
391
Trond Myklebust452e9352010-07-31 14:29:06 -0400392static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
393{
394 do_renew_lease(server->nfs_client, timestamp);
395}
396
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400397#if defined(CONFIG_NFS_V4_1)
398
Benny Halevy510b8172009-04-01 09:22:14 -0400399/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400400 * nfs4_free_slot - free a slot and efficiently update slot table.
401 *
402 * freeing a slot is trivially done by clearing its respective bit
403 * in the bitmap.
404 * If the freed slotid equals highest_used_slotid we want to update it
405 * so that the server would be able to size down the slot table if needed,
406 * otherwise we know that the highest_used_slotid is still in use.
407 * When updating highest_used_slotid there may be "holes" in the bitmap
408 * so we need to scan down from highest_used_slotid to 0 looking for the now
409 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500410 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500411 *
412 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400413 */
414static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500415nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400416{
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400417 /* clear used bit in bitmap */
418 __clear_bit(slotid, tbl->used_slots);
419
420 /* update highest_used_slotid when it is freed */
421 if (slotid == tbl->highest_used_slotid) {
422 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500423 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400424 tbl->highest_used_slotid = slotid;
425 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500426 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400427 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500428 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
429 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400430}
431
Trond Myklebust961a8282012-01-17 22:57:37 -0500432bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
433{
434 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
435 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400436}
437
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800438/*
Andy Adamson42acd022011-01-06 02:04:34 +0000439 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800440 */
Andy Adamson42acd022011-01-06 02:04:34 +0000441static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800442{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400443 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500444 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
445 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800446 return;
447 }
448
Trond Myklebust45d43c22012-02-06 19:38:51 -0500449 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800450 return;
451
Andy Adamson42acd022011-01-06 02:04:34 +0000452 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
453 complete(&ses->fc_slot_table.complete);
454}
455
456/*
457 * Signal state manager thread if session back channel is drained
458 */
459void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
460{
461 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500462 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000463 return;
464 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
465 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800466}
467
Trond Myklebustd185a332010-06-16 09:52:25 -0400468static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400469{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500470 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400471 struct nfs4_slot_table *tbl;
472
Benny Halevydfb4f3092010-09-24 09:17:01 -0400473 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400474 /* just wake up the next guy waiting since
475 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500476 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500477 return;
Andy Adamson13615872009-04-01 09:22:17 -0400478 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500479 tbl = res->sr_slot->table;
480 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500481
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500482 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500483 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500484 nfs4_check_drain_fc_complete(session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500485 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400486 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400487}
488
Trond Myklebust14516c32010-07-31 14:29:06 -0400489static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400490{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500491 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500492 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400493 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400494 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400495
496 /*
497 * sr_status remains 1 if an RPC level error occurred. The server
498 * may or may not have processed the sequence operation..
499 * Proceed as if the server received and processed the sequence
500 * operation.
501 */
502 if (res->sr_status == 1)
503 res->sr_status = NFS_OK;
504
Bryan Schumaker468f8612011-04-18 15:57:32 -0400505 /* don't increment the sequence number if the task wasn't sent */
506 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400507 goto out;
508
Trond Myklebust933602e2012-11-16 12:12:38 -0500509 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500510 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500511
Andy Adamson691daf32009-12-04 15:55:39 -0500512 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 switch (res->sr_status) {
514 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400515 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500516 ++slot->seq_nr;
517 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500518 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400519 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500520 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500521 if (res->sr_status_flags != 0)
522 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400523 break;
524 case -NFS4ERR_DELAY:
525 /* The server detected a resend of the RPC call and
526 * returned NFS4ERR_DELAY as per Section 2.10.6.2
527 * of RFC5661.
528 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500529 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400530 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500531 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500532 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400533 goto out_retry;
534 default:
535 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500536 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400537 }
538out:
539 /* The session may be reset by one of the error handlers. */
540 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400541 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400542 return 1;
543out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400544 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400545 goto out;
546 rpc_delay(task, NFS4_POLL_RETRY_MAX);
547 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400548}
549
Trond Myklebust14516c32010-07-31 14:29:06 -0400550static int nfs4_sequence_done(struct rpc_task *task,
551 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400552{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500553 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400554 return 1;
555 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400556}
557
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400558/*
Benny Halevy510b8172009-04-01 09:22:14 -0400559 * nfs4_find_slot - efficiently look for a free slot
560 *
561 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
562 * If found, we mark the slot as used, update the highest_used_slotid,
563 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500564 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400565 *
566 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400567 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500568static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800569nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400570{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500571 u32 slotid;
572 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400573
Trond Myklebust45d43c22012-02-06 19:38:51 -0500574 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400575 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
576 tbl->max_slots);
577 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
578 if (slotid >= tbl->max_slots)
579 goto out;
580 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500581 if (slotid > tbl->highest_used_slotid ||
582 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400583 tbl->highest_used_slotid = slotid;
584 ret_id = slotid;
585out:
586 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
587 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400588 return ret_id;
589}
590
Trond Myklebust9d12b212012-01-17 22:04:25 -0500591static void nfs41_init_sequence(struct nfs4_sequence_args *args,
592 struct nfs4_sequence_res *res, int cache_reply)
593{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500594 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500595 args->sa_cache_this = 0;
596 if (cache_reply)
597 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500598 res->sr_slot = NULL;
599}
600
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000601int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 struct nfs4_sequence_args *args,
603 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 struct rpc_task *task)
605{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400606 struct nfs4_slot *slot;
607 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500608 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400609
610 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400612 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613 return 0;
614
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400615 tbl = &session->fc_slot_table;
616
617 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400618 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800619 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500620 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500621 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400622 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500623 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500624 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400625 }
626
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800627 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
628 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
629 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
630 spin_unlock(&tbl->slot_tbl_lock);
631 dprintk("%s enforce FIFO order\n", __func__);
632 return -EAGAIN;
633 }
634
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800635 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500636 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400637 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
638 spin_unlock(&tbl->slot_tbl_lock);
639 dprintk("<-- %s: no free slots\n", __func__);
640 return -EAGAIN;
641 }
642 spin_unlock(&tbl->slot_tbl_lock);
643
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800644 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400645 slot = tbl->slots + slotid;
Trond Myklebust933602e2012-11-16 12:12:38 -0500646 slot->renewal_time = jiffies;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500647
648 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400649
650 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
651
Benny Halevydfb4f3092010-09-24 09:17:01 -0400652 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400653 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400654 /*
655 * sr_status is only set in decode_sequence, and so will remain
656 * set to 1 if an rpc level failure occurs.
657 */
658 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659 return 0;
660}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000661EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662
Trond Myklebust035168a2010-06-16 09:52:26 -0400663int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 struct nfs4_sequence_args *args,
665 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666 struct rpc_task *task)
667{
Trond Myklebust035168a2010-06-16 09:52:26 -0400668 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669 int ret = 0;
670
Trond Myklebust9d12b212012-01-17 22:04:25 -0500671 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400672 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400673
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500674 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400675 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500676 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400677
Trond Myklebust9d12b212012-01-17 22:04:25 -0500678 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679out:
680 dprintk("<-- %s status=%d\n", __func__, ret);
681 return ret;
682}
683
684struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400685 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 struct nfs4_sequence_args *seq_args;
687 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400688};
689
690static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
691{
692 struct nfs41_call_sync_data *data = calldata;
693
Trond Myklebust035168a2010-06-16 09:52:26 -0400694 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
695
696 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500697 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400698 return;
699 rpc_call_start(task);
700}
701
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800702static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
703{
704 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
705 nfs41_call_sync_prepare(task, calldata);
706}
707
Andy Adamson69ab40c2009-04-01 09:22:19 -0400708static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
709{
710 struct nfs41_call_sync_data *data = calldata;
711
Trond Myklebust14516c32010-07-31 14:29:06 -0400712 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400713}
714
Trond Myklebust17280172012-03-11 13:11:00 -0400715static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400716 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400717 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400718};
719
Trond Myklebust17280172012-03-11 13:11:00 -0400720static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800721 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
722 .rpc_call_done = nfs41_call_sync_done,
723};
724
Bryan Schumaker7c513052011-03-24 17:12:24 +0000725static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
726 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400727 struct rpc_message *msg,
728 struct nfs4_sequence_args *args,
729 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800730 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400731{
732 int ret;
733 struct rpc_task *task;
734 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400735 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400736 .seq_args = args,
737 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400738 };
739 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000740 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400741 .rpc_message = msg,
742 .callback_ops = &nfs41_call_sync_ops,
743 .callback_data = &data
744 };
745
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800746 if (privileged)
747 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400748 task = rpc_run_task(&task_setup);
749 if (IS_ERR(task))
750 ret = PTR_ERR(task);
751 else {
752 ret = task->tk_status;
753 rpc_put_task(task);
754 }
755 return ret;
756}
757
Bryan Schumaker7c513052011-03-24 17:12:24 +0000758int _nfs4_call_sync_session(struct rpc_clnt *clnt,
759 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400760 struct rpc_message *msg,
761 struct nfs4_sequence_args *args,
762 struct nfs4_sequence_res *res,
763 int cache_reply)
764{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500765 nfs41_init_sequence(args, res, cache_reply);
766 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400767}
768
Trond Myklebustdf896452010-06-16 09:52:26 -0400769#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500770static inline
771void nfs41_init_sequence(struct nfs4_sequence_args *args,
772 struct nfs4_sequence_res *res, int cache_reply)
773{
774}
775
Trond Myklebust14516c32010-07-31 14:29:06 -0400776static int nfs4_sequence_done(struct rpc_task *task,
777 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400778{
Trond Myklebust14516c32010-07-31 14:29:06 -0400779 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400780}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400781#endif /* CONFIG_NFS_V4_1 */
782
Bryan Schumaker7c513052011-03-24 17:12:24 +0000783int _nfs4_call_sync(struct rpc_clnt *clnt,
784 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400785 struct rpc_message *msg,
786 struct nfs4_sequence_args *args,
787 struct nfs4_sequence_res *res,
788 int cache_reply)
789{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500790 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000791 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400792}
793
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000794static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000795int nfs4_call_sync(struct rpc_clnt *clnt,
796 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000797 struct rpc_message *msg,
798 struct nfs4_sequence_args *args,
799 struct nfs4_sequence_res *res,
800 int cache_reply)
801{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000802 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
803 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000804}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400805
Trond Myklebust38478b22006-05-25 01:40:57 -0400806static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Trond Myklebust38478b22006-05-25 01:40:57 -0400808 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Trond Myklebust38478b22006-05-25 01:40:57 -0400810 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400811 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700812 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400813 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700814 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400815 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400819 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 struct nfs_openargs o_arg;
821 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100822 struct nfs_open_confirmargs c_arg;
823 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500824 struct nfs4_string owner_name;
825 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400828 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400830 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100832 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400833 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100834 int rpc_status;
835 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100836};
837
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400838
839static void nfs4_init_opendata_res(struct nfs4_opendata *p)
840{
841 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400842 p->o_res.seqid = p->o_arg.seqid;
843 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400844 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400845 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400846 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500847 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400848}
849
Al Viro82a2c1b2011-06-22 18:30:55 -0400850static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400852 const struct iattr *attrs,
853 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854{
Al Viro82a2c1b2011-06-22 18:30:55 -0400855 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100856 struct inode *dir = parent->d_inode;
857 struct nfs_server *server = NFS_SERVER(dir);
858 struct nfs4_opendata *p;
859
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400860 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100861 if (p == NULL)
862 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400863 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100864 if (p->o_arg.seqid == NULL)
865 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400866 nfs_sb_active(dentry->d_sb);
867 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100868 p->dir = parent;
869 p->owner = sp;
870 atomic_inc(&sp->so_count);
871 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872 p->o_arg.open_flags = flags;
873 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700874 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
875 * will return permission denied for all bits until close */
876 if (!(flags & O_EXCL)) {
877 /* ask server to check for all possible rights as results
878 * are cached */
879 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
880 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700881 }
David Howells7539bba2006-08-22 20:06:09 -0400882 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400883 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
884 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400885 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886 p->o_arg.server = server;
887 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400888 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100889 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500890 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500891 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400892
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893 p->o_arg.u.attrs = &p->attrs;
894 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500895
896 verf[0] = jiffies;
897 verf[1] = current->pid;
898 memcpy(p->o_arg.u.verifier.data, verf,
899 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100900 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100901 p->c_arg.fh = &p->o_res.fh;
902 p->c_arg.stateid = &p->o_res.stateid;
903 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400904 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400905 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100906 return p;
907err_free:
908 kfree(p);
909err:
910 dput(parent);
911 return NULL;
912}
913
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400914static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100915{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400916 struct nfs4_opendata *p = container_of(kref,
917 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400918 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400919
920 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400921 if (p->state != NULL)
922 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400923 nfs4_put_state_owner(p->owner);
924 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400925 dput(p->dentry);
926 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500927 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400928 kfree(p);
929}
930
931static void nfs4_opendata_put(struct nfs4_opendata *p)
932{
933 if (p != NULL)
934 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100935}
936
Trond Myklebust06f814a2006-01-03 09:55:07 +0100937static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
938{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100939 int ret;
940
Trond Myklebust06f814a2006-01-03 09:55:07 +0100941 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100942 return ret;
943}
944
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400946{
947 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948
Trond Myklebust536e43d2012-01-17 22:04:26 -0500949 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950 goto out;
951 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400952 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500953 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
954 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400955 break;
956 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500957 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
958 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400959 break;
960 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500961 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
962 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400963 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400965 return ret;
966}
967
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500970 if (delegation == NULL)
971 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500974 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400975 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500976 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400977 return 1;
978}
979
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100981{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500982 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100983 case FMODE_WRITE:
984 state->n_wronly++;
985 break;
986 case FMODE_READ:
987 state->n_rdonly++;
988 break;
989 case FMODE_READ|FMODE_WRITE:
990 state->n_rdwr++;
991 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100993}
994
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400996{
997 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500998 nfs4_stateid_copy(&state->stateid, stateid);
999 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001001 case FMODE_READ:
1002 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1003 break;
1004 case FMODE_WRITE:
1005 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1006 break;
1007 case FMODE_READ|FMODE_WRITE:
1008 set_bit(NFS_O_RDWR_STATE, &state->flags);
1009 }
1010}
1011
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001013{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001014 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001016 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001017}
1018
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019static 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 -07001020{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001021 /*
1022 * Protect the call to nfs4_state_set_mode_locked and
1023 * serialise the stateid update
1024 */
1025 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001026 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001027 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001028 set_bit(NFS_DELEGATED_STATE, &state->flags);
1029 }
1030 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001032 write_sequnlock(&state->seqlock);
1033 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001035 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001038static 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 -05001039{
1040 struct nfs_inode *nfsi = NFS_I(state->inode);
1041 struct nfs_delegation *deleg_cur;
1042 int ret = 0;
1043
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001045
1046 rcu_read_lock();
1047 deleg_cur = rcu_dereference(nfsi->delegation);
1048 if (deleg_cur == NULL)
1049 goto no_delegation;
1050
1051 spin_lock(&deleg_cur->lock);
1052 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001054 goto no_delegation_unlock;
1055
1056 if (delegation == NULL)
1057 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001058 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001059 goto no_delegation_unlock;
1060
Trond Myklebustb7391f42008-12-23 15:21:52 -05001061 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001063 ret = 1;
1064no_delegation_unlock:
1065 spin_unlock(&deleg_cur->lock);
1066no_delegation:
1067 rcu_read_unlock();
1068
1069 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001071 ret = 1;
1072 }
1073
1074 return ret;
1075}
1076
1077
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001078static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001079{
1080 struct nfs_delegation *delegation;
1081
1082 rcu_read_lock();
1083 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 rcu_read_unlock();
1086 return;
1087 }
1088 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001089 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001090}
1091
Trond Myklebust6ee41262007-07-08 14:11:36 -04001092static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001093{
1094 struct nfs4_state *state = opendata->state;
1095 struct nfs_inode *nfsi = NFS_I(state->inode);
1096 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001097 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001098 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099 nfs4_stateid stateid;
1100 int ret = -EAGAIN;
1101
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001103 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001104 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105 if (can_open_cached(state, fmode, open_mode)) {
1106 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001107 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001108 goto out_return_state;
1109 }
1110 spin_unlock(&state->owner->so_lock);
1111 }
Trond Myklebust34310432008-12-23 15:21:38 -05001112 rcu_read_lock();
1113 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001114 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001115 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001116 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001117 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001119 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001120 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001121 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001122 if (ret != 0)
1123 goto out;
1124 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001125
1126 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001127 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001128 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001129 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001130out:
1131 return ERR_PTR(ret);
1132out_return_state:
1133 atomic_inc(&state->count);
1134 return state;
1135}
1136
Andy Adamsone23008e2012-10-02 21:07:32 -04001137static void
1138nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1139{
1140 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1141 struct nfs_delegation *delegation;
1142 int delegation_flags = 0;
1143
1144 rcu_read_lock();
1145 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1146 if (delegation)
1147 delegation_flags = delegation->flags;
1148 rcu_read_unlock();
1149 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1150 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1151 "returning a delegation for "
1152 "OPEN(CLAIM_DELEGATE_CUR)\n",
1153 clp->cl_hostname);
1154 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1155 nfs_inode_set_delegation(state->inode,
1156 data->owner->so_cred,
1157 &data->o_res);
1158 else
1159 nfs_inode_reclaim_delegation(state->inode,
1160 data->owner->so_cred,
1161 &data->o_res);
1162}
1163
1164/*
1165 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1166 * and update the nfs4_state.
1167 */
1168static struct nfs4_state *
1169_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1170{
1171 struct inode *inode = data->state->inode;
1172 struct nfs4_state *state = data->state;
1173 int ret;
1174
1175 if (!data->rpc_done) {
1176 ret = data->rpc_status;
1177 goto err;
1178 }
1179
1180 ret = -ESTALE;
1181 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1182 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1183 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1184 goto err;
1185
1186 ret = -ENOMEM;
1187 state = nfs4_get_open_state(inode, data->owner);
1188 if (state == NULL)
1189 goto err;
1190
1191 ret = nfs_refresh_inode(inode, &data->f_attr);
1192 if (ret)
1193 goto err;
1194
1195 if (data->o_res.delegation_type != 0)
1196 nfs4_opendata_check_deleg(data, state);
1197 update_open_stateid(state, &data->o_res.stateid, NULL,
1198 data->o_arg.fmode);
1199
1200 return state;
1201err:
1202 return ERR_PTR(ret);
1203
1204}
1205
1206static struct nfs4_state *
1207_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001208{
1209 struct inode *inode;
1210 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001211 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001212
Trond Myklebustaac00a82007-07-05 19:02:21 -04001213 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001214 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001215 goto out;
1216 }
1217
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001218 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001219 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001220 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001221 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001222 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001223 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001224 goto err;
1225 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001226 state = nfs4_get_open_state(inode, data->owner);
1227 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001228 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001229 if (data->o_res.delegation_type != 0)
1230 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001231 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001232 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001233 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001234out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001235 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001236err_put_inode:
1237 iput(inode);
1238err:
1239 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001240}
1241
Andy Adamsone23008e2012-10-02 21:07:32 -04001242static struct nfs4_state *
1243nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1244{
1245 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1246 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1247 return _nfs4_opendata_to_nfs4_state(data);
1248}
1249
Trond Myklebust864472e2006-01-03 09:55:15 +01001250static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1251{
1252 struct nfs_inode *nfsi = NFS_I(state->inode);
1253 struct nfs_open_context *ctx;
1254
1255 spin_lock(&state->inode->i_lock);
1256 list_for_each_entry(ctx, &nfsi->open_files, list) {
1257 if (ctx->state != state)
1258 continue;
1259 get_nfs_open_context(ctx);
1260 spin_unlock(&state->inode->i_lock);
1261 return ctx;
1262 }
1263 spin_unlock(&state->inode->i_lock);
1264 return ERR_PTR(-ENOENT);
1265}
1266
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001267static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1268{
1269 struct nfs4_opendata *opendata;
1270
Al Viro3d4ff432011-06-22 18:40:12 -04001271 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001272 if (opendata == NULL)
1273 return ERR_PTR(-ENOMEM);
1274 opendata->state = state;
1275 atomic_inc(&state->count);
1276 return opendata;
1277}
1278
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001279static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001280{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001281 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 int ret;
1283
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001284 opendata->o_arg.open_flags = 0;
1285 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001286 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1287 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1288 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001289 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001290 if (ret != 0)
1291 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001292 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001293 if (IS_ERR(newstate))
1294 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001295 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001296 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 return 0;
1298}
1299
1300static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1301{
Trond Myklebust864472e2006-01-03 09:55:15 +01001302 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 int ret;
1304
1305 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001306 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001307 smp_rmb();
1308 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001309 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001310 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001311 if (ret != 0)
1312 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001313 if (newstate != state)
1314 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001315 }
1316 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001317 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001318 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001319 if (ret != 0)
1320 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001321 if (newstate != state)
1322 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001323 }
1324 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001325 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001326 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001327 if (ret != 0)
1328 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001329 if (newstate != state)
1330 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001331 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001332 /*
1333 * We may have performed cached opens for all three recoveries.
1334 * Check if we need to update the current stateid.
1335 */
1336 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001337 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001338 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001339 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001340 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001341 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001342 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001343 return 0;
1344}
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346/*
1347 * OPEN_RECLAIM:
1348 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001350static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001352 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001353 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001354 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 int status;
1356
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001357 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1358 if (IS_ERR(opendata))
1359 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001360 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1361 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001362 rcu_read_lock();
1363 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001364 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001365 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001366 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001367 opendata->o_arg.u.delegation_type = delegation_type;
1368 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001369 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return status;
1371}
1372
Trond Myklebust539cd032007-06-05 11:46:42 -04001373static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct nfs_server *server = NFS_SERVER(state->inode);
1376 struct nfs4_exception exception = { };
1377 int err;
1378 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001379 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001380 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001381 break;
1382 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 } while (exception.retry);
1384 return err;
1385}
1386
Trond Myklebust864472e2006-01-03 09:55:15 +01001387static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1388{
1389 struct nfs_open_context *ctx;
1390 int ret;
1391
1392 ctx = nfs4_state_find_open_context(state);
1393 if (IS_ERR(ctx))
1394 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001395 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001396 put_nfs_open_context(ctx);
1397 return ret;
1398}
1399
Trond Myklebust13437e12007-07-06 15:10:43 -04001400static 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 -07001401{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001402 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001403 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001405 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1406 if (IS_ERR(opendata))
1407 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001408 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001409 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001410 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001411 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001412 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Trond Myklebust13437e12007-07-06 15:10:43 -04001415int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001418 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 int err;
1420 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001421 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 switch (err) {
1423 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001424 case -ENOENT:
1425 case -ESTALE:
1426 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001427 case -NFS4ERR_BADSESSION:
1428 case -NFS4ERR_BADSLOT:
1429 case -NFS4ERR_BAD_HIGH_SLOT:
1430 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1431 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001432 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001433 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 case -NFS4ERR_STALE_CLIENTID:
1435 case -NFS4ERR_STALE_STATEID:
1436 case -NFS4ERR_EXPIRED:
1437 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001438 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001439 goto out;
1440 case -ERESTARTSYS:
1441 /*
1442 * The show must go on: exit, but mark the
1443 * stateid as needing recovery.
1444 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001445 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001446 case -NFS4ERR_ADMIN_REVOKED:
1447 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001448 nfs_inode_find_state_and_recover(state->inode,
1449 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001450 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001451 case -EKEYEXPIRED:
1452 /*
1453 * User RPCSEC_GSS context has expired.
1454 * We cannot recover this stateid now, so
1455 * skip it and allow recovery thread to
1456 * proceed.
1457 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001458 case -ENOMEM:
1459 err = 0;
1460 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462 err = nfs4_handle_exception(server, err, &exception);
1463 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001464out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return err;
1466}
1467
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001468static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1469{
1470 struct nfs4_opendata *data = calldata;
1471
1472 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001473 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001474 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001475 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001476 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001477 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001478 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001479}
1480
1481static void nfs4_open_confirm_release(void *calldata)
1482{
1483 struct nfs4_opendata *data = calldata;
1484 struct nfs4_state *state = NULL;
1485
1486 /* If this request hasn't been cancelled, do nothing */
1487 if (data->cancelled == 0)
1488 goto out_free;
1489 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001490 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001491 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001492 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001493 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001494 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001495out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001496 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001497}
1498
1499static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001500 .rpc_call_done = nfs4_open_confirm_done,
1501 .rpc_release = nfs4_open_confirm_release,
1502};
1503
1504/*
1505 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1506 */
1507static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1508{
1509 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1510 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001511 struct rpc_message msg = {
1512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1513 .rpc_argp = &data->c_arg,
1514 .rpc_resp = &data->c_res,
1515 .rpc_cred = data->owner->so_cred,
1516 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001517 struct rpc_task_setup task_setup_data = {
1518 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001519 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001520 .callback_ops = &nfs4_open_confirm_ops,
1521 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001522 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001523 .flags = RPC_TASK_ASYNC,
1524 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 int status;
1526
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001527 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001528 data->rpc_done = 0;
1529 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001530 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001531 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001532 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001533 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001534 status = nfs4_wait_for_completion_rpc_task(task);
1535 if (status != 0) {
1536 data->cancelled = 1;
1537 smp_wmb();
1538 } else
1539 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001540 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return status;
1542}
1543
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001546 struct nfs4_opendata *data = calldata;
1547 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001548
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1550 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001551 /*
1552 * Check if we still need to send an OPEN call, or if we can use
1553 * a delegation instead.
1554 */
1555 if (data->state != NULL) {
1556 struct nfs_delegation *delegation;
1557
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001558 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001559 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001560 rcu_read_lock();
1561 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001562 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1563 can_open_delegated(delegation, data->o_arg.fmode))
1564 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001565 rcu_read_unlock();
1566 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001567 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001568 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001569 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001570 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001571 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001572 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1573 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001574 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001575 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001576 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001577 &data->o_res.seq_res,
1578 task) != 0)
1579 nfs_release_seqid(data->o_arg.seqid);
1580 else
1581 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001582 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001583unlock_no_action:
1584 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001585out_no_action:
1586 task->tk_action = NULL;
1587
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001588}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001590static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1591{
1592 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1593 nfs4_open_prepare(task, calldata);
1594}
1595
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001596static void nfs4_open_done(struct rpc_task *task, void *calldata)
1597{
1598 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001600 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001601
Trond Myklebust14516c32010-07-31 14:29:06 -04001602 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1603 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001604
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001605 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001606 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1607 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001608 case S_IFREG:
1609 break;
1610 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001611 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001612 break;
1613 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001614 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001615 break;
1616 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001617 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001618 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001619 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001620 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001621 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1622 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001623 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001624 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001625}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001626
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001627static void nfs4_open_release(void *calldata)
1628{
1629 struct nfs4_opendata *data = calldata;
1630 struct nfs4_state *state = NULL;
1631
1632 /* If this request hasn't been cancelled, do nothing */
1633 if (data->cancelled == 0)
1634 goto out_free;
1635 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001636 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001637 goto out_free;
1638 /* In case we need an open_confirm, no cleanup! */
1639 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1640 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001642 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001643 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001644out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001645 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001646}
1647
1648static const struct rpc_call_ops nfs4_open_ops = {
1649 .rpc_call_prepare = nfs4_open_prepare,
1650 .rpc_call_done = nfs4_open_done,
1651 .rpc_release = nfs4_open_release,
1652};
1653
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001654static const struct rpc_call_ops nfs4_recover_open_ops = {
1655 .rpc_call_prepare = nfs4_recover_open_prepare,
1656 .rpc_call_done = nfs4_open_done,
1657 .rpc_release = nfs4_open_release,
1658};
1659
1660static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001661{
1662 struct inode *dir = data->dir->d_inode;
1663 struct nfs_server *server = NFS_SERVER(dir);
1664 struct nfs_openargs *o_arg = &data->o_arg;
1665 struct nfs_openres *o_res = &data->o_res;
1666 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001667 struct rpc_message msg = {
1668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1669 .rpc_argp = o_arg,
1670 .rpc_resp = o_res,
1671 .rpc_cred = data->owner->so_cred,
1672 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001673 struct rpc_task_setup task_setup_data = {
1674 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001675 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001676 .callback_ops = &nfs4_open_ops,
1677 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001678 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001679 .flags = RPC_TASK_ASYNC,
1680 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001681 int status;
1682
Trond Myklebust9d12b212012-01-17 22:04:25 -05001683 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001684 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001685 data->rpc_done = 0;
1686 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001687 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001688 if (isrecover)
1689 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001690 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001691 if (IS_ERR(task))
1692 return PTR_ERR(task);
1693 status = nfs4_wait_for_completion_rpc_task(task);
1694 if (status != 0) {
1695 data->cancelled = 1;
1696 smp_wmb();
1697 } else
1698 status = data->rpc_status;
1699 rpc_put_task(task);
1700
1701 return status;
1702}
1703
1704static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1705{
1706 struct inode *dir = data->dir->d_inode;
1707 struct nfs_openres *o_res = &data->o_res;
1708 int status;
1709
1710 status = nfs4_run_open_task(data, 1);
1711 if (status != 0 || !data->rpc_done)
1712 return status;
1713
Trond Myklebust6926afd2012-01-07 13:22:46 -05001714 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1715
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001716 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1717 status = _nfs4_proc_open_confirm(data);
1718 if (status != 0)
1719 return status;
1720 }
1721
1722 return status;
1723}
1724
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001725static int nfs4_opendata_access(struct rpc_cred *cred,
1726 struct nfs4_opendata *opendata,
1727 struct nfs4_state *state, fmode_t fmode)
1728{
1729 struct nfs_access_entry cache;
1730 u32 mask;
1731
1732 /* access call failed or for some reason the server doesn't
1733 * support any access modes -- defer access call until later */
1734 if (opendata->o_res.access_supported == 0)
1735 return 0;
1736
1737 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001738 /* don't check MAY_WRITE - a newly created file may not have
1739 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001740 if (fmode & FMODE_READ)
1741 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001742 if (fmode & FMODE_EXEC)
1743 mask |= MAY_EXEC;
1744
1745 cache.cred = cred;
1746 cache.jiffies = jiffies;
1747 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1748 nfs_access_add_cache(state->inode, &cache);
1749
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001750 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001751 return 0;
1752
1753 /* even though OPEN succeeded, access is denied. Close the file */
1754 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001755 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001756}
1757
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001758/*
1759 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1760 */
1761static int _nfs4_proc_open(struct nfs4_opendata *data)
1762{
1763 struct inode *dir = data->dir->d_inode;
1764 struct nfs_server *server = NFS_SERVER(dir);
1765 struct nfs_openargs *o_arg = &data->o_arg;
1766 struct nfs_openres *o_res = &data->o_res;
1767 int status;
1768
1769 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001770 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001772 if (status != 0) {
1773 if (status == -NFS4ERR_BADNAME &&
1774 !(o_arg->open_flags & O_CREAT))
1775 return -ENOENT;
1776 return status;
1777 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001778
Trond Myklebust6926afd2012-01-07 13:22:46 -05001779 nfs_fattr_map_and_free_names(server, &data->f_attr);
1780
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001781 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001782 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001783 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1784 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001786 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001788 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001791 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Andy Adamsond83217c2011-03-01 01:34:17 +00001795static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001796{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001797 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001798 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001799
Trond Myklebusta78cb572009-08-09 15:06:19 -04001800 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001801 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001802 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001803 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001804 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1805 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001806 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001807 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001808 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001809 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001810 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001811}
1812
Andy Adamsond83217c2011-03-01 01:34:17 +00001813static int nfs4_recover_expired_lease(struct nfs_server *server)
1814{
1815 return nfs4_client_recover_expired_lease(server->nfs_client);
1816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818/*
1819 * OPEN_EXPIRED:
1820 * reclaim state on the server after a network partition.
1821 * Assumes caller holds the appropriate lock
1822 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001823static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001825 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001826 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001828 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1829 if (IS_ERR(opendata))
1830 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001831 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001832 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001833 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001834 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001835 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001838static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001839{
Trond Myklebust539cd032007-06-05 11:46:42 -04001840 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001841 struct nfs4_exception exception = { };
1842 int err;
1843
1844 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001845 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001846 switch (err) {
1847 default:
1848 goto out;
1849 case -NFS4ERR_GRACE:
1850 case -NFS4ERR_DELAY:
1851 nfs4_handle_exception(server, err, &exception);
1852 err = 0;
1853 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001854 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001855out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001856 return err;
1857}
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1860{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001862 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Trond Myklebust864472e2006-01-03 09:55:15 +01001864 ctx = nfs4_state_find_open_context(state);
1865 if (IS_ERR(ctx))
1866 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001867 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001868 put_nfs_open_context(ctx);
1869 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001872#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001873static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001874{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001875 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001876 nfs4_stateid *stateid = &state->stateid;
1877 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001878
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001879 /* If a state reset has been done, test_stateid is unneeded */
1880 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1881 return;
1882
1883 status = nfs41_test_stateid(server, stateid);
1884 if (status != NFS_OK) {
1885 /* Free the stateid unless the server explicitly
1886 * informs us the stateid is unrecognized. */
1887 if (status != -NFS4ERR_BAD_STATEID)
1888 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001889 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001890
Bryan Schumaker69388672012-09-26 15:25:52 -04001891 write_seqlock(&state->seqlock);
1892 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1893 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001894 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1895 }
1896}
1897
1898/**
1899 * nfs41_check_open_stateid - possibly free an open stateid
1900 *
1901 * @state: NFSv4 state for an inode
1902 *
1903 * Returns NFS_OK if recovery for this stateid is now finished.
1904 * Otherwise a negative NFS4ERR value is returned.
1905 */
1906static int nfs41_check_open_stateid(struct nfs4_state *state)
1907{
1908 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001909 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001910 int status;
1911
1912 /* If a state reset has been done, test_stateid is unneeded */
1913 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1914 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1915 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1916 return -NFS4ERR_BAD_STATEID;
1917
1918 status = nfs41_test_stateid(server, stateid);
1919 if (status != NFS_OK) {
1920 /* Free the stateid unless the server explicitly
1921 * informs us the stateid is unrecognized. */
1922 if (status != -NFS4ERR_BAD_STATEID)
1923 nfs41_free_stateid(server, stateid);
1924
1925 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1926 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1927 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001928 }
1929 return status;
1930}
1931
1932static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1933{
Chuck Levereb64cf92012-07-11 16:30:05 -04001934 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001935
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001936 nfs41_clear_delegation_stateid(state);
1937 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001938 if (status != NFS_OK)
1939 status = nfs4_open_expired(sp, state);
1940 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001941}
1942#endif
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001945 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1946 * fields corresponding to attributes that were used to store the verifier.
1947 * Make sure we clobber those fields in the later setattr call
1948 */
1949static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1950{
1951 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1952 !(sattr->ia_valid & ATTR_ATIME_SET))
1953 sattr->ia_valid |= ATTR_ATIME;
1954
1955 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1956 !(sattr->ia_valid & ATTR_MTIME_SET))
1957 sattr->ia_valid |= ATTR_MTIME;
1958}
1959
1960/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001961 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
Andy Adamson82be4172012-05-23 05:02:35 -04001963static int _nfs4_do_open(struct inode *dir,
1964 struct dentry *dentry,
1965 fmode_t fmode,
1966 int flags,
1967 struct iattr *sattr,
1968 struct rpc_cred *cred,
1969 struct nfs4_state **res,
1970 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 struct nfs4_state_owner *sp;
1973 struct nfs4_state *state = NULL;
1974 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001975 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001976 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001980 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1981 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1983 goto out_err;
1984 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001985 status = nfs4_recover_expired_lease(server);
1986 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001987 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001988 if (dentry->d_inode != NULL)
1989 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001990 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001991 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001992 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001993 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Andy Adamson82be4172012-05-23 05:02:35 -04001995 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1996 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1997 if (!opendata->f_attr.mdsthreshold)
1998 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001999 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002000 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002001 if (dentry->d_inode != NULL)
2002 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002003
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002004 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002006 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002008 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002009 status = PTR_ERR(state);
2010 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002011 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002012 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002013 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002014
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002015 status = nfs4_opendata_access(cred, opendata, state, fmode);
2016 if (status != 0)
2017 goto err_opendata_put;
2018
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002019 if (opendata->o_arg.open_flags & O_EXCL) {
2020 nfs4_exclusive_attrset(opendata, sattr);
2021
2022 nfs_fattr_init(opendata->o_res.f_attr);
2023 status = nfs4_do_setattr(state->inode, cred,
2024 opendata->o_res.f_attr, sattr,
2025 state);
2026 if (status == 0)
2027 nfs_setattr_update_inode(state->inode, sattr);
2028 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2029 }
Andy Adamson82be4172012-05-23 05:02:35 -04002030
2031 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2032 *ctx_th = opendata->f_attr.mdsthreshold;
2033 else
2034 kfree(opendata->f_attr.mdsthreshold);
2035 opendata->f_attr.mdsthreshold = NULL;
2036
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002037 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 *res = state;
2040 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002041err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002042 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002043 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002044err_put_state_owner:
2045 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 *res = NULL;
2048 return status;
2049}
2050
2051
Andy Adamson82be4172012-05-23 05:02:35 -04002052static struct nfs4_state *nfs4_do_open(struct inode *dir,
2053 struct dentry *dentry,
2054 fmode_t fmode,
2055 int flags,
2056 struct iattr *sattr,
2057 struct rpc_cred *cred,
2058 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
2060 struct nfs4_exception exception = { };
2061 struct nfs4_state *res;
2062 int status;
2063
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002064 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002066 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2067 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (status == 0)
2069 break;
2070 /* NOTE: BAD_SEQID means the server and client disagree about the
2071 * book-keeping w.r.t. state-changing operations
2072 * (OPEN/CLOSE/LOCK/LOCKU...)
2073 * It is actually a sign of a bug on the client or on the server.
2074 *
2075 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002076 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 * have unhashed the old state_owner for us, and that we can
2078 * therefore safely retry using a new one. We should still warn
2079 * the user though...
2080 */
2081 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002082 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002083 " returned a bad sequence-id error!\n",
2084 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 exception.retry = 1;
2086 continue;
2087 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002088 /*
2089 * BAD_STATEID on OPEN means that the server cancelled our
2090 * state before it received the OPEN_CONFIRM.
2091 * Recover by retrying the request as per the discussion
2092 * on Page 181 of RFC3530.
2093 */
2094 if (status == -NFS4ERR_BAD_STATEID) {
2095 exception.retry = 1;
2096 continue;
2097 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002098 if (status == -EAGAIN) {
2099 /* We must have found a delegation */
2100 exception.retry = 1;
2101 continue;
2102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2104 status, &exception));
2105 } while (exception.retry);
2106 return res;
2107}
2108
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002109static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2110 struct nfs_fattr *fattr, struct iattr *sattr,
2111 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002113 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002115 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 .iap = sattr,
2117 .server = server,
2118 .bitmask = server->attr_bitmask,
2119 };
2120 struct nfs_setattrres res = {
2121 .fattr = fattr,
2122 .server = server,
2123 };
2124 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2126 .rpc_argp = &arg,
2127 .rpc_resp = &res,
2128 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002130 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002131 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Trond Myklebust0e574af2005-10-27 22:12:38 -04002133 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Trond Myklebust4fc87962012-03-08 17:42:01 -05002135 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002136 struct nfs_lockowner lockowner = {
2137 .l_owner = current->files,
2138 .l_pid = current->tgid,
2139 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002140 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002141 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002142 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2143 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002144 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002145 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002146 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Bryan Schumaker7c513052011-03-24 17:12:24 +00002148 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002149 if (status == 0 && state != NULL)
2150 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002151 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002154static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2155 struct nfs_fattr *fattr, struct iattr *sattr,
2156 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002158 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002159 struct nfs4_exception exception = {
2160 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002161 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002162 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 int err;
2164 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002165 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2166 switch (err) {
2167 case -NFS4ERR_OPENMODE:
2168 if (state && !(state->state & FMODE_WRITE)) {
2169 err = -EBADF;
2170 if (sattr->ia_valid & ATTR_OPEN)
2171 err = -EACCES;
2172 goto out;
2173 }
2174 }
2175 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002177out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return err;
2179}
2180
2181struct nfs4_closedata {
2182 struct inode *inode;
2183 struct nfs4_state *state;
2184 struct nfs_closeargs arg;
2185 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002186 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002187 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002188 bool roc;
2189 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190};
2191
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002192static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002193{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002194 struct nfs4_closedata *calldata = data;
2195 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002196 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002197
Fred Isamanf7e89172011-01-06 11:36:32 +00002198 if (calldata->roc)
2199 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002200 nfs4_put_open_state(calldata->state);
2201 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002202 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002203 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002204 kfree(calldata);
2205}
2206
Trond Myklebust88069f72009-12-08 08:33:16 -05002207static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2208 fmode_t fmode)
2209{
2210 spin_lock(&state->owner->so_lock);
2211 if (!(fmode & FMODE_READ))
2212 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2213 if (!(fmode & FMODE_WRITE))
2214 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2215 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2216 spin_unlock(&state->owner->so_lock);
2217}
2218
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002219static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002221 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 struct nfs_server *server = NFS_SERVER(calldata->inode);
2224
Chuck Levera3ca5652012-03-01 17:00:40 -05002225 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002226 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2227 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 /* hmm. we are done with the inode, and in the process of freeing
2229 * the state_owner. we keep this around to process errors
2230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 switch (task->tk_status) {
2232 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002233 if (calldata->roc)
2234 pnfs_roc_set_barrier(state->inode,
2235 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002236 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002237 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002238 nfs4_close_clear_stateid_flags(state,
2239 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 break;
2241 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002242 case -NFS4ERR_OLD_STATEID:
2243 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002245 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002248 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2249 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002251 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002252 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002253 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002256static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002258 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002259 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002260 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002261 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002262
Chuck Levera3ca5652012-03-01 17:00:40 -05002263 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002264 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002265 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002266
Trond Myklebust88069f72009-12-08 08:33:16 -05002267 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2268 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002269 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002270 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002271 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002272 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002273 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2274 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2275 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002276 }
2277 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002278 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2279 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2280 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002281 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002282 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002283 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002284
2285 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002286 /* Note: exit _without_ calling nfs4_close_done */
2287 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002288 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002289 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002290
Fred Isamanf7e89172011-01-06 11:36:32 +00002291 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002292 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002293 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002294 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002295 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002296 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002297
Trond Myklebust516a6af2005-10-27 22:12:41 -04002298 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002299 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002300 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002301 &calldata->arg.seq_args,
2302 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002303 task) != 0)
2304 nfs_release_seqid(calldata->arg.seqid);
2305 else
2306 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002307out:
2308 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002311static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002312 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002313 .rpc_call_done = nfs4_close_done,
2314 .rpc_release = nfs4_free_closedata,
2315};
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317/*
2318 * It is possible for data to be read/written from a mem-mapped file
2319 * after the sys_close call (which hits the vfs layer as a flush).
2320 * This means that we can't safely call nfsv4 close on a file until
2321 * the inode is cleared. This in turn means that we are not good
2322 * NFSv4 citizens - we do not indicate to the server to update the file's
2323 * share state even when we are done with one of the three share
2324 * stateid's in the inode.
2325 *
2326 * NOTE: Caller must be holding the sp->so_owner semaphore!
2327 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002328int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002330 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002332 struct nfs4_state_owner *sp = state->owner;
2333 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002334 struct rpc_message msg = {
2335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2336 .rpc_cred = state->owner->so_cred,
2337 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002338 struct rpc_task_setup task_setup_data = {
2339 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002340 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002341 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002342 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002343 .flags = RPC_TASK_ASYNC,
2344 };
Trond Myklebust95121352005-10-18 14:20:12 -07002345 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002347 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002349 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002350 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002351 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002353 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002354 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002356 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002357 if (calldata->arg.seqid == NULL)
2358 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002359 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002360 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002361 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002362 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002363 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002364 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002365 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002366
Trond Myklebust1174dd12010-12-21 10:52:24 -05002367 msg.rpc_argp = &calldata->arg;
2368 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002369 task_setup_data.callback_data = calldata;
2370 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002371 if (IS_ERR(task))
2372 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002373 status = 0;
2374 if (wait)
2375 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002376 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002377 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002378out_free_calldata:
2379 kfree(calldata);
2380out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002381 nfs4_put_open_state(state);
2382 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002383 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Trond Myklebust2b484292010-09-17 10:56:51 -04002386static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002387nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 struct nfs4_state *state;
2390
Trond Myklebust565277f2007-10-15 18:17:53 -04002391 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002392 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2393 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002394 if (IS_ERR(state))
2395 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002396 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002397 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Trond Myklebust1185a552009-12-03 15:54:02 -05002400static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002401{
2402 if (ctx->state == NULL)
2403 return;
2404 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002405 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002406 else
Al Viro643168c2011-06-22 18:20:23 -04002407 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002408}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2411{
Benny Halevy43652ad2009-04-01 09:21:54 -04002412 struct nfs4_server_caps_arg args = {
2413 .fhandle = fhandle,
2414 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 struct nfs4_server_caps_res res = {};
2416 struct rpc_message msg = {
2417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002418 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 .rpc_resp = &res,
2420 };
2421 int status;
2422
Bryan Schumaker7c513052011-03-24 17:12:24 +00002423 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (status == 0) {
2425 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002426 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2427 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2428 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2429 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2430 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2432 server->caps |= NFS_CAP_ACLS;
2433 if (res.has_links != 0)
2434 server->caps |= NFS_CAP_HARDLINKS;
2435 if (res.has_symlinks != 0)
2436 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002437 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2438 server->caps |= NFS_CAP_FILEID;
2439 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2440 server->caps |= NFS_CAP_MODE;
2441 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2442 server->caps |= NFS_CAP_NLINK;
2443 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2444 server->caps |= NFS_CAP_OWNER;
2445 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2446 server->caps |= NFS_CAP_OWNER_GROUP;
2447 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2448 server->caps |= NFS_CAP_ATIME;
2449 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2450 server->caps |= NFS_CAP_CTIME;
2451 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2452 server->caps |= NFS_CAP_MTIME;
2453
Trond Myklebusta65318b2009-03-11 14:10:28 -04002454 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2455 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2456 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002458 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return status;
2462}
2463
Trond Myklebust55a97592006-06-09 09:34:19 -04002464int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
2466 struct nfs4_exception exception = { };
2467 int err;
2468 do {
2469 err = nfs4_handle_exception(server,
2470 _nfs4_server_capabilities(server, fhandle),
2471 &exception);
2472 } while (exception.retry);
2473 return err;
2474}
2475
2476static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2477 struct nfs_fsinfo *info)
2478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 struct nfs4_lookup_root_arg args = {
2480 .bitmask = nfs4_fattr_bitmap,
2481 };
2482 struct nfs4_lookup_res res = {
2483 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002484 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 .fh = fhandle,
2486 };
2487 struct rpc_message msg = {
2488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2489 .rpc_argp = &args,
2490 .rpc_resp = &res,
2491 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002492
Trond Myklebust0e574af2005-10-27 22:12:38 -04002493 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002494 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
2497static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2498 struct nfs_fsinfo *info)
2499{
2500 struct nfs4_exception exception = { };
2501 int err;
2502 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002503 err = _nfs4_lookup_root(server, fhandle, info);
2504 switch (err) {
2505 case 0:
2506 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002507 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002508 default:
2509 err = nfs4_handle_exception(server, err, &exception);
2510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002512out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return err;
2514}
2515
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002516static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2517 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2518{
2519 struct rpc_auth *auth;
2520 int ret;
2521
2522 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002523 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002524 ret = -EIO;
2525 goto out;
2526 }
2527 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002528out:
2529 return ret;
2530}
2531
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002532static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002533 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002535 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002536 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002538 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002539 if (len < 0)
2540 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002541
2542 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002543 /* AUTH_UNIX is the default flavor if none was specified,
2544 * thus has already been tried. */
2545 if (flav_array[i] == RPC_AUTH_UNIX)
2546 continue;
2547
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002548 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002549 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002550 continue;
2551 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002552 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002553 /*
2554 * -EACCESS could mean that the user doesn't have correct permissions
2555 * to access the mount. It could also mean that we tried to mount
2556 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2557 * existing mount programs don't handle -EACCES very well so it should
2558 * be mapped to -EPERM instead.
2559 */
2560 if (status == -EACCES)
2561 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002562 return status;
2563}
2564
2565/*
2566 * get the file handle for the "/" directory on the server
2567 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002568int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2569 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002570{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002571 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002572 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002573 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2574 /*
2575 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2576 * by nfs4_map_errors() as this function exits.
2577 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002578 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (status == 0)
2580 status = nfs4_server_capabilities(server, fhandle);
2581 if (status == 0)
2582 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002583 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Bryan Schumakerbae36242012-05-10 15:07:31 -04002586static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2587 struct nfs_fsinfo *info)
2588{
2589 int error;
2590 struct nfs_fattr *fattr = info->fattr;
2591
2592 error = nfs4_server_capabilities(server, mntfh);
2593 if (error < 0) {
2594 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2595 return error;
2596 }
2597
2598 error = nfs4_proc_getattr(server, mntfh, fattr);
2599 if (error < 0) {
2600 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2601 return error;
2602 }
2603
2604 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2605 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2606 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2607
2608 return error;
2609}
2610
Manoj Naik6b97fd32006-06-09 09:34:29 -04002611/*
2612 * Get locations and (maybe) other attributes of a referral.
2613 * Note that we'll actually follow the referral later when
2614 * we detect fsid mismatch in inode revalidation
2615 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002616static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2617 const struct qstr *name, struct nfs_fattr *fattr,
2618 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002619{
2620 int status = -ENOMEM;
2621 struct page *page = NULL;
2622 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002623
2624 page = alloc_page(GFP_KERNEL);
2625 if (page == NULL)
2626 goto out;
2627 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2628 if (locations == NULL)
2629 goto out;
2630
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002631 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002632 if (status != 0)
2633 goto out;
2634 /* Make sure server returned a different fsid for the referral */
2635 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002636 dprintk("%s: server did not return a different fsid for"
2637 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002638 status = -EIO;
2639 goto out;
2640 }
Andy Adamson533eb462011-06-13 18:25:56 -04002641 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2642 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002643
Andy Adamson533eb462011-06-13 18:25:56 -04002644 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002645 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002646 memset(fhandle, 0, sizeof(struct nfs_fh));
2647out:
2648 if (page)
2649 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002650 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002651 return status;
2652}
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2655{
2656 struct nfs4_getattr_arg args = {
2657 .fh = fhandle,
2658 .bitmask = server->attr_bitmask,
2659 };
2660 struct nfs4_getattr_res res = {
2661 .fattr = fattr,
2662 .server = server,
2663 };
2664 struct rpc_message msg = {
2665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2666 .rpc_argp = &args,
2667 .rpc_resp = &res,
2668 };
2669
Trond Myklebust0e574af2005-10-27 22:12:38 -04002670 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002671 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
2674static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2675{
2676 struct nfs4_exception exception = { };
2677 int err;
2678 do {
2679 err = nfs4_handle_exception(server,
2680 _nfs4_proc_getattr(server, fhandle, fattr),
2681 &exception);
2682 } while (exception.retry);
2683 return err;
2684}
2685
2686/*
2687 * The file is not closed if it is opened due to the a request to change
2688 * the size of the file. The open call will not be needed once the
2689 * VFS layer lookup-intents are implemented.
2690 *
2691 * Close is called when the inode is destroyed.
2692 * If we haven't opened the file for O_WRONLY, we
2693 * need to in the size_change case to obtain a stateid.
2694 *
2695 * Got race?
2696 * Because OPEN is always done by name in nfsv4, it is
2697 * possible that we opened a different file by the same
2698 * name. We can recognize this race condition, but we
2699 * can't do anything about it besides returning an error.
2700 *
2701 * This will be fixed with VFS changes (lookup-intent).
2702 */
2703static int
2704nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2705 struct iattr *sattr)
2706{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002707 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002708 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002709 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 int status;
2711
Benny Halevy8a1636c2010-07-14 15:43:57 -04002712 if (pnfs_ld_layoutret_on_setattr(inode))
2713 pnfs_return_layout(inode);
2714
Trond Myklebust0e574af2005-10-27 22:12:38 -04002715 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Andy Adamson26699402012-05-30 16:12:24 -04002717 /* Deal with open(O_TRUNC) */
2718 if (sattr->ia_valid & ATTR_OPEN)
2719 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2720
2721 /* Optimization: if the end result is no change, don't RPC */
2722 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2723 return 0;
2724
Trond Myklebustd5308382005-11-04 15:33:38 -05002725 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002726 if (sattr->ia_valid & ATTR_FILE) {
2727 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002728
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002729 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002730 if (ctx) {
2731 cred = ctx->cred;
2732 state = ctx->state;
2733 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002734 }
2735
2736 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002737 if (status == 0)
2738 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return status;
2740}
2741
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002742static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2743 const struct qstr *name, struct nfs_fh *fhandle,
2744 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002745{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002746 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002747 int status;
2748 struct nfs4_lookup_arg args = {
2749 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002750 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002751 .name = name,
2752 };
2753 struct nfs4_lookup_res res = {
2754 .server = server,
2755 .fattr = fattr,
2756 .fh = fhandle,
2757 };
2758 struct rpc_message msg = {
2759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2760 .rpc_argp = &args,
2761 .rpc_resp = &res,
2762 };
2763
2764 nfs_fattr_init(fattr);
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002767 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 dprintk("NFS reply lookup: %d\n", status);
2769 return status;
2770}
2771
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002772static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002773{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002774 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002775 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002776 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2777 fattr->nlink = 2;
2778}
2779
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002780static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2781 struct qstr *name, struct nfs_fh *fhandle,
2782 struct nfs_fattr *fattr)
2783{
2784 struct nfs4_exception exception = { };
2785 struct rpc_clnt *client = *clnt;
2786 int err;
2787 do {
2788 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2789 switch (err) {
2790 case -NFS4ERR_BADNAME:
2791 err = -ENOENT;
2792 goto out;
2793 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002794 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002795 goto out;
2796 case -NFS4ERR_WRONGSEC:
2797 err = -EPERM;
2798 if (client != *clnt)
2799 goto out;
2800
2801 client = nfs4_create_sec_client(client, dir, name);
2802 if (IS_ERR(client))
2803 return PTR_ERR(client);
2804
2805 exception.retry = 1;
2806 break;
2807 default:
2808 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2809 }
2810 } while (exception.retry);
2811
2812out:
2813 if (err == 0)
2814 *clnt = client;
2815 else if (client != *clnt)
2816 rpc_shutdown_client(client);
2817
2818 return err;
2819}
2820
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002821static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002822 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002824 int status;
2825 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002826
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002827 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2828 if (client != NFS_CLIENT(dir)) {
2829 rpc_shutdown_client(client);
2830 nfs_fixup_secinfo_attributes(fattr);
2831 }
2832 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833}
2834
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002835struct rpc_clnt *
2836nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2837 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2838{
2839 int status;
2840 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2841
2842 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2843 if (status < 0) {
2844 rpc_shutdown_client(client);
2845 return ERR_PTR(status);
2846 }
2847 return client;
2848}
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2851{
Trond Myklebust76b32992007-08-10 17:45:11 -04002852 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 struct nfs4_accessargs args = {
2854 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002855 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002857 struct nfs4_accessres res = {
2858 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002859 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 struct rpc_message msg = {
2861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2862 .rpc_argp = &args,
2863 .rpc_resp = &res,
2864 .rpc_cred = entry->cred,
2865 };
2866 int mode = entry->mask;
2867 int status;
2868
2869 /*
2870 * Determine which access bits we want to ask for...
2871 */
2872 if (mode & MAY_READ)
2873 args.access |= NFS4_ACCESS_READ;
2874 if (S_ISDIR(inode->i_mode)) {
2875 if (mode & MAY_WRITE)
2876 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2877 if (mode & MAY_EXEC)
2878 args.access |= NFS4_ACCESS_LOOKUP;
2879 } else {
2880 if (mode & MAY_WRITE)
2881 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2882 if (mode & MAY_EXEC)
2883 args.access |= NFS4_ACCESS_EXECUTE;
2884 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002885
2886 res.fattr = nfs_alloc_fattr();
2887 if (res.fattr == NULL)
2888 return -ENOMEM;
2889
Bryan Schumaker7c513052011-03-24 17:12:24 +00002890 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002892 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002893 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002895 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 return status;
2897}
2898
2899static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2900{
2901 struct nfs4_exception exception = { };
2902 int err;
2903 do {
2904 err = nfs4_handle_exception(NFS_SERVER(inode),
2905 _nfs4_proc_access(inode, entry),
2906 &exception);
2907 } while (exception.retry);
2908 return err;
2909}
2910
2911/*
2912 * TODO: For the time being, we don't try to get any attributes
2913 * along with any of the zero-copy operations READ, READDIR,
2914 * READLINK, WRITE.
2915 *
2916 * In the case of the first three, we want to put the GETATTR
2917 * after the read-type operation -- this is because it is hard
2918 * to predict the length of a GETATTR response in v4, and thus
2919 * align the READ data correctly. This means that the GETATTR
2920 * may end up partially falling into the page cache, and we should
2921 * shift it into the 'tail' of the xdr_buf before processing.
2922 * To do this efficiently, we need to know the total length
2923 * of data received, which doesn't seem to be available outside
2924 * of the RPC layer.
2925 *
2926 * In the case of WRITE, we also want to put the GETATTR after
2927 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002928 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 *
2930 * Both of these changes to the XDR layer would in fact be quite
2931 * minor, but I decided to leave them for a subsequent patch.
2932 */
2933static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2934 unsigned int pgbase, unsigned int pglen)
2935{
2936 struct nfs4_readlink args = {
2937 .fh = NFS_FH(inode),
2938 .pgbase = pgbase,
2939 .pglen = pglen,
2940 .pages = &page,
2941 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002942 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 struct rpc_message msg = {
2944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2945 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002946 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 };
2948
Bryan Schumaker7c513052011-03-24 17:12:24 +00002949 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 -07002950}
2951
2952static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2953 unsigned int pgbase, unsigned int pglen)
2954{
2955 struct nfs4_exception exception = { };
2956 int err;
2957 do {
2958 err = nfs4_handle_exception(NFS_SERVER(inode),
2959 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2960 &exception);
2961 } while (exception.retry);
2962 return err;
2963}
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002966 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968static int
2969nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002970 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002972 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 int status = 0;
2975
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002976 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2977 if (IS_ERR(ctx))
2978 return PTR_ERR(ctx);
2979
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002980 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002981 state = nfs4_do_open(dir, dentry, ctx->mode,
2982 flags, sattr, ctx->cred,
2983 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002984 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (IS_ERR(state)) {
2986 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002987 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002989 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002990 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002991 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002993 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return status;
2995}
2996
2997static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2998{
Trond Myklebust16e42952005-10-27 22:12:44 -04002999 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003000 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003002 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003004 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003005 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003006 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3009 .rpc_argp = &args,
3010 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003012 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003013
Bryan Schumaker7c513052011-03-24 17:12:24 +00003014 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003015 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003016 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 return status;
3018}
3019
3020static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3021{
3022 struct nfs4_exception exception = { };
3023 int err;
3024 do {
3025 err = nfs4_handle_exception(NFS_SERVER(dir),
3026 _nfs4_proc_remove(dir, name),
3027 &exception);
3028 } while (exception.retry);
3029 return err;
3030}
3031
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003032static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003034 struct nfs_server *server = NFS_SERVER(dir);
3035 struct nfs_removeargs *args = msg->rpc_argp;
3036 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003038 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003040 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003043static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3044{
3045 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3046 &data->args.seq_args,
3047 &data->res.seq_res,
3048 task))
3049 return;
3050 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003053static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003055 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3056
Trond Myklebust14516c32010-07-31 14:29:06 -04003057 if (!nfs4_sequence_done(task, &res->seq_res))
3058 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003059 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003060 return 0;
3061 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003062 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
Jeff Laytond3d41522010-09-17 17:31:57 -04003065static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3066{
3067 struct nfs_server *server = NFS_SERVER(dir);
3068 struct nfs_renameargs *arg = msg->rpc_argp;
3069 struct nfs_renameres *res = msg->rpc_resp;
3070
3071 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003072 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003073 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003074}
3075
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003076static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3077{
3078 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3079 &data->args.seq_args,
3080 &data->res.seq_res,
3081 task))
3082 return;
3083 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003084}
3085
3086static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3087 struct inode *new_dir)
3088{
3089 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3090
3091 if (!nfs4_sequence_done(task, &res->seq_res))
3092 return 0;
3093 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3094 return 0;
3095
3096 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003097 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003098 return 1;
3099}
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3102 struct inode *new_dir, struct qstr *new_name)
3103{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003104 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003105 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 .old_dir = NFS_FH(old_dir),
3107 .new_dir = NFS_FH(new_dir),
3108 .old_name = old_name,
3109 .new_name = new_name,
3110 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003111 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003112 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003113 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 struct rpc_message msg = {
3115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3116 .rpc_argp = &arg,
3117 .rpc_resp = &res,
3118 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003119 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Bryan Schumaker7c513052011-03-24 17:12:24 +00003121 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 if (!status) {
3123 update_changeattr(old_dir, &res.old_cinfo);
3124 update_changeattr(new_dir, &res.new_cinfo);
3125 }
3126 return status;
3127}
3128
3129static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3130 struct inode *new_dir, struct qstr *new_name)
3131{
3132 struct nfs4_exception exception = { };
3133 int err;
3134 do {
3135 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3136 _nfs4_proc_rename(old_dir, old_name,
3137 new_dir, new_name),
3138 &exception);
3139 } while (exception.retry);
3140 return err;
3141}
3142
3143static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3144{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003145 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 struct nfs4_link_arg arg = {
3147 .fh = NFS_FH(inode),
3148 .dir_fh = NFS_FH(dir),
3149 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003150 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003152 struct nfs4_link_res res = {
3153 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003154 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 struct rpc_message msg = {
3156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3157 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003158 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003160 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Trond Myklebust136f2622010-04-16 16:22:49 -04003162 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003163 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003164 goto out;
3165
Bryan Schumaker7c513052011-03-24 17:12:24 +00003166 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003167 if (!status) {
3168 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003169 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003170 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003171out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003172 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return status;
3174}
3175
3176static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3177{
3178 struct nfs4_exception exception = { };
3179 int err;
3180 do {
3181 err = nfs4_handle_exception(NFS_SERVER(inode),
3182 _nfs4_proc_link(inode, dir, name),
3183 &exception);
3184 } while (exception.retry);
3185 return err;
3186}
3187
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003188struct nfs4_createdata {
3189 struct rpc_message msg;
3190 struct nfs4_create_arg arg;
3191 struct nfs4_create_res res;
3192 struct nfs_fh fh;
3193 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003194};
3195
3196static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3197 struct qstr *name, struct iattr *sattr, u32 ftype)
3198{
3199 struct nfs4_createdata *data;
3200
3201 data = kzalloc(sizeof(*data), GFP_KERNEL);
3202 if (data != NULL) {
3203 struct nfs_server *server = NFS_SERVER(dir);
3204
3205 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3206 data->msg.rpc_argp = &data->arg;
3207 data->msg.rpc_resp = &data->res;
3208 data->arg.dir_fh = NFS_FH(dir);
3209 data->arg.server = server;
3210 data->arg.name = name;
3211 data->arg.attrs = sattr;
3212 data->arg.ftype = ftype;
3213 data->arg.bitmask = server->attr_bitmask;
3214 data->res.server = server;
3215 data->res.fh = &data->fh;
3216 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003217 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003218 }
3219 return data;
3220}
3221
3222static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3223{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003224 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003225 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003226 if (status == 0) {
3227 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003228 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3229 }
3230 return status;
3231}
3232
3233static void nfs4_free_createdata(struct nfs4_createdata *data)
3234{
3235 kfree(data);
3236}
3237
Chuck Lever4f390c12006-08-22 20:06:22 -04003238static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003239 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003241 struct nfs4_createdata *data;
3242 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Chuck Lever94a6d752006-08-22 20:06:23 -04003244 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003245 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003246
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003247 status = -ENOMEM;
3248 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3249 if (data == NULL)
3250 goto out;
3251
3252 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3253 data->arg.u.symlink.pages = &page;
3254 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003256 status = nfs4_do_create(dir, dentry, data);
3257
3258 nfs4_free_createdata(data);
3259out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return status;
3261}
3262
Chuck Lever4f390c12006-08-22 20:06:22 -04003263static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003264 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
3266 struct nfs4_exception exception = { };
3267 int err;
3268 do {
3269 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003270 _nfs4_proc_symlink(dir, dentry, page,
3271 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 &exception);
3273 } while (exception.retry);
3274 return err;
3275}
3276
3277static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3278 struct iattr *sattr)
3279{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003280 struct nfs4_createdata *data;
3281 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003283 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3284 if (data == NULL)
3285 goto out;
3286
3287 status = nfs4_do_create(dir, dentry, data);
3288
3289 nfs4_free_createdata(data);
3290out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 return status;
3292}
3293
3294static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3295 struct iattr *sattr)
3296{
3297 struct nfs4_exception exception = { };
3298 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003299
3300 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 do {
3302 err = nfs4_handle_exception(NFS_SERVER(dir),
3303 _nfs4_proc_mkdir(dir, dentry, sattr),
3304 &exception);
3305 } while (exception.retry);
3306 return err;
3307}
3308
3309static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003310 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
3312 struct inode *dir = dentry->d_inode;
3313 struct nfs4_readdir_arg args = {
3314 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003315 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 .pgbase = 0,
3317 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003318 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003319 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 };
3321 struct nfs4_readdir_res res;
3322 struct rpc_message msg = {
3323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3324 .rpc_argp = &args,
3325 .rpc_resp = &res,
3326 .rpc_cred = cred,
3327 };
3328 int status;
3329
Harvey Harrison3110ff82008-05-02 13:42:44 -07003330 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003331 dentry->d_parent->d_name.name,
3332 dentry->d_name.name,
3333 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003334 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003336 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 -05003337 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003338 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003339 status += args.pgbase;
3340 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003341
3342 nfs_invalidate_atime(dir);
3343
Harvey Harrison3110ff82008-05-02 13:42:44 -07003344 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 return status;
3346}
3347
3348static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003349 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
3351 struct nfs4_exception exception = { };
3352 int err;
3353 do {
3354 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3355 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003356 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 &exception);
3358 } while (exception.retry);
3359 return err;
3360}
3361
3362static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3363 struct iattr *sattr, dev_t rdev)
3364{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003365 struct nfs4_createdata *data;
3366 int mode = sattr->ia_mode;
3367 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003369 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3370 if (data == NULL)
3371 goto out;
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003374 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003376 data->arg.ftype = NF4BLK;
3377 data->arg.u.device.specdata1 = MAJOR(rdev);
3378 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 }
3380 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003381 data->arg.ftype = NF4CHR;
3382 data->arg.u.device.specdata1 = MAJOR(rdev);
3383 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003384 } else if (!S_ISSOCK(mode)) {
3385 status = -EINVAL;
3386 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003389 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003390out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003391 nfs4_free_createdata(data);
3392out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 return status;
3394}
3395
3396static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3397 struct iattr *sattr, dev_t rdev)
3398{
3399 struct nfs4_exception exception = { };
3400 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003401
3402 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 do {
3404 err = nfs4_handle_exception(NFS_SERVER(dir),
3405 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3406 &exception);
3407 } while (exception.retry);
3408 return err;
3409}
3410
3411static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3412 struct nfs_fsstat *fsstat)
3413{
3414 struct nfs4_statfs_arg args = {
3415 .fh = fhandle,
3416 .bitmask = server->attr_bitmask,
3417 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003418 struct nfs4_statfs_res res = {
3419 .fsstat = fsstat,
3420 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 struct rpc_message msg = {
3422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3423 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003424 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 };
3426
Trond Myklebust0e574af2005-10-27 22:12:38 -04003427 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003428 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429}
3430
3431static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3432{
3433 struct nfs4_exception exception = { };
3434 int err;
3435 do {
3436 err = nfs4_handle_exception(server,
3437 _nfs4_proc_statfs(server, fhandle, fsstat),
3438 &exception);
3439 } while (exception.retry);
3440 return err;
3441}
3442
3443static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3444 struct nfs_fsinfo *fsinfo)
3445{
3446 struct nfs4_fsinfo_arg args = {
3447 .fh = fhandle,
3448 .bitmask = server->attr_bitmask,
3449 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003450 struct nfs4_fsinfo_res res = {
3451 .fsinfo = fsinfo,
3452 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 struct rpc_message msg = {
3454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3455 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003456 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 };
3458
Bryan Schumaker7c513052011-03-24 17:12:24 +00003459 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460}
3461
3462static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3463{
3464 struct nfs4_exception exception = { };
3465 int err;
3466
3467 do {
3468 err = nfs4_handle_exception(server,
3469 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3470 &exception);
3471 } while (exception.retry);
3472 return err;
3473}
3474
3475static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3476{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003477 int error;
3478
Trond Myklebust0e574af2005-10-27 22:12:38 -04003479 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003480 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003481 if (error == 0) {
3482 /* block layout checks this! */
3483 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003484 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003485 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003486
3487 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488}
3489
3490static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3491 struct nfs_pathconf *pathconf)
3492{
3493 struct nfs4_pathconf_arg args = {
3494 .fh = fhandle,
3495 .bitmask = server->attr_bitmask,
3496 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003497 struct nfs4_pathconf_res res = {
3498 .pathconf = pathconf,
3499 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 struct rpc_message msg = {
3501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3502 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003503 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 };
3505
3506 /* None of the pathconf attributes are mandatory to implement */
3507 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3508 memset(pathconf, 0, sizeof(*pathconf));
3509 return 0;
3510 }
3511
Trond Myklebust0e574af2005-10-27 22:12:38 -04003512 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003513 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
3515
3516static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3517 struct nfs_pathconf *pathconf)
3518{
3519 struct nfs4_exception exception = { };
3520 int err;
3521
3522 do {
3523 err = nfs4_handle_exception(server,
3524 _nfs4_proc_pathconf(server, fhandle, pathconf),
3525 &exception);
3526 } while (exception.retry);
3527 return err;
3528}
3529
Benny Halevyd20581a2011-05-22 19:52:03 +03003530void __nfs4_read_done_cb(struct nfs_read_data *data)
3531{
Fred Isamancd841602012-04-20 14:47:44 -04003532 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003533}
3534
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003535static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
Fred Isamancd841602012-04-20 14:47:44 -04003537 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003539 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003540 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003541 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003543
Benny Halevyd20581a2011-05-22 19:52:03 +03003544 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003546 renew_lease(server, data->timestamp);
3547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003550static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3551{
3552
3553 dprintk("--> %s\n", __func__);
3554
3555 if (!nfs4_sequence_done(task, &data->res.seq_res))
3556 return -EAGAIN;
3557
Benny Halevyd20581a2011-05-22 19:52:03 +03003558 return data->read_done_cb ? data->read_done_cb(task, data) :
3559 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003560}
3561
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003562static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003565 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003566 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003567 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003570static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3571{
Fred Isamancd841602012-04-20 14:47:44 -04003572 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003573 &data->args.seq_args,
3574 &data->res.seq_res,
3575 task))
3576 return;
3577 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578}
3579
Fred Isamanb029bc92011-03-03 15:13:42 +00003580static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
Fred Isamancd841602012-04-20 14:47:44 -04003582 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003584 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -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 Myklebust4f9838c2005-10-27 22:12:44 -04003588 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003590 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003591 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
Fred Isamanb029bc92011-03-03 15:13:42 +00003595static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3596{
3597 if (!nfs4_sequence_done(task, &data->res.seq_res))
3598 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003599 return data->write_done_cb ? data->write_done_cb(task, data) :
3600 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003601}
3602
Trond Myklebust5a37f852012-04-28 14:55:16 -04003603static
3604bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003605{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003606 const struct nfs_pgio_header *hdr = data->header;
3607
3608 /* Don't request attributes for pNFS or O_DIRECT writes */
3609 if (data->ds_clp != NULL || hdr->dreq != NULL)
3610 return false;
3611 /* Otherwise, request attributes if and only if we don't hold
3612 * a delegation
3613 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003614 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003615}
Fred Isamana69aef12011-03-03 15:13:47 +00003616
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003617static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
Fred Isamancd841602012-04-20 14:47:44 -04003619 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003620
Trond Myklebust5a37f852012-04-28 14:55:16 -04003621 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003622 data->args.bitmask = NULL;
3623 data->res.fattr = NULL;
3624 } else
3625 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003626
Fred Isamanb029bc92011-03-03 15:13:42 +00003627 if (!data->write_done_cb)
3628 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003629 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 data->timestamp = jiffies;
3631
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003632 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003633 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634}
3635
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003636static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3637{
Fred Isamancd841602012-04-20 14:47:44 -04003638 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003639 &data->args.seq_args,
3640 &data->res.seq_res,
3641 task))
3642 return;
3643 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644}
3645
Fred Isaman0b7c0152012-04-20 14:47:39 -04003646static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3647{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3649 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003650 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return;
3653 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003654}
3655
Fred Isaman0b7c0152012-04-20 14:47:39 -04003656static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
3658 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003659
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003660 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003661 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003662 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665}
3666
Fred Isaman0b7c0152012-04-20 14:47:39 -04003667static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003668{
3669 if (!nfs4_sequence_done(task, &data->res.seq_res))
3670 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003671 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003672}
3673
Fred Isaman0b7c0152012-04-20 14:47:39 -04003674static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003676 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003677
Fred Isaman0b7c0152012-04-20 14:47:39 -04003678 if (data->commit_done_cb == NULL)
3679 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003680 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003681 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003682 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683}
3684
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003685struct nfs4_renewdata {
3686 struct nfs_client *client;
3687 unsigned long timestamp;
3688};
3689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690/*
3691 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3692 * standalone procedure for queueing an asynchronous RENEW.
3693 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003694static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003695{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003696 struct nfs4_renewdata *data = calldata;
3697 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003698
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003699 if (atomic_read(&clp->cl_count) > 1)
3700 nfs4_schedule_state_renewal(clp);
3701 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003702 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003703}
3704
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003705static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003707 struct nfs4_renewdata *data = calldata;
3708 struct nfs_client *clp = data->client;
3709 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003712 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003713 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3714 return;
3715 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003716 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003717 return;
3718 }
3719 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003721 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722}
3723
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003724static const struct rpc_call_ops nfs4_renew_ops = {
3725 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003726 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003727};
3728
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003729static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
3731 struct rpc_message msg = {
3732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3733 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003734 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003736 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003738 if (renew_flags == 0)
3739 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003740 if (!atomic_inc_not_zero(&clp->cl_count))
3741 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003742 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003743 if (data == NULL)
3744 return -ENOMEM;
3745 data->client = clp;
3746 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003748 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749}
3750
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003751static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
3753 struct rpc_message msg = {
3754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3755 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003756 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 };
3758 unsigned long now = jiffies;
3759 int status;
3760
3761 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3762 if (status < 0)
3763 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003764 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 return 0;
3766}
3767
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003768static inline int nfs4_server_supports_acls(struct nfs_server *server)
3769{
3770 return (server->caps & NFS_CAP_ACLS)
3771 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3772 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3773}
3774
Trond Myklebust21f498c2012-08-24 10:59:25 -04003775/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3776 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003777 * the stack.
3778 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003779#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003780
Neil Hormane9e3d722011-03-04 19:26:03 -05003781static int buf_to_pages_noslab(const void *buf, size_t buflen,
3782 struct page **pages, unsigned int *pgbase)
3783{
3784 struct page *newpage, **spages;
3785 int rc = 0;
3786 size_t len;
3787 spages = pages;
3788
3789 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003790 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003791 newpage = alloc_page(GFP_KERNEL);
3792
3793 if (newpage == NULL)
3794 goto unwind;
3795 memcpy(page_address(newpage), buf, len);
3796 buf += len;
3797 buflen -= len;
3798 *pages++ = newpage;
3799 rc++;
3800 } while (buflen != 0);
3801
3802 return rc;
3803
3804unwind:
3805 for(; rc > 0; rc--)
3806 __free_page(spages[rc-1]);
3807 return -ENOMEM;
3808}
3809
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003810struct nfs4_cached_acl {
3811 int cached;
3812 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003813 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003814};
3815
3816static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003817{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003818 struct nfs_inode *nfsi = NFS_I(inode);
3819
3820 spin_lock(&inode->i_lock);
3821 kfree(nfsi->nfs4_acl);
3822 nfsi->nfs4_acl = acl;
3823 spin_unlock(&inode->i_lock);
3824}
3825
3826static void nfs4_zap_acl_attr(struct inode *inode)
3827{
3828 nfs4_set_cached_acl(inode, NULL);
3829}
3830
3831static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3832{
3833 struct nfs_inode *nfsi = NFS_I(inode);
3834 struct nfs4_cached_acl *acl;
3835 int ret = -ENOENT;
3836
3837 spin_lock(&inode->i_lock);
3838 acl = nfsi->nfs4_acl;
3839 if (acl == NULL)
3840 goto out;
3841 if (buf == NULL) /* user is just asking for length */
3842 goto out_len;
3843 if (acl->cached == 0)
3844 goto out;
3845 ret = -ERANGE; /* see getxattr(2) man page */
3846 if (acl->len > buflen)
3847 goto out;
3848 memcpy(buf, acl->data, acl->len);
3849out_len:
3850 ret = acl->len;
3851out:
3852 spin_unlock(&inode->i_lock);
3853 return ret;
3854}
3855
Sachin Prabhu5794d212012-04-17 14:36:40 +01003856static 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 +00003857{
3858 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003859 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003860
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003861 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003862 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003863 if (acl == NULL)
3864 goto out;
3865 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003866 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003867 } else {
3868 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3869 if (acl == NULL)
3870 goto out;
3871 acl->cached = 0;
3872 }
3873 acl->len = acl_len;
3874out:
3875 nfs4_set_cached_acl(inode, acl);
3876}
3877
Andy Adamsonbf118a32011-12-07 11:55:27 -05003878/*
3879 * The getxattr API returns the required buffer length when called with a
3880 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3881 * the required buf. On a NULL buf, we send a page of data to the server
3882 * guessing that the ACL request can be serviced by a page. If so, we cache
3883 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3884 * the cache. If not so, we throw away the page, and cache the required
3885 * length. The next getxattr call will then produce another round trip to
3886 * the server, this time with the input buf of the required size.
3887 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003888static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003889{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003890 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003891 struct nfs_getaclargs args = {
3892 .fh = NFS_FH(inode),
3893 .acl_pages = pages,
3894 .acl_len = buflen,
3895 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003896 struct nfs_getaclres res = {
3897 .acl_len = buflen,
3898 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003899 struct rpc_message msg = {
3900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3901 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003902 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003903 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003904 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3905 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003906
Andy Adamsonbf118a32011-12-07 11:55:27 -05003907 /* As long as we're doing a round trip to the server anyway,
3908 * let's be prepared for a page of acl data. */
3909 if (npages == 0)
3910 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003911 if (npages > ARRAY_SIZE(pages))
3912 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003913
Andy Adamsonbf118a32011-12-07 11:55:27 -05003914 for (i = 0; i < npages; i++) {
3915 pages[i] = alloc_page(GFP_KERNEL);
3916 if (!pages[i])
3917 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003918 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003919
3920 /* for decoding across pages */
3921 res.acl_scratch = alloc_page(GFP_KERNEL);
3922 if (!res.acl_scratch)
3923 goto out_free;
3924
Andy Adamsonbf118a32011-12-07 11:55:27 -05003925 args.acl_len = npages * PAGE_SIZE;
3926 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003927
Peng Taode040be2012-01-10 22:42:47 +08003928 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003929 __func__, buf, buflen, npages, args.acl_len);
3930 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3931 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003932 if (ret)
3933 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003934
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003935 /* Handle the case where the passed-in buffer is too short */
3936 if (res.acl_flags & NFS4_ACL_TRUNC) {
3937 /* Did the user only issue a request for the acl length? */
3938 if (buf == NULL)
3939 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003940 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003941 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003942 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003943 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3944 if (buf)
3945 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3946out_ok:
3947 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003948out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003949 for (i = 0; i < npages; i++)
3950 if (pages[i])
3951 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003952 if (res.acl_scratch)
3953 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003954 return ret;
3955}
3956
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003957static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3958{
3959 struct nfs4_exception exception = { };
3960 ssize_t ret;
3961 do {
3962 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3963 if (ret >= 0)
3964 break;
3965 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3966 } while (exception.retry);
3967 return ret;
3968}
3969
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003970static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3971{
3972 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003973 int ret;
3974
3975 if (!nfs4_server_supports_acls(server))
3976 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003977 ret = nfs_revalidate_inode(server, inode);
3978 if (ret < 0)
3979 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003980 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3981 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003982 ret = nfs4_read_cached_acl(inode, buf, buflen);
3983 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003984 /* -ENOENT is returned if there is no ACL or if there is an ACL
3985 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003986 return ret;
3987 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003988}
3989
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003990static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003991{
3992 struct nfs_server *server = NFS_SERVER(inode);
3993 struct page *pages[NFS4ACL_MAXPAGES];
3994 struct nfs_setaclargs arg = {
3995 .fh = NFS_FH(inode),
3996 .acl_pages = pages,
3997 .acl_len = buflen,
3998 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003999 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004000 struct rpc_message msg = {
4001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4002 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004003 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004004 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004005 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004006 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004007
4008 if (!nfs4_server_supports_acls(server))
4009 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004010 if (npages > ARRAY_SIZE(pages))
4011 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004012 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4013 if (i < 0)
4014 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004015 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004016 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004017
4018 /*
4019 * Free each page after tx, so the only ref left is
4020 * held by the network stack
4021 */
4022 for (; i > 0; i--)
4023 put_page(pages[i-1]);
4024
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004025 /*
4026 * Acl update can result in inode attribute update.
4027 * so mark the attribute cache invalid.
4028 */
4029 spin_lock(&inode->i_lock);
4030 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4031 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004032 nfs_access_zap_cache(inode);
4033 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004034 return ret;
4035}
4036
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004037static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4038{
4039 struct nfs4_exception exception = { };
4040 int err;
4041 do {
4042 err = nfs4_handle_exception(NFS_SERVER(inode),
4043 __nfs4_proc_set_acl(inode, buf, buflen),
4044 &exception);
4045 } while (exception.retry);
4046 return err;
4047}
4048
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004050nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004052 struct nfs_client *clp = server->nfs_client;
4053
4054 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 return 0;
4056 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004057 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004058 case -NFS4ERR_ADMIN_REVOKED:
4059 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004060 if (state == NULL)
4061 break;
4062 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004063 case -NFS4ERR_OPENMODE:
4064 if (state == NULL)
4065 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004066 nfs4_schedule_stateid_recovery(server, state);
4067 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004068 case -NFS4ERR_EXPIRED:
4069 if (state != NULL)
4070 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004072 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004073 nfs4_schedule_lease_recovery(clp);
4074 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004075#if defined(CONFIG_NFS_V4_1)
4076 case -NFS4ERR_BADSESSION:
4077 case -NFS4ERR_BADSLOT:
4078 case -NFS4ERR_BAD_HIGH_SLOT:
4079 case -NFS4ERR_DEADSESSION:
4080 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4081 case -NFS4ERR_SEQ_FALSE_RETRY:
4082 case -NFS4ERR_SEQ_MISORDERED:
4083 dprintk("%s ERROR %d, Reset session\n", __func__,
4084 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004085 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004086 task->tk_status = 0;
4087 return -EAGAIN;
4088#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004090 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004091 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004092 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4094 task->tk_status = 0;
4095 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004096 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 case -NFS4ERR_OLD_STATEID:
4098 task->tk_status = 0;
4099 return -EAGAIN;
4100 }
4101 task->tk_status = nfs4_map_errors(task->tk_status);
4102 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004103wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004104 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004105 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4106 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4107 task->tk_status = 0;
4108 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109}
4110
Chuck Leverf0920752012-05-21 22:45:41 -04004111static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4112 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004113{
4114 __be32 verf[2];
4115
Chuck Lever2c820d92012-05-21 22:45:33 -04004116 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4117 /* An impossible timestamp guarantees this value
4118 * will never match a generated boot time. */
4119 verf[0] = 0;
4120 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4121 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004122 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4123 verf[0] = (__be32)nn->boot_time.tv_sec;
4124 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004125 }
Chuck Levercd937102012-03-02 17:14:31 -05004126 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4127}
4128
Chuck Levere984a552012-09-14 17:24:21 -04004129static unsigned int
4130nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4131 char *buf, size_t len)
4132{
4133 unsigned int result;
4134
4135 rcu_read_lock();
4136 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4137 clp->cl_ipaddr,
4138 rpc_peeraddr2str(clp->cl_rpcclient,
4139 RPC_DISPLAY_ADDR),
4140 rpc_peeraddr2str(clp->cl_rpcclient,
4141 RPC_DISPLAY_PROTO));
4142 rcu_read_unlock();
4143 return result;
4144}
4145
4146static unsigned int
4147nfs4_init_uniform_client_string(const struct nfs_client *clp,
4148 char *buf, size_t len)
4149{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004150 char *nodename = clp->cl_rpcclient->cl_nodename;
4151
4152 if (nfs4_client_id_uniquifier[0] != '\0')
4153 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004154 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4155 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004156 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004157}
4158
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004159/**
4160 * nfs4_proc_setclientid - Negotiate client ID
4161 * @clp: state data structure
4162 * @program: RPC program for NFSv4 callback service
4163 * @port: IP port number for NFS4 callback service
4164 * @cred: RPC credential to use for this call
4165 * @res: where to place the result
4166 *
4167 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4168 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004169int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4170 unsigned short port, struct rpc_cred *cred,
4171 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172{
4173 nfs4_verifier sc_verifier;
4174 struct nfs4_setclientid setclientid = {
4175 .sc_verifier = &sc_verifier,
4176 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004177 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 };
4179 struct rpc_message msg = {
4180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4181 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004182 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004183 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004185 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Chuck Leverde734832012-07-11 16:30:50 -04004187 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004188 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004189 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4190 setclientid.sc_name_len =
4191 nfs4_init_uniform_client_string(clp,
4192 setclientid.sc_name,
4193 sizeof(setclientid.sc_name));
4194 else
4195 setclientid.sc_name_len =
4196 nfs4_init_nonuniform_client_string(clp,
4197 setclientid.sc_name,
4198 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004199 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004200 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004201 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004202 sizeof(setclientid.sc_netid),
4203 rpc_peeraddr2str(clp->cl_rpcclient,
4204 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004205 rcu_read_unlock();
4206 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004207 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 clp->cl_ipaddr, port >> 8, port & 255);
4209
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004210 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4211 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4212 setclientid.sc_name_len, setclientid.sc_name);
4213 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4214 dprintk("NFS reply setclientid: %d\n", status);
4215 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216}
4217
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004218/**
4219 * nfs4_proc_setclientid_confirm - Confirm client ID
4220 * @clp: state data structure
4221 * @res: result of a previous SETCLIENTID
4222 * @cred: RPC credential to use for this call
4223 *
4224 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4225 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004226int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004227 struct nfs4_setclientid_res *arg,
4228 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
4230 struct nfs_fsinfo fsinfo;
4231 struct rpc_message msg = {
4232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004233 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004235 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 };
4237 unsigned long now;
4238 int status;
4239
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004240 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4241 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4242 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004244 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 if (status == 0) {
4246 spin_lock(&clp->cl_lock);
4247 clp->cl_lease_time = fsinfo.lease_time * HZ;
4248 clp->cl_last_renewal = now;
4249 spin_unlock(&clp->cl_lock);
4250 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004251 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 return status;
4253}
4254
Trond Myklebustfe650402006-01-03 09:55:18 +01004255struct nfs4_delegreturndata {
4256 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004257 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004258 struct nfs_fh fh;
4259 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004260 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004261 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004262 int rpc_status;
4263};
4264
Trond Myklebustfe650402006-01-03 09:55:18 +01004265static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4266{
4267 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004268
Trond Myklebust14516c32010-07-31 14:29:06 -04004269 if (!nfs4_sequence_done(task, &data->res.seq_res))
4270 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004271
Ricardo Labiaga79708862009-12-07 09:23:21 -05004272 switch (task->tk_status) {
4273 case -NFS4ERR_STALE_STATEID:
4274 case -NFS4ERR_EXPIRED:
4275 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004276 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004277 break;
4278 default:
4279 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4280 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004281 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004282 return;
4283 }
4284 }
4285 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004286}
4287
4288static void nfs4_delegreturn_release(void *calldata)
4289{
Trond Myklebustfe650402006-01-03 09:55:18 +01004290 kfree(calldata);
4291}
4292
Andy Adamson938e1012009-04-01 09:22:28 -04004293#if defined(CONFIG_NFS_V4_1)
4294static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4295{
4296 struct nfs4_delegreturndata *d_data;
4297
4298 d_data = (struct nfs4_delegreturndata *)data;
4299
Trond Myklebust035168a2010-06-16 09:52:26 -04004300 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004301 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004302 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004303 return;
4304 rpc_call_start(task);
4305}
4306#endif /* CONFIG_NFS_V4_1 */
4307
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004308static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004309#if defined(CONFIG_NFS_V4_1)
4310 .rpc_call_prepare = nfs4_delegreturn_prepare,
4311#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004312 .rpc_call_done = nfs4_delegreturn_done,
4313 .rpc_release = nfs4_delegreturn_release,
4314};
4315
Trond Myklebuste6f81072008-01-24 18:14:34 -05004316static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004317{
4318 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004319 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004320 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004321 struct rpc_message msg = {
4322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4323 .rpc_cred = cred,
4324 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004325 struct rpc_task_setup task_setup_data = {
4326 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004327 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004328 .callback_ops = &nfs4_delegreturn_ops,
4329 .flags = RPC_TASK_ASYNC,
4330 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004331 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004332
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004333 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004334 if (data == NULL)
4335 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004336 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004337 data->args.fhandle = &data->fh;
4338 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004339 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004340 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004341 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004342 data->res.fattr = &data->fattr;
4343 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004344 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004345 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004346 data->rpc_status = 0;
4347
Trond Myklebustc970aa82007-07-14 15:39:59 -04004348 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004349 msg.rpc_argp = &data->args;
4350 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004351 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004352 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004353 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004354 if (!issync)
4355 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004356 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004357 if (status != 0)
4358 goto out;
4359 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004360 if (status == 0)
4361 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4362 else
4363 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004364out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004365 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004366 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367}
4368
Trond Myklebuste6f81072008-01-24 18:14:34 -05004369int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
4371 struct nfs_server *server = NFS_SERVER(inode);
4372 struct nfs4_exception exception = { };
4373 int err;
4374 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004375 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 switch (err) {
4377 case -NFS4ERR_STALE_STATEID:
4378 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 case 0:
4380 return 0;
4381 }
4382 err = nfs4_handle_exception(server, err, &exception);
4383 } while (exception.retry);
4384 return err;
4385}
4386
4387#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4388#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4389
4390/*
4391 * sleep, with exponential backoff, and retry the LOCK operation.
4392 */
4393static unsigned long
4394nfs4_set_lock_task_retry(unsigned long timeout)
4395{
Jeff Laytond3103102011-12-01 22:44:39 +01004396 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 timeout <<= 1;
4398 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4399 return NFS4_LOCK_MAXTIMEOUT;
4400 return timeout;
4401}
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4404{
4405 struct inode *inode = state->inode;
4406 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004407 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004408 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004410 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004412 struct nfs_lockt_res res = {
4413 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 };
4415 struct rpc_message msg = {
4416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4417 .rpc_argp = &arg,
4418 .rpc_resp = &res,
4419 .rpc_cred = state->owner->so_cred,
4420 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 struct nfs4_lock_state *lsp;
4422 int status;
4423
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004424 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004425 status = nfs4_set_lock_state(state, request);
4426 if (status != 0)
4427 goto out;
4428 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004429 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004430 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004431 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004432 switch (status) {
4433 case 0:
4434 request->fl_type = F_UNLCK;
4435 break;
4436 case -NFS4ERR_DENIED:
4437 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004439 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004440out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 return status;
4442}
4443
4444static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4445{
4446 struct nfs4_exception exception = { };
4447 int err;
4448
4449 do {
4450 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4451 _nfs4_proc_getlk(state, cmd, request),
4452 &exception);
4453 } while (exception.retry);
4454 return err;
4455}
4456
4457static int do_vfs_lock(struct file *file, struct file_lock *fl)
4458{
4459 int res = 0;
4460 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4461 case FL_POSIX:
4462 res = posix_lock_file_wait(file, fl);
4463 break;
4464 case FL_FLOCK:
4465 res = flock_lock_file_wait(file, fl);
4466 break;
4467 default:
4468 BUG();
4469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 return res;
4471}
4472
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004473struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004474 struct nfs_locku_args arg;
4475 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004476 struct nfs4_lock_state *lsp;
4477 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004478 struct file_lock fl;
4479 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004480 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004481};
4482
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004483static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4484 struct nfs_open_context *ctx,
4485 struct nfs4_lock_state *lsp,
4486 struct nfs_seqid *seqid)
4487{
4488 struct nfs4_unlockdata *p;
4489 struct inode *inode = lsp->ls_state->inode;
4490
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004491 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004492 if (p == NULL)
4493 return NULL;
4494 p->arg.fh = NFS_FH(inode);
4495 p->arg.fl = &p->fl;
4496 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004497 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004498 p->arg.stateid = &lsp->ls_stateid;
4499 p->lsp = lsp;
4500 atomic_inc(&lsp->ls_count);
4501 /* Ensure we don't close file until we're done freeing locks! */
4502 p->ctx = get_nfs_open_context(ctx);
4503 memcpy(&p->fl, fl, sizeof(p->fl));
4504 p->server = NFS_SERVER(inode);
4505 return p;
4506}
4507
Trond Myklebust06f814a2006-01-03 09:55:07 +01004508static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004509{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004510 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004511 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004512 nfs4_put_lock_state(calldata->lsp);
4513 put_nfs_open_context(calldata->ctx);
4514 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004515}
4516
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004517static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004518{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004519 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004520
Trond Myklebust14516c32010-07-31 14:29:06 -04004521 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4522 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004523 switch (task->tk_status) {
4524 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004525 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4526 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004527 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004528 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004529 case -NFS4ERR_BAD_STATEID:
4530 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004531 case -NFS4ERR_STALE_STATEID:
4532 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004533 break;
4534 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004535 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004536 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004537 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004538 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004539}
4540
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004541static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004542{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004543 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004545 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004546 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004547 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004548 /* Note: exit _without_ running nfs4_locku_done */
4549 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004550 return;
4551 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004552 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004553 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004554 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004555 &calldata->res.seq_res,
4556 task) != 0)
4557 nfs_release_seqid(calldata->arg.seqid);
4558 else
4559 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560}
4561
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004562static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004563 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004564 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004565 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004566};
4567
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004568static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4569 struct nfs_open_context *ctx,
4570 struct nfs4_lock_state *lsp,
4571 struct nfs_seqid *seqid)
4572{
4573 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004574 struct rpc_message msg = {
4575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4576 .rpc_cred = ctx->cred,
4577 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004578 struct rpc_task_setup task_setup_data = {
4579 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004580 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004581 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004582 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004583 .flags = RPC_TASK_ASYNC,
4584 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004585
Frank Filz137d6ac2007-07-09 15:32:29 -07004586 /* Ensure this is an unlock - when canceling a lock, the
4587 * canceled lock is passed in, and it won't be an unlock.
4588 */
4589 fl->fl_type = F_UNLCK;
4590
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004591 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4592 if (data == NULL) {
4593 nfs_free_seqid(seqid);
4594 return ERR_PTR(-ENOMEM);
4595 }
4596
Trond Myklebust9d12b212012-01-17 22:04:25 -05004597 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004598 msg.rpc_argp = &data->arg;
4599 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004600 task_setup_data.callback_data = data;
4601 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004602}
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4605{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004606 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004607 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004608 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004609 struct rpc_task *task;
4610 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004611 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Trond Myklebust9b073572006-06-29 16:38:34 -04004613 status = nfs4_set_lock_state(state, request);
4614 /* Unlock _before_ we do the RPC call */
4615 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004616 down_read(&nfsi->rwsem);
4617 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4618 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004619 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004620 }
4621 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004622 if (status != 0)
4623 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004624 /* Is this a delegated lock? */
4625 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004626 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004627 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004628 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004629 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004630 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004631 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004632 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004633 status = PTR_ERR(task);
4634 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004635 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004636 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004637 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004638out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004639 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004640 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004643struct nfs4_lockdata {
4644 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004645 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004646 struct nfs4_lock_state *lsp;
4647 struct nfs_open_context *ctx;
4648 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004649 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004650 int rpc_status;
4651 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004652 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004653};
4654
4655static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004656 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4657 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004658{
4659 struct nfs4_lockdata *p;
4660 struct inode *inode = lsp->ls_state->inode;
4661 struct nfs_server *server = NFS_SERVER(inode);
4662
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004663 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004664 if (p == NULL)
4665 return NULL;
4666
4667 p->arg.fh = NFS_FH(inode);
4668 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004669 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004670 if (p->arg.open_seqid == NULL)
4671 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004672 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004673 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004674 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004675 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004676 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004677 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004678 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004679 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004680 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004681 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004682 atomic_inc(&lsp->ls_count);
4683 p->ctx = get_nfs_open_context(ctx);
4684 memcpy(&p->fl, fl, sizeof(p->fl));
4685 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004686out_free_seqid:
4687 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004688out_free:
4689 kfree(p);
4690 return NULL;
4691}
4692
4693static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4694{
4695 struct nfs4_lockdata *data = calldata;
4696 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Harvey Harrison3110ff82008-05-02 13:42:44 -07004698 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004699 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4700 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004701 /* Do we need to do an open_to_lock_owner? */
4702 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004703 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004704 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004705 data->arg.open_stateid = &state->stateid;
4706 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004707 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004708 } else
4709 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004710 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004711 if (nfs4_setup_sequence(data->server,
4712 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004713 &data->res.seq_res,
4714 task) == 0) {
4715 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004716 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004717 }
4718 nfs_release_seqid(data->arg.open_seqid);
4719out_release_lock_seqid:
4720 nfs_release_seqid(data->arg.lock_seqid);
4721 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004722}
4723
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004724static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4725{
4726 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4727 nfs4_lock_prepare(task, calldata);
4728}
4729
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004730static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4731{
4732 struct nfs4_lockdata *data = calldata;
4733
Harvey Harrison3110ff82008-05-02 13:42:44 -07004734 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004735
Trond Myklebust14516c32010-07-31 14:29:06 -04004736 if (!nfs4_sequence_done(task, &data->res.seq_res))
4737 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004738
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004739 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004740 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004741 if (data->rpc_status == 0)
4742 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4743 else
4744 goto out;
4745 }
4746 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004747 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004748 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004749 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004750 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004751out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004752 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004753}
4754
4755static void nfs4_lock_release(void *calldata)
4756{
4757 struct nfs4_lockdata *data = calldata;
4758
Harvey Harrison3110ff82008-05-02 13:42:44 -07004759 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004760 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004761 if (data->cancelled != 0) {
4762 struct rpc_task *task;
4763 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4764 data->arg.lock_seqid);
4765 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004766 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004767 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004768 } else
4769 nfs_free_seqid(data->arg.lock_seqid);
4770 nfs4_put_lock_state(data->lsp);
4771 put_nfs_open_context(data->ctx);
4772 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004773 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004774}
4775
4776static const struct rpc_call_ops nfs4_lock_ops = {
4777 .rpc_call_prepare = nfs4_lock_prepare,
4778 .rpc_call_done = nfs4_lock_done,
4779 .rpc_release = nfs4_lock_release,
4780};
4781
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004782static const struct rpc_call_ops nfs4_recover_lock_ops = {
4783 .rpc_call_prepare = nfs4_recover_lock_prepare,
4784 .rpc_call_done = nfs4_lock_done,
4785 .rpc_release = nfs4_lock_release,
4786};
4787
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004788static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4789{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004790 switch (error) {
4791 case -NFS4ERR_ADMIN_REVOKED:
4792 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004793 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004794 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004795 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004796 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004797 break;
4798 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004799 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004800 case -NFS4ERR_EXPIRED:
4801 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004802 };
4803}
4804
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004805static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004806{
4807 struct nfs4_lockdata *data;
4808 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004809 struct rpc_message msg = {
4810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4811 .rpc_cred = state->owner->so_cred,
4812 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004813 struct rpc_task_setup task_setup_data = {
4814 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004815 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004816 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004817 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004818 .flags = RPC_TASK_ASYNC,
4819 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004820 int ret;
4821
Harvey Harrison3110ff82008-05-02 13:42:44 -07004822 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004823 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004824 fl->fl_u.nfs4_fl.owner,
4825 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004826 if (data == NULL)
4827 return -ENOMEM;
4828 if (IS_SETLKW(cmd))
4829 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004830 if (recovery_type > NFS_LOCK_NEW) {
4831 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004832 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004833 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4834 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004835 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004836 msg.rpc_argp = &data->arg;
4837 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004838 task_setup_data.callback_data = data;
4839 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004840 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004841 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004842 ret = nfs4_wait_for_completion_rpc_task(task);
4843 if (ret == 0) {
4844 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004845 if (ret)
4846 nfs4_handle_setlk_error(data->server, data->lsp,
4847 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004848 } else
4849 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004850 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004851 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004852 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853}
4854
4855static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4856{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004857 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004858 struct nfs4_exception exception = {
4859 .inode = state->inode,
4860 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004861 int err;
4862
4863 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004864 /* Cache the lock if possible... */
4865 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4866 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004867 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004868 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004869 break;
4870 nfs4_handle_exception(server, err, &exception);
4871 } while (exception.retry);
4872 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873}
4874
4875static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4876{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004877 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004878 struct nfs4_exception exception = {
4879 .inode = state->inode,
4880 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004881 int err;
4882
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004883 err = nfs4_set_lock_state(state, request);
4884 if (err != 0)
4885 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004886 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004887 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4888 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004889 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004890 switch (err) {
4891 default:
4892 goto out;
4893 case -NFS4ERR_GRACE:
4894 case -NFS4ERR_DELAY:
4895 nfs4_handle_exception(server, err, &exception);
4896 err = 0;
4897 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004898 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004899out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004900 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901}
4902
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004903#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004904/**
4905 * nfs41_check_expired_locks - possibly free a lock stateid
4906 *
4907 * @state: NFSv4 state for an inode
4908 *
4909 * Returns NFS_OK if recovery for this stateid is now finished.
4910 * Otherwise a negative NFS4ERR value is returned.
4911 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004912static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004913{
Chuck Levereb64cf92012-07-11 16:30:05 -04004914 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004915 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004916 struct nfs_server *server = NFS_SERVER(state->inode);
4917
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004918 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004919 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004920 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4921 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004922 /* Free the stateid unless the server
4923 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004924 if (status != -NFS4ERR_BAD_STATEID)
4925 nfs41_free_stateid(server,
4926 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004927 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004928 ret = status;
4929 }
4930 }
4931 };
4932
4933 return ret;
4934}
4935
4936static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4937{
4938 int status = NFS_OK;
4939
4940 if (test_bit(LK_STATE_IN_USE, &state->flags))
4941 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004942 if (status != NFS_OK)
4943 status = nfs4_lock_expired(state, request);
4944 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004945}
4946#endif
4947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4949{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004950 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004951 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004952 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004954 if ((fl_flags & FL_POSIX) &&
4955 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4956 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004957 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004958 status = nfs4_set_lock_state(state, request);
4959 if (status != 0)
4960 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004961 request->fl_flags |= FL_ACCESS;
4962 status = do_vfs_lock(request->fl_file, request);
4963 if (status < 0)
4964 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004965 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004966 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004967 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004968 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004969 request->fl_flags = fl_flags & ~FL_SLEEP;
4970 status = do_vfs_lock(request->fl_file, request);
4971 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004972 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004973 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004974 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004975 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004976 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004977 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004978 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004979 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4980 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004981out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004982 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004983out:
4984 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 return status;
4986}
4987
4988static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4989{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004990 struct nfs4_exception exception = {
4991 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004992 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004993 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 int err;
4995
4996 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004997 err = _nfs4_proc_setlk(state, cmd, request);
4998 if (err == -NFS4ERR_DENIED)
4999 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005001 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 } while (exception.retry);
5003 return err;
5004}
5005
5006static int
5007nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5008{
5009 struct nfs_open_context *ctx;
5010 struct nfs4_state *state;
5011 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5012 int status;
5013
5014 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005015 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 state = ctx->state;
5017
5018 if (request->fl_start < 0 || request->fl_end < 0)
5019 return -EINVAL;
5020
Trond Myklebustd9531262009-07-21 19:22:38 -04005021 if (IS_GETLK(cmd)) {
5022 if (state != NULL)
5023 return nfs4_proc_getlk(state, F_GETLK, request);
5024 return 0;
5025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
5027 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5028 return -EINVAL;
5029
Trond Myklebustd9531262009-07-21 19:22:38 -04005030 if (request->fl_type == F_UNLCK) {
5031 if (state != NULL)
5032 return nfs4_proc_unlck(state, cmd, request);
5033 return 0;
5034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
Trond Myklebustd9531262009-07-21 19:22:38 -04005036 if (state == NULL)
5037 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005038 /*
5039 * Don't rely on the VFS having checked the file open mode,
5040 * since it won't do this for flock() locks.
5041 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005042 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005043 case F_RDLCK:
5044 if (!(filp->f_mode & FMODE_READ))
5045 return -EBADF;
5046 break;
5047 case F_WRLCK:
5048 if (!(filp->f_mode & FMODE_WRITE))
5049 return -EBADF;
5050 }
5051
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 do {
5053 status = nfs4_proc_setlk(state, cmd, request);
5054 if ((status != -EAGAIN) || IS_SETLK(cmd))
5055 break;
5056 timeout = nfs4_set_lock_task_retry(timeout);
5057 status = -ERESTARTSYS;
5058 if (signalled())
5059 break;
5060 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 return status;
5062}
5063
Trond Myklebust888e6942005-11-04 15:38:11 -05005064int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5065{
5066 struct nfs_server *server = NFS_SERVER(state->inode);
5067 struct nfs4_exception exception = { };
5068 int err;
5069
5070 err = nfs4_set_lock_state(state, fl);
5071 if (err != 0)
5072 goto out;
5073 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005074 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005075 switch (err) {
5076 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005077 printk(KERN_ERR "NFS: %s: unhandled error "
5078 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005079 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005080 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005081 goto out;
5082 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005083 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005084 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005085 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005086 nfs4_schedule_lease_recovery(server->nfs_client);
5087 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005088 case -NFS4ERR_BADSESSION:
5089 case -NFS4ERR_BADSLOT:
5090 case -NFS4ERR_BAD_HIGH_SLOT:
5091 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5092 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005093 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005094 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005095 case -ERESTARTSYS:
5096 /*
5097 * The show must go on: exit, but mark the
5098 * stateid as needing recovery.
5099 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005100 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005101 case -NFS4ERR_ADMIN_REVOKED:
5102 case -NFS4ERR_BAD_STATEID:
5103 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005104 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005105 err = 0;
5106 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005107 case -EKEYEXPIRED:
5108 /*
5109 * User RPCSEC_GSS context has expired.
5110 * We cannot recover this stateid now, so
5111 * skip it and allow recovery thread to
5112 * proceed.
5113 */
5114 err = 0;
5115 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005116 case -ENOMEM:
5117 case -NFS4ERR_DENIED:
5118 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5119 err = 0;
5120 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005121 case -NFS4ERR_DELAY:
5122 break;
5123 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005124 err = nfs4_handle_exception(server, err, &exception);
5125 } while (exception.retry);
5126out:
5127 return err;
5128}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005129
Trond Myklebustcf470c32012-03-07 13:49:12 -05005130struct nfs_release_lockowner_data {
5131 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005132 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005133 struct nfs_release_lockowner_args args;
5134};
5135
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005136static void nfs4_release_lockowner_release(void *calldata)
5137{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005138 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005139 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005140 kfree(calldata);
5141}
5142
Trond Myklebust17280172012-03-11 13:11:00 -04005143static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005144 .rpc_release = nfs4_release_lockowner_release,
5145};
5146
Trond Myklebustcf470c32012-03-07 13:49:12 -05005147int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005148{
5149 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005150 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005151 struct rpc_message msg = {
5152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5153 };
5154
5155 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005156 return -EINVAL;
5157 data = kmalloc(sizeof(*data), GFP_NOFS);
5158 if (!data)
5159 return -ENOMEM;
5160 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005161 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005162 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5163 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5164 data->args.lock_owner.s_dev = server->s_dev;
5165 msg.rpc_argp = &data->args;
5166 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5167 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005168}
5169
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005170#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5171
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005172static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5173 const void *buf, size_t buflen,
5174 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005175{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005176 if (strcmp(key, "") != 0)
5177 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005178
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005179 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005180}
5181
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005182static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5183 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005184{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005185 if (strcmp(key, "") != 0)
5186 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005187
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005188 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005189}
5190
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005191static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5192 size_t list_len, const char *name,
5193 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005194{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005195 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005196
J. Bruce Fields096455a2006-03-20 23:23:42 -05005197 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5198 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005199
5200 if (list && len <= list_len)
5201 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005202 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005203}
5204
Andy Adamson533eb462011-06-13 18:25:56 -04005205/*
5206 * nfs_fhget will use either the mounted_on_fileid or the fileid
5207 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005208static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5209{
Andy Adamson533eb462011-06-13 18:25:56 -04005210 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5211 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5212 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005213 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005214 return;
5215
5216 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005217 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005218 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5219 fattr->nlink = 2;
5220}
5221
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005222static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5223 const struct qstr *name,
5224 struct nfs4_fs_locations *fs_locations,
5225 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005226{
5227 struct nfs_server *server = NFS_SERVER(dir);
5228 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005229 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005230 };
5231 struct nfs4_fs_locations_arg args = {
5232 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005233 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005234 .page = page,
5235 .bitmask = bitmask,
5236 };
Benny Halevy22958462009-04-01 09:22:02 -04005237 struct nfs4_fs_locations_res res = {
5238 .fs_locations = fs_locations,
5239 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005240 struct rpc_message msg = {
5241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5242 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005243 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005244 };
5245 int status;
5246
Harvey Harrison3110ff82008-05-02 13:42:44 -07005247 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005248
5249 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5250 * is not supported */
5251 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5252 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5253 else
5254 bitmask[0] |= FATTR4_WORD0_FILEID;
5255
Trond Myklebustc228fd32007-01-13 02:28:11 -05005256 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005257 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005258 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005259 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005260 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005261 return status;
5262}
5263
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005264int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5265 const struct qstr *name,
5266 struct nfs4_fs_locations *fs_locations,
5267 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005268{
5269 struct nfs4_exception exception = { };
5270 int err;
5271 do {
5272 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005273 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005274 &exception);
5275 } while (exception.retry);
5276 return err;
5277}
5278
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005279static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5280{
5281 int status;
5282 struct nfs4_secinfo_arg args = {
5283 .dir_fh = NFS_FH(dir),
5284 .name = name,
5285 };
5286 struct nfs4_secinfo_res res = {
5287 .flavors = flavors,
5288 };
5289 struct rpc_message msg = {
5290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5291 .rpc_argp = &args,
5292 .rpc_resp = &res,
5293 };
5294
5295 dprintk("NFS call secinfo %s\n", name->name);
5296 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5297 dprintk("NFS reply secinfo: %d\n", status);
5298 return status;
5299}
5300
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005301int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5302 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005303{
5304 struct nfs4_exception exception = { };
5305 int err;
5306 do {
5307 err = nfs4_handle_exception(NFS_SERVER(dir),
5308 _nfs4_proc_secinfo(dir, name, flavors),
5309 &exception);
5310 } while (exception.retry);
5311 return err;
5312}
5313
Andy Adamson557134a2009-04-01 09:21:53 -04005314#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005315/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005316 * Check the exchange flags returned by the server for invalid flags, having
5317 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5318 * DS flags set.
5319 */
5320static int nfs4_check_cl_exchange_flags(u32 flags)
5321{
5322 if (flags & ~EXCHGID4_FLAG_MASK_R)
5323 goto out_inval;
5324 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5325 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5326 goto out_inval;
5327 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5328 goto out_inval;
5329 return NFS_OK;
5330out_inval:
5331 return -NFS4ERR_INVAL;
5332}
5333
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005334static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005335nfs41_same_server_scope(struct nfs41_server_scope *a,
5336 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005337{
5338 if (a->server_scope_sz == b->server_scope_sz &&
5339 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5340 return true;
5341
5342 return false;
5343}
5344
Andy Adamson357f54d2010-12-14 10:11:57 -05005345/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005346 * nfs4_proc_bind_conn_to_session()
5347 *
5348 * The 4.1 client currently uses the same TCP connection for the
5349 * fore and backchannel.
5350 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005351int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005352{
5353 int status;
5354 struct nfs41_bind_conn_to_session_res res;
5355 struct rpc_message msg = {
5356 .rpc_proc =
5357 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5358 .rpc_argp = clp,
5359 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005360 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005361 };
5362
5363 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005364
5365 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5366 if (unlikely(res.session == NULL)) {
5367 status = -ENOMEM;
5368 goto out;
5369 }
5370
5371 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5372 if (status == 0) {
5373 if (memcmp(res.session->sess_id.data,
5374 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5375 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5376 status = -EIO;
5377 goto out_session;
5378 }
5379 if (res.dir != NFS4_CDFS4_BOTH) {
5380 dprintk("NFS: %s: Unexpected direction from server\n",
5381 __func__);
5382 status = -EIO;
5383 goto out_session;
5384 }
5385 if (res.use_conn_in_rdma_mode) {
5386 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5387 __func__);
5388 status = -EIO;
5389 goto out_session;
5390 }
5391 }
5392out_session:
5393 kfree(res.session);
5394out:
5395 dprintk("<-- %s status= %d\n", __func__, status);
5396 return status;
5397}
5398
5399/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005400 * nfs4_proc_exchange_id()
5401 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005402 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5403 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005404 * Since the clientid has expired, all compounds using sessions
5405 * associated with the stale clientid will be returning
5406 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5407 * be in some phase of session reset.
5408 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005409int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005410{
5411 nfs4_verifier verifier;
5412 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005413 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005414 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005415 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005416 };
5417 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005418 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005419 };
5420 int status;
5421 struct rpc_message msg = {
5422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5423 .rpc_argp = &args,
5424 .rpc_resp = &res,
5425 .rpc_cred = cred,
5426 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005427
Chuck Leverf0920752012-05-21 22:45:41 -04005428 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005429 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5430 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005431 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5432 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5433 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005434
Chuck Leveracdeb692012-05-21 22:46:16 -04005435 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005436 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005437 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005438 status = -ENOMEM;
5439 goto out;
5440 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005441
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005442 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005443 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005444 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005445 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005446 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005447 }
5448
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005449 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005450 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005451 status = -ENOMEM;
5452 goto out_server_scope;
5453 }
5454
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005455 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005456 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005457 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005458
Chuck Lever177313f2012-05-21 22:44:58 -04005459 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005460 clp->cl_clientid = res.clientid;
5461 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5462 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5463 clp->cl_seqid = res.seqid;
5464
Chuck Leveracdeb692012-05-21 22:46:16 -04005465 kfree(clp->cl_serverowner);
5466 clp->cl_serverowner = res.server_owner;
5467 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005468
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005469 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005470 kfree(clp->cl_implid);
5471 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005472
Chuck Lever177313f2012-05-21 22:44:58 -04005473 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005474 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005475 res.server_scope)) {
5476 dprintk("%s: server_scope mismatch detected\n",
5477 __func__);
5478 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005479 kfree(clp->cl_serverscope);
5480 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005481 }
5482
Chuck Lever177313f2012-05-21 22:44:58 -04005483 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005484 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005485 goto out;
5486 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005487 } else
5488 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005489
Chuck Leveracdeb692012-05-21 22:46:16 -04005490out_server_owner:
5491 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005492out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005493 kfree(res.server_scope);
5494out:
Chuck Lever177313f2012-05-21 22:44:58 -04005495 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005496 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005497 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005498 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005499 clp->cl_implid->date.seconds,
5500 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005501 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005502 return status;
5503}
5504
Trond Myklebust66245532012-05-25 17:18:09 -04005505static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5506 struct rpc_cred *cred)
5507{
5508 struct rpc_message msg = {
5509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5510 .rpc_argp = clp,
5511 .rpc_cred = cred,
5512 };
5513 int status;
5514
5515 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5516 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005517 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005518 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5519 return status;
5520}
5521
5522static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5523 struct rpc_cred *cred)
5524{
5525 unsigned int loop;
5526 int ret;
5527
5528 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5529 ret = _nfs4_proc_destroy_clientid(clp, cred);
5530 switch (ret) {
5531 case -NFS4ERR_DELAY:
5532 case -NFS4ERR_CLIENTID_BUSY:
5533 ssleep(1);
5534 break;
5535 default:
5536 return ret;
5537 }
5538 }
5539 return 0;
5540}
5541
5542int nfs4_destroy_clientid(struct nfs_client *clp)
5543{
5544 struct rpc_cred *cred;
5545 int ret = 0;
5546
5547 if (clp->cl_mvops->minor_version < 1)
5548 goto out;
5549 if (clp->cl_exchange_flags == 0)
5550 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005551 if (clp->cl_preserve_clid)
5552 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005553 cred = nfs4_get_exchange_id_cred(clp);
5554 ret = nfs4_proc_destroy_clientid(clp, cred);
5555 if (cred)
5556 put_rpccred(cred);
5557 switch (ret) {
5558 case 0:
5559 case -NFS4ERR_STALE_CLIENTID:
5560 clp->cl_exchange_flags = 0;
5561 }
5562out:
5563 return ret;
5564}
5565
Andy Adamson2050f0c2009-04-01 09:22:30 -04005566struct nfs4_get_lease_time_data {
5567 struct nfs4_get_lease_time_args *args;
5568 struct nfs4_get_lease_time_res *res;
5569 struct nfs_client *clp;
5570};
5571
5572static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5573 void *calldata)
5574{
5575 int ret;
5576 struct nfs4_get_lease_time_data *data =
5577 (struct nfs4_get_lease_time_data *)calldata;
5578
5579 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005580 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005581 /* just setup sequence, do not trigger session recovery
5582 since we're invoked within one */
5583 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005584 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005585 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005586
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005587 if (ret != -EAGAIN)
5588 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005589 dprintk("<-- %s\n", __func__);
5590}
5591
5592/*
5593 * Called from nfs4_state_manager thread for session setup, so don't recover
5594 * from sequence operation or clientid errors.
5595 */
5596static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5597{
5598 struct nfs4_get_lease_time_data *data =
5599 (struct nfs4_get_lease_time_data *)calldata;
5600
5601 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005602 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5603 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005604 switch (task->tk_status) {
5605 case -NFS4ERR_DELAY:
5606 case -NFS4ERR_GRACE:
5607 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5608 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5609 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005610 /* fall through */
5611 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005612 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005613 return;
5614 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005615 dprintk("<-- %s\n", __func__);
5616}
5617
Trond Myklebust17280172012-03-11 13:11:00 -04005618static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005619 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5620 .rpc_call_done = nfs4_get_lease_time_done,
5621};
5622
5623int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5624{
5625 struct rpc_task *task;
5626 struct nfs4_get_lease_time_args args;
5627 struct nfs4_get_lease_time_res res = {
5628 .lr_fsinfo = fsinfo,
5629 };
5630 struct nfs4_get_lease_time_data data = {
5631 .args = &args,
5632 .res = &res,
5633 .clp = clp,
5634 };
5635 struct rpc_message msg = {
5636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5637 .rpc_argp = &args,
5638 .rpc_resp = &res,
5639 };
5640 struct rpc_task_setup task_setup = {
5641 .rpc_client = clp->cl_rpcclient,
5642 .rpc_message = &msg,
5643 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005644 .callback_data = &data,
5645 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005646 };
5647 int status;
5648
Trond Myklebust9d12b212012-01-17 22:04:25 -05005649 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005650 dprintk("--> %s\n", __func__);
5651 task = rpc_run_task(&task_setup);
5652
5653 if (IS_ERR(task))
5654 status = PTR_ERR(task);
5655 else {
5656 status = task->tk_status;
5657 rpc_put_task(task);
5658 }
5659 dprintk("<-- %s return %d\n", __func__, status);
5660
5661 return status;
5662}
5663
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005664struct nfs4_slot *nfs4_alloc_slots(struct nfs4_slot_table *table,
5665 u32 max_slots, gfp_t gfp_flags)
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005666{
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005667 struct nfs4_slot *tbl;
5668 u32 i;
5669
5670 tbl = kmalloc_array(max_slots, sizeof(*tbl), gfp_flags);
5671 if (tbl != NULL) {
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005672 for (i = 0; i < max_slots; i++) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005673 tbl[i].table = table;
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005674 tbl[i].slot_nr = i;
5675 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005676 }
5677 return tbl;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005678}
5679
5680static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5681 struct nfs4_slot *new,
5682 u32 max_slots,
5683 u32 ivalue)
5684{
5685 struct nfs4_slot *old = NULL;
5686 u32 i;
5687
5688 spin_lock(&tbl->slot_tbl_lock);
5689 if (new) {
5690 old = tbl->slots;
5691 tbl->slots = new;
5692 tbl->max_slots = max_slots;
5693 }
Trond Myklebustf9b1ef52012-10-29 16:48:40 -04005694 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005695 for (i = 0; i < tbl->max_slots; i++)
5696 tbl->slots[i].seq_nr = ivalue;
5697 spin_unlock(&tbl->slot_tbl_lock);
5698 kfree(old);
5699}
5700
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005701/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005702 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005703 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005704static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5705 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005706{
Andy Adamson104aeba2010-01-14 17:45:10 -05005707 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005708 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005709
Andy Adamson104aeba2010-01-14 17:45:10 -05005710 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5711 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005712
Andy Adamson104aeba2010-01-14 17:45:10 -05005713 /* Does the newly negotiated max_reqs match the existing slot table? */
5714 if (max_reqs != tbl->max_slots) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005715 new = nfs4_alloc_slots(tbl, max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005716 if (!new)
5717 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005718 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005719 ret = 0;
5720
5721 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005722 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5723 tbl, tbl->slots, tbl->max_slots);
5724out:
5725 dprintk("<-- %s: return %d\n", __func__, ret);
5726 return ret;
5727}
5728
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005729/* Destroy the slot table */
5730static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5731{
5732 if (session->fc_slot_table.slots != NULL) {
5733 kfree(session->fc_slot_table.slots);
5734 session->fc_slot_table.slots = NULL;
5735 }
5736 if (session->bc_slot_table.slots != NULL) {
5737 kfree(session->bc_slot_table.slots);
5738 session->bc_slot_table.slots = NULL;
5739 }
5740 return;
5741}
5742
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005743/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005744 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005745 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005746static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005747{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005748 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005749 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005750
Andy Adamsonaacd5532011-11-09 13:58:21 -05005751 dprintk("--> %s\n", __func__);
5752 /* Fore channel */
5753 tbl = &ses->fc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005754 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005755 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5756 if (status) /* -ENOMEM */
5757 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005758 /* Back channel */
5759 tbl = &ses->bc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005760 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005761 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5762 if (status && tbl->slots == NULL)
5763 /* Fore and back channel share a connection so get
5764 * both slot tables or neither */
5765 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005766 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005767}
5768
5769struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5770{
5771 struct nfs4_session *session;
5772 struct nfs4_slot_table *tbl;
5773
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005774 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005775 if (!session)
5776 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005777
Andy Adamson557134a2009-04-01 09:21:53 -04005778 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005779 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005780 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005781 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005782 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005783
5784 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005785 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005786 spin_lock_init(&tbl->slot_tbl_lock);
5787 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005788 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005789
Trond Myklebust1055d762010-06-16 09:52:27 -04005790 session->session_state = 1<<NFS4_SESSION_INITING;
5791
Andy Adamson557134a2009-04-01 09:21:53 -04005792 session->clp = clp;
5793 return session;
5794}
5795
5796void nfs4_destroy_session(struct nfs4_session *session)
5797{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005798 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005799 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005800
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005801 cred = nfs4_get_exchange_id_cred(session->clp);
5802 nfs4_proc_destroy_session(session, cred);
5803 if (cred)
5804 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005805
5806 rcu_read_lock();
5807 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5808 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005809 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005810 __func__, xprt);
5811 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005812 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005813 kfree(session);
5814}
5815
Andy Adamsonfc931582009-04-01 09:22:31 -04005816/*
5817 * Initialize the values to be used by the client in CREATE_SESSION
5818 * If nfs4_init_session set the fore channel request and response sizes,
5819 * use them.
5820 *
5821 * Set the back channel max_resp_sz_cached to zero to force the client to
5822 * always set csa_cachethis to FALSE because the current implementation
5823 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5824 */
5825static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5826{
5827 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005828 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5829 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005830
5831 if (mxrqst_sz == 0)
5832 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5833 if (mxresp_sz == 0)
5834 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5835 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005836 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5837 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005838 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005839 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005840
5841 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005842 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005843 __func__,
5844 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005845 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005846
5847 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005848 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5849 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5850 args->bc_attrs.max_resp_sz_cached = 0;
5851 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5852 args->bc_attrs.max_reqs = 1;
5853
5854 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5855 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5856 __func__,
5857 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5858 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5859 args->bc_attrs.max_reqs);
5860}
5861
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005862static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005863{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005864 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5865 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5866
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005867 if (rcvd->max_resp_sz > sent->max_resp_sz)
5868 return -EINVAL;
5869 /*
5870 * Our requested max_ops is the minimum we need; we're not
5871 * prepared to break up compounds into smaller pieces than that.
5872 * So, no point even trying to continue if the server won't
5873 * cooperate:
5874 */
5875 if (rcvd->max_ops < sent->max_ops)
5876 return -EINVAL;
5877 if (rcvd->max_reqs == 0)
5878 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005879 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5880 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005881 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005882}
5883
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005884static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5885{
5886 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5887 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005888
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005889 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5890 return -EINVAL;
5891 if (rcvd->max_resp_sz < sent->max_resp_sz)
5892 return -EINVAL;
5893 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5894 return -EINVAL;
5895 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005896 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005897 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005898 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005899 return -EINVAL;
5900 return 0;
5901}
Andy Adamson8d353012009-04-01 09:22:32 -04005902
Andy Adamson8d353012009-04-01 09:22:32 -04005903static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5904 struct nfs4_session *session)
5905{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005906 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005907
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005908 ret = nfs4_verify_fore_channel_attrs(args, session);
5909 if (ret)
5910 return ret;
5911 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005912}
5913
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005914static int _nfs4_proc_create_session(struct nfs_client *clp,
5915 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005916{
5917 struct nfs4_session *session = clp->cl_session;
5918 struct nfs41_create_session_args args = {
5919 .client = clp,
5920 .cb_program = NFS4_CALLBACK,
5921 };
5922 struct nfs41_create_session_res res = {
5923 .client = clp,
5924 };
5925 struct rpc_message msg = {
5926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5927 .rpc_argp = &args,
5928 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005929 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005930 };
5931 int status;
5932
5933 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005934 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005935
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005936 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005937
Trond Myklebust43095d32012-11-20 11:13:12 -05005938 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005939 /* Verify the session's negotiated channel_attrs values */
5940 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005941 /* Increment the clientid slot sequence id */
5942 clp->cl_seqid++;
5943 }
5944
5945 return status;
5946}
5947
5948/*
5949 * Issues a CREATE_SESSION operation to the server.
5950 * It is the responsibility of the caller to verify the session is
5951 * expired before calling this routine.
5952 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005953int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005954{
5955 int status;
5956 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005957 struct nfs4_session *session = clp->cl_session;
5958
5959 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5960
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005961 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005962 if (status)
5963 goto out;
5964
Andy Adamsonaacd5532011-11-09 13:58:21 -05005965 /* Init or reset the session slot tables */
5966 status = nfs4_setup_session_slot_tables(session);
5967 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005968 if (status)
5969 goto out;
5970
5971 ptr = (unsigned *)&session->sess_id.data[0];
5972 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5973 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005974out:
5975 dprintk("<-- %s\n", __func__);
5976 return status;
5977}
5978
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005979/*
5980 * Issue the over-the-wire RPC DESTROY_SESSION.
5981 * The caller must serialize access to this routine.
5982 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005983int nfs4_proc_destroy_session(struct nfs4_session *session,
5984 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005985{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005986 struct rpc_message msg = {
5987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5988 .rpc_argp = session,
5989 .rpc_cred = cred,
5990 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005991 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005992
5993 dprintk("--> nfs4_proc_destroy_session\n");
5994
5995 /* session is still being setup */
5996 if (session->clp->cl_cons_state != NFS_CS_READY)
5997 return status;
5998
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005999 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006000
6001 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006002 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006003 "Session has been destroyed regardless...\n", status);
6004
6005 dprintk("<-- nfs4_proc_destroy_session\n");
6006 return status;
6007}
6008
Trond Myklebust7b38c362012-05-23 13:23:31 -04006009/*
6010 * With sessions, the client is not marked ready until after a
6011 * successful EXCHANGE_ID and CREATE_SESSION.
6012 *
6013 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6014 * other versions of NFS can be tried.
6015 */
6016static int nfs41_check_session_ready(struct nfs_client *clp)
6017{
6018 int ret;
6019
6020 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6021 ret = nfs4_client_recover_expired_lease(clp);
6022 if (ret)
6023 return ret;
6024 }
6025 if (clp->cl_cons_state < NFS_CS_READY)
6026 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006027 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006028 return 0;
6029}
6030
Trond Myklebustfccba802009-07-21 16:48:07 -04006031int nfs4_init_session(struct nfs_server *server)
6032{
6033 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006034 struct nfs4_session *session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006035 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6036 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
Trond Myklebustfccba802009-07-21 16:48:07 -04006037
6038 if (!nfs4_has_session(clp))
6039 return 0;
6040
Trond Myklebustae72ae672012-11-20 11:02:55 -05006041 if (server->rsize != 0)
6042 target_max_resp_sz = server->rsize;
6043 target_max_resp_sz += nfs41_maxread_overhead;
6044
6045 if (server->wsize != 0)
6046 target_max_rqst_sz = server->wsize;
6047 target_max_rqst_sz += nfs41_maxwrite_overhead;
6048
Trond Myklebust1055d762010-06-16 09:52:27 -04006049 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006050 spin_lock(&clp->cl_lock);
6051 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebustae72ae672012-11-20 11:02:55 -05006052 /* Initialise targets and channel attributes */
6053 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6054 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6055 session->fc_target_max_resp_sz = target_max_resp_sz;
6056 session->fc_attrs.max_resp_sz = target_max_resp_sz;
Trond Myklebust688a9022012-11-20 10:53:39 -05006057 } else {
6058 /* Just adjust the targets */
6059 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6060 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6061 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6062 }
6063 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6064 session->fc_target_max_resp_sz = target_max_resp_sz;
6065 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6066 }
Trond Myklebust7b38c362012-05-23 13:23:31 -04006067 }
6068 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006069
Trond Myklebust688a9022012-11-20 10:53:39 -05006070 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6071 nfs4_schedule_lease_recovery(clp);
6072
Trond Myklebust7b38c362012-05-23 13:23:31 -04006073 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006074}
6075
Trond Myklebust7b38c362012-05-23 13:23:31 -04006076int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006077{
6078 struct nfs4_session *session = clp->cl_session;
6079 int ret;
6080
Trond Myklebust7b38c362012-05-23 13:23:31 -04006081 spin_lock(&clp->cl_lock);
6082 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6083 /*
6084 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6085 * DS lease to be equal to the MDS lease.
6086 */
6087 clp->cl_lease_time = lease_time;
6088 clp->cl_last_renewal = jiffies;
6089 }
6090 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006091
Trond Myklebust7b38c362012-05-23 13:23:31 -04006092 ret = nfs41_check_session_ready(clp);
6093 if (ret)
6094 return ret;
6095 /* Test for the DS role */
6096 if (!is_ds_client(clp))
6097 return -ENODEV;
6098 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006099}
6100EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6101
6102
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006103/*
6104 * Renew the cl_session lease.
6105 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006106struct nfs4_sequence_data {
6107 struct nfs_client *clp;
6108 struct nfs4_sequence_args args;
6109 struct nfs4_sequence_res res;
6110};
6111
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006112static void nfs41_sequence_release(void *data)
6113{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006114 struct nfs4_sequence_data *calldata = data;
6115 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006116
Alexandros Batsakis71358402010-02-05 03:45:05 -08006117 if (atomic_read(&clp->cl_count) > 1)
6118 nfs4_schedule_state_renewal(clp);
6119 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006120 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006121}
6122
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006123static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6124{
6125 switch(task->tk_status) {
6126 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006127 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6128 return -EAGAIN;
6129 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006130 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006131 }
6132 return 0;
6133}
6134
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006135static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006136{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006137 struct nfs4_sequence_data *calldata = data;
6138 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006139
Trond Myklebust14516c32010-07-31 14:29:06 -04006140 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6141 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006142
6143 if (task->tk_status < 0) {
6144 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006145 if (atomic_read(&clp->cl_count) == 1)
6146 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006147
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006148 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6149 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006150 return;
6151 }
6152 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006153 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006154out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006155 dprintk("<-- %s\n", __func__);
6156}
6157
6158static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6159{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006160 struct nfs4_sequence_data *calldata = data;
6161 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006162 struct nfs4_sequence_args *args;
6163 struct nfs4_sequence_res *res;
6164
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006165 args = task->tk_msg.rpc_argp;
6166 res = task->tk_msg.rpc_resp;
6167
Trond Myklebust9d12b212012-01-17 22:04:25 -05006168 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006169 return;
6170 rpc_call_start(task);
6171}
6172
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006173static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6174{
6175 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6176 nfs41_sequence_prepare(task, data);
6177}
6178
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006179static const struct rpc_call_ops nfs41_sequence_ops = {
6180 .rpc_call_done = nfs41_sequence_call_done,
6181 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006182 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006183};
6184
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006185static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6186 .rpc_call_done = nfs41_sequence_call_done,
6187 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6188 .rpc_release = nfs41_sequence_release,
6189};
6190
6191static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6192 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006193{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006194 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006195 struct rpc_message msg = {
6196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6197 .rpc_cred = cred,
6198 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006199 struct rpc_task_setup task_setup_data = {
6200 .rpc_client = clp->cl_rpcclient,
6201 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006202 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006203 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6204 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006205
Alexandros Batsakis71358402010-02-05 03:45:05 -08006206 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006207 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006208 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006209 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006210 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006211 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006212 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006213 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006214 msg.rpc_argp = &calldata->args;
6215 msg.rpc_resp = &calldata->res;
6216 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006217 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006218
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006219 return rpc_run_task(&task_setup_data);
6220}
6221
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006222static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006223{
6224 struct rpc_task *task;
6225 int ret = 0;
6226
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006227 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6228 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006229 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006230 if (IS_ERR(task))
6231 ret = PTR_ERR(task);
6232 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006233 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006234 dprintk("<-- %s status=%d\n", __func__, ret);
6235 return ret;
6236}
6237
6238static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6239{
6240 struct rpc_task *task;
6241 int ret;
6242
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006243 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006244 if (IS_ERR(task)) {
6245 ret = PTR_ERR(task);
6246 goto out;
6247 }
6248 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006249 if (!ret) {
6250 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6251
6252 if (task->tk_status == 0)
6253 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006254 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006255 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006256 rpc_put_task(task);
6257out:
6258 dprintk("<-- %s status=%d\n", __func__, ret);
6259 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006260}
6261
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006262struct nfs4_reclaim_complete_data {
6263 struct nfs_client *clp;
6264 struct nfs41_reclaim_complete_args arg;
6265 struct nfs41_reclaim_complete_res res;
6266};
6267
6268static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6269{
6270 struct nfs4_reclaim_complete_data *calldata = data;
6271
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006272 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006273 if (nfs41_setup_sequence(calldata->clp->cl_session,
6274 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006275 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006276 return;
6277
6278 rpc_call_start(task);
6279}
6280
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006281static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6282{
6283 switch(task->tk_status) {
6284 case 0:
6285 case -NFS4ERR_COMPLETE_ALREADY:
6286 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6287 break;
6288 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006289 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006290 /* fall through */
6291 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006292 return -EAGAIN;
6293 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006294 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006295 }
6296 return 0;
6297}
6298
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006299static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6300{
6301 struct nfs4_reclaim_complete_data *calldata = data;
6302 struct nfs_client *clp = calldata->clp;
6303 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6304
6305 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006306 if (!nfs41_sequence_done(task, res))
6307 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006308
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006309 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6310 rpc_restart_call_prepare(task);
6311 return;
6312 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006313 dprintk("<-- %s\n", __func__);
6314}
6315
6316static void nfs4_free_reclaim_complete_data(void *data)
6317{
6318 struct nfs4_reclaim_complete_data *calldata = data;
6319
6320 kfree(calldata);
6321}
6322
6323static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6324 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6325 .rpc_call_done = nfs4_reclaim_complete_done,
6326 .rpc_release = nfs4_free_reclaim_complete_data,
6327};
6328
6329/*
6330 * Issue a global reclaim complete.
6331 */
6332static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6333{
6334 struct nfs4_reclaim_complete_data *calldata;
6335 struct rpc_task *task;
6336 struct rpc_message msg = {
6337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6338 };
6339 struct rpc_task_setup task_setup_data = {
6340 .rpc_client = clp->cl_rpcclient,
6341 .rpc_message = &msg,
6342 .callback_ops = &nfs4_reclaim_complete_call_ops,
6343 .flags = RPC_TASK_ASYNC,
6344 };
6345 int status = -ENOMEM;
6346
6347 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006348 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006349 if (calldata == NULL)
6350 goto out;
6351 calldata->clp = clp;
6352 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006353
Trond Myklebust9d12b212012-01-17 22:04:25 -05006354 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006355 msg.rpc_argp = &calldata->arg;
6356 msg.rpc_resp = &calldata->res;
6357 task_setup_data.callback_data = calldata;
6358 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006359 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006360 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006361 goto out;
6362 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006363 status = nfs4_wait_for_completion_rpc_task(task);
6364 if (status == 0)
6365 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006366 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006367 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006368out:
6369 dprintk("<-- %s status=%d\n", __func__, status);
6370 return status;
6371}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006372
6373static void
6374nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6375{
6376 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006377 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006378
6379 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006380 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6381 * right now covering the LAYOUTGET we are about to send.
6382 * However, that is not so catastrophic, and there seems
6383 * to be no way to prevent it completely.
6384 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006385 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006386 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006387 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006388 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6389 NFS_I(lgp->args.inode)->layout,
6390 lgp->args.ctx->state)) {
6391 rpc_exit(task, NFS4_OK);
6392 return;
6393 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006394 rpc_call_start(task);
6395}
6396
6397static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6398{
6399 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006400 struct inode *inode = lgp->args.inode;
6401 struct nfs_server *server = NFS_SERVER(inode);
6402 struct pnfs_layout_hdr *lo;
6403 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006404
6405 dprintk("--> %s\n", __func__);
6406
6407 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006408 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006409
6410 switch (task->tk_status) {
6411 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006412 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006413 case -NFS4ERR_LAYOUTTRYLATER:
6414 case -NFS4ERR_RECALLCONFLICT:
6415 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006416 break;
6417 case -NFS4ERR_EXPIRED:
6418 case -NFS4ERR_BAD_STATEID:
6419 spin_lock(&inode->i_lock);
6420 lo = NFS_I(inode)->layout;
6421 if (!lo || list_empty(&lo->plh_segs)) {
6422 spin_unlock(&inode->i_lock);
6423 /* If the open stateid was bad, then recover it. */
6424 state = lgp->args.ctx->state;
6425 } else {
6426 LIST_HEAD(head);
6427
6428 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6429 spin_unlock(&inode->i_lock);
6430 /* Mark the bad layout state as invalid, then
6431 * retry using the open stateid. */
6432 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006433 }
6434 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006435 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6436 rpc_restart_call_prepare(task);
6437out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006438 dprintk("<-- %s\n", __func__);
6439}
6440
Idan Kedar85541162012-08-02 11:47:10 +03006441static size_t max_response_pages(struct nfs_server *server)
6442{
6443 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6444 return nfs_page_array_len(0, max_resp_sz);
6445}
6446
6447static void nfs4_free_pages(struct page **pages, size_t size)
6448{
6449 int i;
6450
6451 if (!pages)
6452 return;
6453
6454 for (i = 0; i < size; i++) {
6455 if (!pages[i])
6456 break;
6457 __free_page(pages[i]);
6458 }
6459 kfree(pages);
6460}
6461
6462static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6463{
6464 struct page **pages;
6465 int i;
6466
6467 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6468 if (!pages) {
6469 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6470 return NULL;
6471 }
6472
6473 for (i = 0; i < size; i++) {
6474 pages[i] = alloc_page(gfp_flags);
6475 if (!pages[i]) {
6476 dprintk("%s: failed to allocate page\n", __func__);
6477 nfs4_free_pages(pages, size);
6478 return NULL;
6479 }
6480 }
6481
6482 return pages;
6483}
6484
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006485static void nfs4_layoutget_release(void *calldata)
6486{
6487 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006488 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6489 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006490
6491 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006492 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006493 put_nfs_open_context(lgp->args.ctx);
6494 kfree(calldata);
6495 dprintk("<-- %s\n", __func__);
6496}
6497
6498static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6499 .rpc_call_prepare = nfs4_layoutget_prepare,
6500 .rpc_call_done = nfs4_layoutget_done,
6501 .rpc_release = nfs4_layoutget_release,
6502};
6503
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006504struct pnfs_layout_segment *
6505nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006506{
6507 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006508 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006509 struct rpc_task *task;
6510 struct rpc_message msg = {
6511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6512 .rpc_argp = &lgp->args,
6513 .rpc_resp = &lgp->res,
6514 };
6515 struct rpc_task_setup task_setup_data = {
6516 .rpc_client = server->client,
6517 .rpc_message = &msg,
6518 .callback_ops = &nfs4_layoutget_call_ops,
6519 .callback_data = lgp,
6520 .flags = RPC_TASK_ASYNC,
6521 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006522 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006523 int status = 0;
6524
6525 dprintk("--> %s\n", __func__);
6526
Idan Kedar85541162012-08-02 11:47:10 +03006527 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6528 if (!lgp->args.layout.pages) {
6529 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006530 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006531 }
6532 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6533
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006534 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006535 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006536 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006537 task = rpc_run_task(&task_setup_data);
6538 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006539 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006540 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006541 if (status == 0)
6542 status = task->tk_status;
6543 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006544 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006545 rpc_put_task(task);
6546 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006547 if (status)
6548 return ERR_PTR(status);
6549 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006550}
6551
Benny Halevycbe82602011-05-22 19:52:37 +03006552static void
6553nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6554{
6555 struct nfs4_layoutreturn *lrp = calldata;
6556
6557 dprintk("--> %s\n", __func__);
6558 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006559 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006560 return;
6561 rpc_call_start(task);
6562}
6563
6564static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6565{
6566 struct nfs4_layoutreturn *lrp = calldata;
6567 struct nfs_server *server;
6568
6569 dprintk("--> %s\n", __func__);
6570
6571 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6572 return;
6573
6574 server = NFS_SERVER(lrp->args.inode);
6575 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006576 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006577 return;
6578 }
Benny Halevycbe82602011-05-22 19:52:37 +03006579 dprintk("<-- %s\n", __func__);
6580}
6581
6582static void nfs4_layoutreturn_release(void *calldata)
6583{
6584 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006585 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006586
6587 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006588 spin_lock(&lo->plh_inode->i_lock);
6589 if (lrp->res.lrs_present)
6590 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6591 lo->plh_block_lgets--;
6592 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006593 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006594 kfree(calldata);
6595 dprintk("<-- %s\n", __func__);
6596}
6597
6598static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6599 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6600 .rpc_call_done = nfs4_layoutreturn_done,
6601 .rpc_release = nfs4_layoutreturn_release,
6602};
6603
6604int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6605{
6606 struct rpc_task *task;
6607 struct rpc_message msg = {
6608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6609 .rpc_argp = &lrp->args,
6610 .rpc_resp = &lrp->res,
6611 };
6612 struct rpc_task_setup task_setup_data = {
6613 .rpc_client = lrp->clp->cl_rpcclient,
6614 .rpc_message = &msg,
6615 .callback_ops = &nfs4_layoutreturn_call_ops,
6616 .callback_data = lrp,
6617 };
6618 int status;
6619
6620 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006621 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006622 task = rpc_run_task(&task_setup_data);
6623 if (IS_ERR(task))
6624 return PTR_ERR(task);
6625 status = task->tk_status;
6626 dprintk("<-- %s status=%d\n", __func__, status);
6627 rpc_put_task(task);
6628 return status;
6629}
6630
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006631/*
6632 * Retrieve the list of Data Server devices from the MDS.
6633 */
6634static int _nfs4_getdevicelist(struct nfs_server *server,
6635 const struct nfs_fh *fh,
6636 struct pnfs_devicelist *devlist)
6637{
6638 struct nfs4_getdevicelist_args args = {
6639 .fh = fh,
6640 .layoutclass = server->pnfs_curr_ld->id,
6641 };
6642 struct nfs4_getdevicelist_res res = {
6643 .devlist = devlist,
6644 };
6645 struct rpc_message msg = {
6646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6647 .rpc_argp = &args,
6648 .rpc_resp = &res,
6649 };
6650 int status;
6651
6652 dprintk("--> %s\n", __func__);
6653 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6654 &res.seq_res, 0);
6655 dprintk("<-- %s status=%d\n", __func__, status);
6656 return status;
6657}
6658
6659int nfs4_proc_getdevicelist(struct nfs_server *server,
6660 const struct nfs_fh *fh,
6661 struct pnfs_devicelist *devlist)
6662{
6663 struct nfs4_exception exception = { };
6664 int err;
6665
6666 do {
6667 err = nfs4_handle_exception(server,
6668 _nfs4_getdevicelist(server, fh, devlist),
6669 &exception);
6670 } while (exception.retry);
6671
6672 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6673 err, devlist->num_devs);
6674
6675 return err;
6676}
6677EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6678
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006679static int
6680_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6681{
6682 struct nfs4_getdeviceinfo_args args = {
6683 .pdev = pdev,
6684 };
6685 struct nfs4_getdeviceinfo_res res = {
6686 .pdev = pdev,
6687 };
6688 struct rpc_message msg = {
6689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6690 .rpc_argp = &args,
6691 .rpc_resp = &res,
6692 };
6693 int status;
6694
6695 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006696 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006697 dprintk("<-- %s status=%d\n", __func__, status);
6698
6699 return status;
6700}
6701
6702int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6703{
6704 struct nfs4_exception exception = { };
6705 int err;
6706
6707 do {
6708 err = nfs4_handle_exception(server,
6709 _nfs4_proc_getdeviceinfo(server, pdev),
6710 &exception);
6711 } while (exception.retry);
6712 return err;
6713}
6714EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6715
Andy Adamson863a3c62011-03-23 13:27:54 +00006716static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6717{
6718 struct nfs4_layoutcommit_data *data = calldata;
6719 struct nfs_server *server = NFS_SERVER(data->args.inode);
6720
6721 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006722 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006723 return;
6724 rpc_call_start(task);
6725}
6726
6727static void
6728nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6729{
6730 struct nfs4_layoutcommit_data *data = calldata;
6731 struct nfs_server *server = NFS_SERVER(data->args.inode);
6732
6733 if (!nfs4_sequence_done(task, &data->res.seq_res))
6734 return;
6735
6736 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006737 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6738 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6739 case -NFS4ERR_BADLAYOUT: /* no layout */
6740 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006741 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006742 break;
6743 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006744 nfs_post_op_update_inode_force_wcc(data->args.inode,
6745 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006746 break;
6747 default:
6748 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6749 rpc_restart_call_prepare(task);
6750 return;
6751 }
6752 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006753}
6754
6755static void nfs4_layoutcommit_release(void *calldata)
6756{
6757 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006758 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006759 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006760
Andy Adamsondb29c082011-07-30 20:52:38 -04006761 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006762 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006763 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6764 list_del_init(&lseg->pls_lc_list);
6765 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6766 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006767 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006768 }
Peng Tao92407e72011-10-23 20:21:17 -07006769
6770 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6771 smp_mb__after_clear_bit();
6772 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6773
Andy Adamson863a3c62011-03-23 13:27:54 +00006774 put_rpccred(data->cred);
6775 kfree(data);
6776}
6777
6778static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6779 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6780 .rpc_call_done = nfs4_layoutcommit_done,
6781 .rpc_release = nfs4_layoutcommit_release,
6782};
6783
6784int
Andy Adamsonef311532011-03-12 02:58:10 -05006785nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006786{
6787 struct rpc_message msg = {
6788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6789 .rpc_argp = &data->args,
6790 .rpc_resp = &data->res,
6791 .rpc_cred = data->cred,
6792 };
6793 struct rpc_task_setup task_setup_data = {
6794 .task = &data->task,
6795 .rpc_client = NFS_CLIENT(data->args.inode),
6796 .rpc_message = &msg,
6797 .callback_ops = &nfs4_layoutcommit_ops,
6798 .callback_data = data,
6799 .flags = RPC_TASK_ASYNC,
6800 };
6801 struct rpc_task *task;
6802 int status = 0;
6803
6804 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6805 "lbw: %llu inode %lu\n",
6806 data->task.tk_pid, sync,
6807 data->args.lastbytewritten,
6808 data->args.inode->i_ino);
6809
Trond Myklebust9d12b212012-01-17 22:04:25 -05006810 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006811 task = rpc_run_task(&task_setup_data);
6812 if (IS_ERR(task))
6813 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006814 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006815 goto out;
6816 status = nfs4_wait_for_completion_rpc_task(task);
6817 if (status != 0)
6818 goto out;
6819 status = task->tk_status;
6820out:
6821 dprintk("%s: status %d\n", __func__, status);
6822 rpc_put_task(task);
6823 return status;
6824}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006825
6826static int
6827_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6828 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6829{
6830 struct nfs41_secinfo_no_name_args args = {
6831 .style = SECINFO_STYLE_CURRENT_FH,
6832 };
6833 struct nfs4_secinfo_res res = {
6834 .flavors = flavors,
6835 };
6836 struct rpc_message msg = {
6837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6838 .rpc_argp = &args,
6839 .rpc_resp = &res,
6840 };
6841 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6842}
6843
6844static int
6845nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6846 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6847{
6848 struct nfs4_exception exception = { };
6849 int err;
6850 do {
6851 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6852 switch (err) {
6853 case 0:
6854 case -NFS4ERR_WRONGSEC:
6855 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006856 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006857 default:
6858 err = nfs4_handle_exception(server, err, &exception);
6859 }
6860 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006861out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006862 return err;
6863}
6864
6865static int
6866nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6867 struct nfs_fsinfo *info)
6868{
6869 int err;
6870 struct page *page;
6871 rpc_authflavor_t flavor;
6872 struct nfs4_secinfo_flavors *flavors;
6873
6874 page = alloc_page(GFP_KERNEL);
6875 if (!page) {
6876 err = -ENOMEM;
6877 goto out;
6878 }
6879
6880 flavors = page_address(page);
6881 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6882
6883 /*
6884 * Fall back on "guess and check" method if
6885 * the server doesn't support SECINFO_NO_NAME
6886 */
6887 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6888 err = nfs4_find_root_sec(server, fhandle, info);
6889 goto out_freepage;
6890 }
6891 if (err)
6892 goto out_freepage;
6893
6894 flavor = nfs_find_best_sec(flavors);
6895 if (err == 0)
6896 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6897
6898out_freepage:
6899 put_page(page);
6900 if (err == -EACCES)
6901 return -EPERM;
6902out:
6903 return err;
6904}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006905
6906static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006907{
6908 int status;
6909 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006910 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006911 };
6912 struct nfs41_test_stateid_res res;
6913 struct rpc_message msg = {
6914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6915 .rpc_argp = &args,
6916 .rpc_resp = &res,
6917 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006918
Chuck Lever38527b12012-07-11 16:30:23 -04006919 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006920 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006921 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006922 if (status != NFS_OK) {
6923 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006924 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006925 }
6926 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006927 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006928}
6929
Chuck Lever38527b12012-07-11 16:30:23 -04006930/**
6931 * nfs41_test_stateid - perform a TEST_STATEID operation
6932 *
6933 * @server: server / transport on which to perform the operation
6934 * @stateid: state ID to test
6935 *
6936 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6937 * Otherwise a negative NFS4ERR value is returned if the operation
6938 * failed or the state ID is not currently valid.
6939 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006940static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006941{
6942 struct nfs4_exception exception = { };
6943 int err;
6944 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006945 err = _nfs41_test_stateid(server, stateid);
6946 if (err != -NFS4ERR_DELAY)
6947 break;
6948 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006949 } while (exception.retry);
6950 return err;
6951}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006952
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006953static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006954{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006955 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006956 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006957 };
6958 struct nfs41_free_stateid_res res;
6959 struct rpc_message msg = {
6960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6961 .rpc_argp = &args,
6962 .rpc_resp = &res,
6963 };
Chuck Lever38527b12012-07-11 16:30:23 -04006964 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006965
Chuck Lever38527b12012-07-11 16:30:23 -04006966 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006967 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006968 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006969 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006970 dprintk("NFS reply free_stateid: %d\n", status);
6971 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006972}
6973
Chuck Lever38527b12012-07-11 16:30:23 -04006974/**
6975 * nfs41_free_stateid - perform a FREE_STATEID operation
6976 *
6977 * @server: server / transport on which to perform the operation
6978 * @stateid: state ID to release
6979 *
6980 * Returns NFS_OK if the server freed "stateid". Otherwise a
6981 * negative NFS4ERR value is returned.
6982 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006983static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006984{
6985 struct nfs4_exception exception = { };
6986 int err;
6987 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006988 err = _nfs4_free_stateid(server, stateid);
6989 if (err != -NFS4ERR_DELAY)
6990 break;
6991 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006992 } while (exception.retry);
6993 return err;
6994}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006995
6996static bool nfs41_match_stateid(const nfs4_stateid *s1,
6997 const nfs4_stateid *s2)
6998{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006999 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007000 return false;
7001
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007002 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007003 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007004 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007005 return true;
7006
7007 return false;
7008}
7009
Andy Adamson557134a2009-04-01 09:21:53 -04007010#endif /* CONFIG_NFS_V4_1 */
7011
Trond Myklebust36281ca2012-03-04 18:13:56 -05007012static bool nfs4_match_stateid(const nfs4_stateid *s1,
7013 const nfs4_stateid *s2)
7014{
Trond Myklebustf597c532012-03-04 18:13:56 -05007015 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007016}
7017
7018
Trond Myklebust17280172012-03-11 13:11:00 -04007019static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007020 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007021 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022 .recover_open = nfs4_open_reclaim,
7023 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007024 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007025 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007026 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027};
7028
Andy Adamson591d71c2009-04-01 09:22:47 -04007029#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007030static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007031 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7032 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7033 .recover_open = nfs4_open_reclaim,
7034 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007035 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007036 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007037 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007038 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007039};
7040#endif /* CONFIG_NFS_V4_1 */
7041
Trond Myklebust17280172012-03-11 13:11:00 -04007042static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007043 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007044 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 .recover_open = nfs4_open_expired,
7046 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007047 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007048 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049};
7050
Andy Adamson591d71c2009-04-01 09:22:47 -04007051#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007052static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007053 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7054 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007055 .recover_open = nfs41_open_expired,
7056 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007057 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007058 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007059};
7060#endif /* CONFIG_NFS_V4_1 */
7061
Trond Myklebust17280172012-03-11 13:11:00 -04007062static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007063 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007064 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007065 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007066};
7067
7068#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007069static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007070 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007071 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007072 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007073};
7074#endif
7075
Trond Myklebust97dc1352010-06-16 09:52:26 -04007076static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7077 .minor_version = 0,
7078 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007079 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007080 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007081 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7082 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7083 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007084};
7085
7086#if defined(CONFIG_NFS_V4_1)
7087static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7088 .minor_version = 1,
7089 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007090 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007091 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007092 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7093 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7094 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007095};
7096#endif
7097
Trond Myklebust97dc1352010-06-16 09:52:26 -04007098const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7099 [0] = &nfs_v4_0_minor_ops,
7100#if defined(CONFIG_NFS_V4_1)
7101 [1] = &nfs_v4_1_minor_ops,
7102#endif
7103};
7104
Bryan Schumaker73a79702012-07-16 16:39:12 -04007105const struct inode_operations nfs4_dir_inode_operations = {
7106 .create = nfs_create,
7107 .lookup = nfs_lookup,
7108 .atomic_open = nfs_atomic_open,
7109 .link = nfs_link,
7110 .unlink = nfs_unlink,
7111 .symlink = nfs_symlink,
7112 .mkdir = nfs_mkdir,
7113 .rmdir = nfs_rmdir,
7114 .mknod = nfs_mknod,
7115 .rename = nfs_rename,
7116 .permission = nfs_permission,
7117 .getattr = nfs_getattr,
7118 .setattr = nfs_setattr,
7119 .getxattr = generic_getxattr,
7120 .setxattr = generic_setxattr,
7121 .listxattr = generic_listxattr,
7122 .removexattr = generic_removexattr,
7123};
7124
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007125static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007126 .permission = nfs_permission,
7127 .getattr = nfs_getattr,
7128 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007129 .getxattr = generic_getxattr,
7130 .setxattr = generic_setxattr,
7131 .listxattr = generic_listxattr,
7132 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007133};
7134
David Howells509de812006-08-22 20:06:11 -04007135const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 .version = 4, /* protocol version */
7137 .dentry_ops = &nfs4_dentry_operations,
7138 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007139 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007140 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007142 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007143 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 .getattr = nfs4_proc_getattr,
7145 .setattr = nfs4_proc_setattr,
7146 .lookup = nfs4_proc_lookup,
7147 .access = nfs4_proc_access,
7148 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149 .create = nfs4_proc_create,
7150 .remove = nfs4_proc_remove,
7151 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007152 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 .unlink_done = nfs4_proc_unlink_done,
7154 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007155 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007156 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007157 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 .link = nfs4_proc_link,
7159 .symlink = nfs4_proc_symlink,
7160 .mkdir = nfs4_proc_mkdir,
7161 .rmdir = nfs4_proc_remove,
7162 .readdir = nfs4_proc_readdir,
7163 .mknod = nfs4_proc_mknod,
7164 .statfs = nfs4_proc_statfs,
7165 .fsinfo = nfs4_proc_fsinfo,
7166 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007167 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 .decode_dirent = nfs4_decode_dirent,
7169 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007170 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007171 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007172 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007174 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007175 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007176 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007178 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007179 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007181 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007182 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007183 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007184 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007185 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007186 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007187 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007188 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007189 .create_server = nfs4_create_server,
7190 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191};
7192
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007193static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7194 .prefix = XATTR_NAME_NFSV4_ACL,
7195 .list = nfs4_xattr_list_nfs4_acl,
7196 .get = nfs4_xattr_get_nfs4_acl,
7197 .set = nfs4_xattr_set_nfs4_acl,
7198};
7199
7200const struct xattr_handler *nfs4_xattr_handlers[] = {
7201 &nfs4_xattr_nfs4_acl_handler,
7202 NULL
7203};
7204
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205/*
7206 * Local variables:
7207 * c-basic-offset: 8
7208 * End:
7209 */