blob: 0789ef18a94df34c6d363dca34df53abbd83f554 [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/*
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500559 * nfs4_alloc_slot - efficiently look for a free slot
Benny Halevy510b8172009-04-01 09:22:14 -0400560 *
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500561 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
Benny Halevy510b8172009-04-01 09:22:14 -0400562 * If found, we mark the slot as used, update the highest_used_slotid,
563 * and respectively set up the sequence operation args.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400564 *
565 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400566 */
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500567static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400568{
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500569 struct nfs4_slot *ret = NULL;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500570 u32 slotid;
Benny Halevy510b8172009-04-01 09:22:14 -0400571
Trond Myklebust45d43c22012-02-06 19:38:51 -0500572 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400573 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
574 tbl->max_slots);
575 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
576 if (slotid >= tbl->max_slots)
577 goto out;
578 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500579 if (slotid > tbl->highest_used_slotid ||
580 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400581 tbl->highest_used_slotid = slotid;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500582 ret = &tbl->slots[slotid];
583 ret->renewal_time = jiffies;
584
Benny Halevy510b8172009-04-01 09:22:14 -0400585out:
586 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500587 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
588 ret ? ret->slot_nr : -1);
589 return ret;
Benny Halevy510b8172009-04-01 09:22:14 -0400590}
591
Trond Myklebust9d12b212012-01-17 22:04:25 -0500592static void nfs41_init_sequence(struct nfs4_sequence_args *args,
593 struct nfs4_sequence_res *res, int cache_reply)
594{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500595 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500596 args->sa_cache_this = 0;
597 if (cache_reply)
598 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500599 res->sr_slot = NULL;
600}
601
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000602int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603 struct nfs4_sequence_args *args,
604 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605 struct rpc_task *task)
606{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400607 struct nfs4_slot *slot;
608 struct nfs4_slot_table *tbl;
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
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500635 slot = nfs4_alloc_slot(tbl);
636 if (slot == NULL) {
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);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500645
646 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400647
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500648 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
649 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400650
Benny Halevydfb4f3092010-09-24 09:17:01 -0400651 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400652 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400653 /*
654 * sr_status is only set in decode_sequence, and so will remain
655 * set to 1 if an rpc level failure occurs.
656 */
657 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 return 0;
659}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000660EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661
Trond Myklebust035168a2010-06-16 09:52:26 -0400662int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663 struct nfs4_sequence_args *args,
664 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400665 struct rpc_task *task)
666{
Trond Myklebust035168a2010-06-16 09:52:26 -0400667 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400668 int ret = 0;
669
Trond Myklebust9d12b212012-01-17 22:04:25 -0500670 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400672
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500673 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400674 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500675 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400676
Trond Myklebust9d12b212012-01-17 22:04:25 -0500677 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678out:
679 dprintk("<-- %s status=%d\n", __func__, ret);
680 return ret;
681}
682
683struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400684 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400685 struct nfs4_sequence_args *seq_args;
686 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687};
688
689static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
690{
691 struct nfs41_call_sync_data *data = calldata;
692
Trond Myklebust035168a2010-06-16 09:52:26 -0400693 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
694
695 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500696 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400697 return;
698 rpc_call_start(task);
699}
700
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800701static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
702{
703 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
704 nfs41_call_sync_prepare(task, calldata);
705}
706
Andy Adamson69ab40c2009-04-01 09:22:19 -0400707static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
708{
709 struct nfs41_call_sync_data *data = calldata;
710
Trond Myklebust14516c32010-07-31 14:29:06 -0400711 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400712}
713
Trond Myklebust17280172012-03-11 13:11:00 -0400714static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400715 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400716 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717};
718
Trond Myklebust17280172012-03-11 13:11:00 -0400719static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800720 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
721 .rpc_call_done = nfs41_call_sync_done,
722};
723
Bryan Schumaker7c513052011-03-24 17:12:24 +0000724static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
725 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400726 struct rpc_message *msg,
727 struct nfs4_sequence_args *args,
728 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800729 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400730{
731 int ret;
732 struct rpc_task *task;
733 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400734 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400735 .seq_args = args,
736 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400737 };
738 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000739 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400740 .rpc_message = msg,
741 .callback_ops = &nfs41_call_sync_ops,
742 .callback_data = &data
743 };
744
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800745 if (privileged)
746 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400747 task = rpc_run_task(&task_setup);
748 if (IS_ERR(task))
749 ret = PTR_ERR(task);
750 else {
751 ret = task->tk_status;
752 rpc_put_task(task);
753 }
754 return ret;
755}
756
Bryan Schumaker7c513052011-03-24 17:12:24 +0000757int _nfs4_call_sync_session(struct rpc_clnt *clnt,
758 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400759 struct rpc_message *msg,
760 struct nfs4_sequence_args *args,
761 struct nfs4_sequence_res *res,
762 int cache_reply)
763{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500764 nfs41_init_sequence(args, res, cache_reply);
765 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400766}
767
Trond Myklebustdf896452010-06-16 09:52:26 -0400768#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500769static inline
770void nfs41_init_sequence(struct nfs4_sequence_args *args,
771 struct nfs4_sequence_res *res, int cache_reply)
772{
773}
774
Trond Myklebust14516c32010-07-31 14:29:06 -0400775static int nfs4_sequence_done(struct rpc_task *task,
776 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400777{
Trond Myklebust14516c32010-07-31 14:29:06 -0400778 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400779}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400780#endif /* CONFIG_NFS_V4_1 */
781
Bryan Schumaker7c513052011-03-24 17:12:24 +0000782int _nfs4_call_sync(struct rpc_clnt *clnt,
783 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400784 struct rpc_message *msg,
785 struct nfs4_sequence_args *args,
786 struct nfs4_sequence_res *res,
787 int cache_reply)
788{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500789 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000790 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400791}
792
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000793static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000794int nfs4_call_sync(struct rpc_clnt *clnt,
795 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000796 struct rpc_message *msg,
797 struct nfs4_sequence_args *args,
798 struct nfs4_sequence_res *res,
799 int cache_reply)
800{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000801 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
802 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000803}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400804
Trond Myklebust38478b22006-05-25 01:40:57 -0400805static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Trond Myklebust38478b22006-05-25 01:40:57 -0400807 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Trond Myklebust38478b22006-05-25 01:40:57 -0400809 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400810 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700811 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400812 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700813 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400814 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400818 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 struct nfs_openargs o_arg;
820 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100821 struct nfs_open_confirmargs c_arg;
822 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500823 struct nfs4_string owner_name;
824 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400827 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400829 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100831 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400832 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100833 int rpc_status;
834 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100835};
836
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400837
838static void nfs4_init_opendata_res(struct nfs4_opendata *p)
839{
840 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400841 p->o_res.seqid = p->o_arg.seqid;
842 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400843 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400844 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400845 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500846 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400847}
848
Al Viro82a2c1b2011-06-22 18:30:55 -0400849static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500850 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400851 const struct iattr *attrs,
852 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853{
Al Viro82a2c1b2011-06-22 18:30:55 -0400854 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 struct inode *dir = parent->d_inode;
856 struct nfs_server *server = NFS_SERVER(dir);
857 struct nfs4_opendata *p;
858
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400859 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860 if (p == NULL)
861 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400862 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100863 if (p->o_arg.seqid == NULL)
864 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400865 nfs_sb_active(dentry->d_sb);
866 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100867 p->dir = parent;
868 p->owner = sp;
869 atomic_inc(&sp->so_count);
870 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871 p->o_arg.open_flags = flags;
872 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700873 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
874 * will return permission denied for all bits until close */
875 if (!(flags & O_EXCL)) {
876 /* ask server to check for all possible rights as results
877 * are cached */
878 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
879 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700880 }
David Howells7539bba2006-08-22 20:06:09 -0400881 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400882 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
883 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400884 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100885 p->o_arg.server = server;
886 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400887 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100888 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500889 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500890 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400891
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100892 p->o_arg.u.attrs = &p->attrs;
893 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500894
895 verf[0] = jiffies;
896 verf[1] = current->pid;
897 memcpy(p->o_arg.u.verifier.data, verf,
898 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100899 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100900 p->c_arg.fh = &p->o_res.fh;
901 p->c_arg.stateid = &p->o_res.stateid;
902 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400903 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400904 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100905 return p;
906err_free:
907 kfree(p);
908err:
909 dput(parent);
910 return NULL;
911}
912
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400913static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100914{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400915 struct nfs4_opendata *p = container_of(kref,
916 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400917 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400918
919 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400920 if (p->state != NULL)
921 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400922 nfs4_put_state_owner(p->owner);
923 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400924 dput(p->dentry);
925 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500926 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400927 kfree(p);
928}
929
930static void nfs4_opendata_put(struct nfs4_opendata *p)
931{
932 if (p != NULL)
933 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100934}
935
Trond Myklebust06f814a2006-01-03 09:55:07 +0100936static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
937{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100938 int ret;
939
Trond Myklebust06f814a2006-01-03 09:55:07 +0100940 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100941 return ret;
942}
943
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400945{
946 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947
Trond Myklebust536e43d2012-01-17 22:04:26 -0500948 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949 goto out;
950 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400951 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500952 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
953 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400954 break;
955 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500956 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
957 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400958 break;
959 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500960 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
961 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400962 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400964 return ret;
965}
966
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400968{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500969 if (delegation == NULL)
970 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400972 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500973 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500975 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400976 return 1;
977}
978
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100980{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100982 case FMODE_WRITE:
983 state->n_wronly++;
984 break;
985 case FMODE_READ:
986 state->n_rdonly++;
987 break;
988 case FMODE_READ|FMODE_WRITE:
989 state->n_rdwr++;
990 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100992}
993
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400995{
996 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500997 nfs4_stateid_copy(&state->stateid, stateid);
998 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500999 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001000 case FMODE_READ:
1001 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1002 break;
1003 case FMODE_WRITE:
1004 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1005 break;
1006 case FMODE_READ|FMODE_WRITE:
1007 set_bit(NFS_O_RDWR_STATE, &state->flags);
1008 }
1009}
1010
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001012{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001013 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001015 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001016}
1017
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001018static 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 -07001019{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001020 /*
1021 * Protect the call to nfs4_state_set_mode_locked and
1022 * serialise the stateid update
1023 */
1024 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001025 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001026 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001027 set_bit(NFS_DELEGATED_STATE, &state->flags);
1028 }
1029 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001031 write_sequnlock(&state->seqlock);
1032 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001034 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static 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 -05001038{
1039 struct nfs_inode *nfsi = NFS_I(state->inode);
1040 struct nfs_delegation *deleg_cur;
1041 int ret = 0;
1042
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001044
1045 rcu_read_lock();
1046 deleg_cur = rcu_dereference(nfsi->delegation);
1047 if (deleg_cur == NULL)
1048 goto no_delegation;
1049
1050 spin_lock(&deleg_cur->lock);
1051 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001053 goto no_delegation_unlock;
1054
1055 if (delegation == NULL)
1056 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001057 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001058 goto no_delegation_unlock;
1059
Trond Myklebustb7391f42008-12-23 15:21:52 -05001060 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001062 ret = 1;
1063no_delegation_unlock:
1064 spin_unlock(&deleg_cur->lock);
1065no_delegation:
1066 rcu_read_unlock();
1067
1068 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001069 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001070 ret = 1;
1071 }
1072
1073 return ret;
1074}
1075
1076
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001077static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001078{
1079 struct nfs_delegation *delegation;
1080
1081 rcu_read_lock();
1082 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001084 rcu_read_unlock();
1085 return;
1086 }
1087 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001088 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001089}
1090
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001092{
1093 struct nfs4_state *state = opendata->state;
1094 struct nfs_inode *nfsi = NFS_I(state->inode);
1095 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001096 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001097 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001098 nfs4_stateid stateid;
1099 int ret = -EAGAIN;
1100
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001104 if (can_open_cached(state, fmode, open_mode)) {
1105 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001106 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001107 goto out_return_state;
1108 }
1109 spin_unlock(&state->owner->so_lock);
1110 }
Trond Myklebust34310432008-12-23 15:21:38 -05001111 rcu_read_lock();
1112 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001113 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001114 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001115 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001116 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001118 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001119 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001120 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001121 if (ret != 0)
1122 goto out;
1123 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001124
1125 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001126 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001127 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001128 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001129out:
1130 return ERR_PTR(ret);
1131out_return_state:
1132 atomic_inc(&state->count);
1133 return state;
1134}
1135
Andy Adamsone23008e2012-10-02 21:07:32 -04001136static void
1137nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1138{
1139 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1140 struct nfs_delegation *delegation;
1141 int delegation_flags = 0;
1142
1143 rcu_read_lock();
1144 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1145 if (delegation)
1146 delegation_flags = delegation->flags;
1147 rcu_read_unlock();
1148 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1149 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1150 "returning a delegation for "
1151 "OPEN(CLAIM_DELEGATE_CUR)\n",
1152 clp->cl_hostname);
1153 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1154 nfs_inode_set_delegation(state->inode,
1155 data->owner->so_cred,
1156 &data->o_res);
1157 else
1158 nfs_inode_reclaim_delegation(state->inode,
1159 data->owner->so_cred,
1160 &data->o_res);
1161}
1162
1163/*
1164 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1165 * and update the nfs4_state.
1166 */
1167static struct nfs4_state *
1168_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1169{
1170 struct inode *inode = data->state->inode;
1171 struct nfs4_state *state = data->state;
1172 int ret;
1173
1174 if (!data->rpc_done) {
1175 ret = data->rpc_status;
1176 goto err;
1177 }
1178
1179 ret = -ESTALE;
1180 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1181 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1182 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1183 goto err;
1184
1185 ret = -ENOMEM;
1186 state = nfs4_get_open_state(inode, data->owner);
1187 if (state == NULL)
1188 goto err;
1189
1190 ret = nfs_refresh_inode(inode, &data->f_attr);
1191 if (ret)
1192 goto err;
1193
1194 if (data->o_res.delegation_type != 0)
1195 nfs4_opendata_check_deleg(data, state);
1196 update_open_stateid(state, &data->o_res.stateid, NULL,
1197 data->o_arg.fmode);
1198
1199 return state;
1200err:
1201 return ERR_PTR(ret);
1202
1203}
1204
1205static struct nfs4_state *
1206_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001207{
1208 struct inode *inode;
1209 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001210 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001211
Trond Myklebustaac00a82007-07-05 19:02:21 -04001212 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001213 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001214 goto out;
1215 }
1216
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001217 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001218 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001219 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001220 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001221 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001222 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001223 goto err;
1224 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001225 state = nfs4_get_open_state(inode, data->owner);
1226 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001227 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001228 if (data->o_res.delegation_type != 0)
1229 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001230 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001231 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001232 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001233out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001234 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001235err_put_inode:
1236 iput(inode);
1237err:
1238 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001239}
1240
Andy Adamsone23008e2012-10-02 21:07:32 -04001241static struct nfs4_state *
1242nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1243{
1244 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1245 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1246 return _nfs4_opendata_to_nfs4_state(data);
1247}
1248
Trond Myklebust864472e2006-01-03 09:55:15 +01001249static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1250{
1251 struct nfs_inode *nfsi = NFS_I(state->inode);
1252 struct nfs_open_context *ctx;
1253
1254 spin_lock(&state->inode->i_lock);
1255 list_for_each_entry(ctx, &nfsi->open_files, list) {
1256 if (ctx->state != state)
1257 continue;
1258 get_nfs_open_context(ctx);
1259 spin_unlock(&state->inode->i_lock);
1260 return ctx;
1261 }
1262 spin_unlock(&state->inode->i_lock);
1263 return ERR_PTR(-ENOENT);
1264}
1265
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001266static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1267{
1268 struct nfs4_opendata *opendata;
1269
Al Viro3d4ff432011-06-22 18:40:12 -04001270 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001271 if (opendata == NULL)
1272 return ERR_PTR(-ENOMEM);
1273 opendata->state = state;
1274 atomic_inc(&state->count);
1275 return opendata;
1276}
1277
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001278static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001279{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001280 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001281 int ret;
1282
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001283 opendata->o_arg.open_flags = 0;
1284 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001285 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1286 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1287 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001288 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 if (ret != 0)
1290 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001291 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001292 if (IS_ERR(newstate))
1293 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001294 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001295 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 return 0;
1297}
1298
1299static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1300{
Trond Myklebust864472e2006-01-03 09:55:15 +01001301 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001302 int ret;
1303
1304 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001305 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001306 smp_rmb();
1307 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001308 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001309 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001310 if (ret != 0)
1311 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001312 if (newstate != state)
1313 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001314 }
1315 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001316 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001317 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 if (ret != 0)
1319 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001320 if (newstate != state)
1321 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001322 }
1323 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001324 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001325 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001326 if (ret != 0)
1327 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001328 if (newstate != state)
1329 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001330 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001331 /*
1332 * We may have performed cached opens for all three recoveries.
1333 * Check if we need to update the current stateid.
1334 */
1335 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001336 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001337 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001338 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001339 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001340 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001341 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001342 return 0;
1343}
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345/*
1346 * OPEN_RECLAIM:
1347 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001349static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001351 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001352 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int status;
1355
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001356 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1357 if (IS_ERR(opendata))
1358 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001359 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1360 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001361 rcu_read_lock();
1362 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001363 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001364 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001365 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001366 opendata->o_arg.u.delegation_type = delegation_type;
1367 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001368 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return status;
1370}
1371
Trond Myklebust539cd032007-06-05 11:46:42 -04001372static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 struct nfs_server *server = NFS_SERVER(state->inode);
1375 struct nfs4_exception exception = { };
1376 int err;
1377 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001378 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001379 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001380 break;
1381 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 } while (exception.retry);
1383 return err;
1384}
1385
Trond Myklebust864472e2006-01-03 09:55:15 +01001386static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1387{
1388 struct nfs_open_context *ctx;
1389 int ret;
1390
1391 ctx = nfs4_state_find_open_context(state);
1392 if (IS_ERR(ctx))
1393 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001394 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001395 put_nfs_open_context(ctx);
1396 return ret;
1397}
1398
Trond Myklebust13437e12007-07-06 15:10:43 -04001399static 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 -07001400{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001401 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001402 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001404 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1405 if (IS_ERR(opendata))
1406 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001407 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001408 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001409 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001410 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001411 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Trond Myklebust13437e12007-07-06 15:10:43 -04001414int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001417 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int err;
1419 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001420 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 switch (err) {
1422 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001423 case -ENOENT:
1424 case -ESTALE:
1425 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001426 case -NFS4ERR_BADSESSION:
1427 case -NFS4ERR_BADSLOT:
1428 case -NFS4ERR_BAD_HIGH_SLOT:
1429 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1430 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001431 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001432 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 case -NFS4ERR_STALE_CLIENTID:
1434 case -NFS4ERR_STALE_STATEID:
1435 case -NFS4ERR_EXPIRED:
1436 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001437 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001438 goto out;
1439 case -ERESTARTSYS:
1440 /*
1441 * The show must go on: exit, but mark the
1442 * stateid as needing recovery.
1443 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001444 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001445 case -NFS4ERR_ADMIN_REVOKED:
1446 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001447 nfs_inode_find_state_and_recover(state->inode,
1448 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001449 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001450 case -EKEYEXPIRED:
1451 /*
1452 * User RPCSEC_GSS context has expired.
1453 * We cannot recover this stateid now, so
1454 * skip it and allow recovery thread to
1455 * proceed.
1456 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001457 case -ENOMEM:
1458 err = 0;
1459 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461 err = nfs4_handle_exception(server, err, &exception);
1462 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001463out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return err;
1465}
1466
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001467static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1468{
1469 struct nfs4_opendata *data = calldata;
1470
1471 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001472 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001473 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001474 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001475 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001476 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001477 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001478}
1479
1480static void nfs4_open_confirm_release(void *calldata)
1481{
1482 struct nfs4_opendata *data = calldata;
1483 struct nfs4_state *state = NULL;
1484
1485 /* If this request hasn't been cancelled, do nothing */
1486 if (data->cancelled == 0)
1487 goto out_free;
1488 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001489 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001490 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001491 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001492 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001493 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001494out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001495 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001496}
1497
1498static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001499 .rpc_call_done = nfs4_open_confirm_done,
1500 .rpc_release = nfs4_open_confirm_release,
1501};
1502
1503/*
1504 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1505 */
1506static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1507{
1508 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1509 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001510 struct rpc_message msg = {
1511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1512 .rpc_argp = &data->c_arg,
1513 .rpc_resp = &data->c_res,
1514 .rpc_cred = data->owner->so_cred,
1515 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001516 struct rpc_task_setup task_setup_data = {
1517 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001518 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001519 .callback_ops = &nfs4_open_confirm_ops,
1520 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001521 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001522 .flags = RPC_TASK_ASYNC,
1523 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 int status;
1525
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001526 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001527 data->rpc_done = 0;
1528 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001529 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001530 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001531 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001532 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001533 status = nfs4_wait_for_completion_rpc_task(task);
1534 if (status != 0) {
1535 data->cancelled = 1;
1536 smp_wmb();
1537 } else
1538 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001539 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return status;
1541}
1542
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545 struct nfs4_opendata *data = calldata;
1546 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001547
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1549 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001550 /*
1551 * Check if we still need to send an OPEN call, or if we can use
1552 * a delegation instead.
1553 */
1554 if (data->state != NULL) {
1555 struct nfs_delegation *delegation;
1556
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001557 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001558 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001559 rcu_read_lock();
1560 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001561 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1562 can_open_delegated(delegation, data->o_arg.fmode))
1563 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001564 rcu_read_unlock();
1565 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001566 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001567 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001568 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001569 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001570 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001571 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1572 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001573 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001574 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001575 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001576 &data->o_res.seq_res,
1577 task) != 0)
1578 nfs_release_seqid(data->o_arg.seqid);
1579 else
1580 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001581 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001582unlock_no_action:
1583 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001584out_no_action:
1585 task->tk_action = NULL;
1586
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001587}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001589static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1590{
1591 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1592 nfs4_open_prepare(task, calldata);
1593}
1594
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001595static void nfs4_open_done(struct rpc_task *task, void *calldata)
1596{
1597 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001599 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001600
Trond Myklebust14516c32010-07-31 14:29:06 -04001601 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1602 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001603
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001604 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001605 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1606 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001607 case S_IFREG:
1608 break;
1609 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001611 break;
1612 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001613 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001614 break;
1615 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001616 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001617 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001618 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001619 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001620 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1621 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001622 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001623 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001624}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001625
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001626static void nfs4_open_release(void *calldata)
1627{
1628 struct nfs4_opendata *data = calldata;
1629 struct nfs4_state *state = NULL;
1630
1631 /* If this request hasn't been cancelled, do nothing */
1632 if (data->cancelled == 0)
1633 goto out_free;
1634 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001635 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001636 goto out_free;
1637 /* In case we need an open_confirm, no cleanup! */
1638 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1639 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001640 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001641 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001642 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001643out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001644 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001645}
1646
1647static const struct rpc_call_ops nfs4_open_ops = {
1648 .rpc_call_prepare = nfs4_open_prepare,
1649 .rpc_call_done = nfs4_open_done,
1650 .rpc_release = nfs4_open_release,
1651};
1652
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001653static const struct rpc_call_ops nfs4_recover_open_ops = {
1654 .rpc_call_prepare = nfs4_recover_open_prepare,
1655 .rpc_call_done = nfs4_open_done,
1656 .rpc_release = nfs4_open_release,
1657};
1658
1659static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001660{
1661 struct inode *dir = data->dir->d_inode;
1662 struct nfs_server *server = NFS_SERVER(dir);
1663 struct nfs_openargs *o_arg = &data->o_arg;
1664 struct nfs_openres *o_res = &data->o_res;
1665 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001666 struct rpc_message msg = {
1667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1668 .rpc_argp = o_arg,
1669 .rpc_resp = o_res,
1670 .rpc_cred = data->owner->so_cred,
1671 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001672 struct rpc_task_setup task_setup_data = {
1673 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001674 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001675 .callback_ops = &nfs4_open_ops,
1676 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001677 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001678 .flags = RPC_TASK_ASYNC,
1679 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680 int status;
1681
Trond Myklebust9d12b212012-01-17 22:04:25 -05001682 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001683 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001684 data->rpc_done = 0;
1685 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001686 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001687 if (isrecover)
1688 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001689 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001690 if (IS_ERR(task))
1691 return PTR_ERR(task);
1692 status = nfs4_wait_for_completion_rpc_task(task);
1693 if (status != 0) {
1694 data->cancelled = 1;
1695 smp_wmb();
1696 } else
1697 status = data->rpc_status;
1698 rpc_put_task(task);
1699
1700 return status;
1701}
1702
1703static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1704{
1705 struct inode *dir = data->dir->d_inode;
1706 struct nfs_openres *o_res = &data->o_res;
1707 int status;
1708
1709 status = nfs4_run_open_task(data, 1);
1710 if (status != 0 || !data->rpc_done)
1711 return status;
1712
Trond Myklebust6926afd2012-01-07 13:22:46 -05001713 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1714
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001715 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1716 status = _nfs4_proc_open_confirm(data);
1717 if (status != 0)
1718 return status;
1719 }
1720
1721 return status;
1722}
1723
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001724static int nfs4_opendata_access(struct rpc_cred *cred,
1725 struct nfs4_opendata *opendata,
1726 struct nfs4_state *state, fmode_t fmode)
1727{
1728 struct nfs_access_entry cache;
1729 u32 mask;
1730
1731 /* access call failed or for some reason the server doesn't
1732 * support any access modes -- defer access call until later */
1733 if (opendata->o_res.access_supported == 0)
1734 return 0;
1735
1736 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001737 /* don't check MAY_WRITE - a newly created file may not have
1738 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001739 if (fmode & FMODE_READ)
1740 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001741 if (fmode & FMODE_EXEC)
1742 mask |= MAY_EXEC;
1743
1744 cache.cred = cred;
1745 cache.jiffies = jiffies;
1746 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1747 nfs_access_add_cache(state->inode, &cache);
1748
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001749 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001750 return 0;
1751
1752 /* even though OPEN succeeded, access is denied. Close the file */
1753 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001754 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001755}
1756
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001757/*
1758 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1759 */
1760static int _nfs4_proc_open(struct nfs4_opendata *data)
1761{
1762 struct inode *dir = data->dir->d_inode;
1763 struct nfs_server *server = NFS_SERVER(dir);
1764 struct nfs_openargs *o_arg = &data->o_arg;
1765 struct nfs_openres *o_res = &data->o_res;
1766 int status;
1767
1768 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001769 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001770 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001771 if (status != 0) {
1772 if (status == -NFS4ERR_BADNAME &&
1773 !(o_arg->open_flags & O_CREAT))
1774 return -ENOENT;
1775 return status;
1776 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001777
Trond Myklebust6926afd2012-01-07 13:22:46 -05001778 nfs_fattr_map_and_free_names(server, &data->f_attr);
1779
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001780 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001781 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001782 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1783 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001785 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001787 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001790 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
Andy Adamsond83217c2011-03-01 01:34:17 +00001794static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001795{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001796 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001797 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001798
Trond Myklebusta78cb572009-08-09 15:06:19 -04001799 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001800 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001801 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001802 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001803 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1804 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001805 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001806 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001807 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001808 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001809 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001810}
1811
Andy Adamsond83217c2011-03-01 01:34:17 +00001812static int nfs4_recover_expired_lease(struct nfs_server *server)
1813{
1814 return nfs4_client_recover_expired_lease(server->nfs_client);
1815}
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/*
1818 * OPEN_EXPIRED:
1819 * reclaim state on the server after a network partition.
1820 * Assumes caller holds the appropriate lock
1821 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001822static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001824 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001825 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001827 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1828 if (IS_ERR(opendata))
1829 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001830 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001831 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001832 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001833 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001834 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001837static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001838{
Trond Myklebust539cd032007-06-05 11:46:42 -04001839 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001840 struct nfs4_exception exception = { };
1841 int err;
1842
1843 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001844 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001845 switch (err) {
1846 default:
1847 goto out;
1848 case -NFS4ERR_GRACE:
1849 case -NFS4ERR_DELAY:
1850 nfs4_handle_exception(server, err, &exception);
1851 err = 0;
1852 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001853 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001854out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001855 return err;
1856}
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1859{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001861 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Trond Myklebust864472e2006-01-03 09:55:15 +01001863 ctx = nfs4_state_find_open_context(state);
1864 if (IS_ERR(ctx))
1865 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001866 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001867 put_nfs_open_context(ctx);
1868 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001871#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001872static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001873{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001874 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001875 nfs4_stateid *stateid = &state->stateid;
1876 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001877
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001878 /* If a state reset has been done, test_stateid is unneeded */
1879 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1880 return;
1881
1882 status = nfs41_test_stateid(server, stateid);
1883 if (status != NFS_OK) {
1884 /* Free the stateid unless the server explicitly
1885 * informs us the stateid is unrecognized. */
1886 if (status != -NFS4ERR_BAD_STATEID)
1887 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001888 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001889
Bryan Schumaker69388672012-09-26 15:25:52 -04001890 write_seqlock(&state->seqlock);
1891 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1892 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001893 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1894 }
1895}
1896
1897/**
1898 * nfs41_check_open_stateid - possibly free an open stateid
1899 *
1900 * @state: NFSv4 state for an inode
1901 *
1902 * Returns NFS_OK if recovery for this stateid is now finished.
1903 * Otherwise a negative NFS4ERR value is returned.
1904 */
1905static int nfs41_check_open_stateid(struct nfs4_state *state)
1906{
1907 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001908 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001909 int status;
1910
1911 /* If a state reset has been done, test_stateid is unneeded */
1912 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1913 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1914 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1915 return -NFS4ERR_BAD_STATEID;
1916
1917 status = nfs41_test_stateid(server, stateid);
1918 if (status != NFS_OK) {
1919 /* Free the stateid unless the server explicitly
1920 * informs us the stateid is unrecognized. */
1921 if (status != -NFS4ERR_BAD_STATEID)
1922 nfs41_free_stateid(server, stateid);
1923
1924 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1925 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1926 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001927 }
1928 return status;
1929}
1930
1931static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1932{
Chuck Levereb64cf92012-07-11 16:30:05 -04001933 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001934
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001935 nfs41_clear_delegation_stateid(state);
1936 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001937 if (status != NFS_OK)
1938 status = nfs4_open_expired(sp, state);
1939 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001940}
1941#endif
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001944 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1945 * fields corresponding to attributes that were used to store the verifier.
1946 * Make sure we clobber those fields in the later setattr call
1947 */
1948static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1949{
1950 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1951 !(sattr->ia_valid & ATTR_ATIME_SET))
1952 sattr->ia_valid |= ATTR_ATIME;
1953
1954 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1955 !(sattr->ia_valid & ATTR_MTIME_SET))
1956 sattr->ia_valid |= ATTR_MTIME;
1957}
1958
1959/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001960 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 */
Andy Adamson82be4172012-05-23 05:02:35 -04001962static int _nfs4_do_open(struct inode *dir,
1963 struct dentry *dentry,
1964 fmode_t fmode,
1965 int flags,
1966 struct iattr *sattr,
1967 struct rpc_cred *cred,
1968 struct nfs4_state **res,
1969 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 struct nfs4_state_owner *sp;
1972 struct nfs4_state *state = NULL;
1973 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001974 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001975 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001979 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1980 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1982 goto out_err;
1983 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001984 status = nfs4_recover_expired_lease(server);
1985 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001986 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001987 if (dentry->d_inode != NULL)
1988 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001989 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001990 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001991 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001992 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Andy Adamson82be4172012-05-23 05:02:35 -04001994 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1995 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1996 if (!opendata->f_attr.mdsthreshold)
1997 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001998 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001999 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002000 if (dentry->d_inode != NULL)
2001 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002002
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002003 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002005 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002007 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002008 status = PTR_ERR(state);
2009 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002010 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002011 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002012 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002013
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002014 status = nfs4_opendata_access(cred, opendata, state, fmode);
2015 if (status != 0)
2016 goto err_opendata_put;
2017
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002018 if (opendata->o_arg.open_flags & O_EXCL) {
2019 nfs4_exclusive_attrset(opendata, sattr);
2020
2021 nfs_fattr_init(opendata->o_res.f_attr);
2022 status = nfs4_do_setattr(state->inode, cred,
2023 opendata->o_res.f_attr, sattr,
2024 state);
2025 if (status == 0)
2026 nfs_setattr_update_inode(state->inode, sattr);
2027 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2028 }
Andy Adamson82be4172012-05-23 05:02:35 -04002029
2030 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2031 *ctx_th = opendata->f_attr.mdsthreshold;
2032 else
2033 kfree(opendata->f_attr.mdsthreshold);
2034 opendata->f_attr.mdsthreshold = NULL;
2035
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002036 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 *res = state;
2039 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002040err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002041 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002042 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002043err_put_state_owner:
2044 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 *res = NULL;
2047 return status;
2048}
2049
2050
Andy Adamson82be4172012-05-23 05:02:35 -04002051static struct nfs4_state *nfs4_do_open(struct inode *dir,
2052 struct dentry *dentry,
2053 fmode_t fmode,
2054 int flags,
2055 struct iattr *sattr,
2056 struct rpc_cred *cred,
2057 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 struct nfs4_exception exception = { };
2060 struct nfs4_state *res;
2061 int status;
2062
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002063 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002065 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2066 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (status == 0)
2068 break;
2069 /* NOTE: BAD_SEQID means the server and client disagree about the
2070 * book-keeping w.r.t. state-changing operations
2071 * (OPEN/CLOSE/LOCK/LOCKU...)
2072 * It is actually a sign of a bug on the client or on the server.
2073 *
2074 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002075 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 * have unhashed the old state_owner for us, and that we can
2077 * therefore safely retry using a new one. We should still warn
2078 * the user though...
2079 */
2080 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002081 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002082 " returned a bad sequence-id error!\n",
2083 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 exception.retry = 1;
2085 continue;
2086 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002087 /*
2088 * BAD_STATEID on OPEN means that the server cancelled our
2089 * state before it received the OPEN_CONFIRM.
2090 * Recover by retrying the request as per the discussion
2091 * on Page 181 of RFC3530.
2092 */
2093 if (status == -NFS4ERR_BAD_STATEID) {
2094 exception.retry = 1;
2095 continue;
2096 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002097 if (status == -EAGAIN) {
2098 /* We must have found a delegation */
2099 exception.retry = 1;
2100 continue;
2101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2103 status, &exception));
2104 } while (exception.retry);
2105 return res;
2106}
2107
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002108static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2109 struct nfs_fattr *fattr, struct iattr *sattr,
2110 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002112 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002114 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 .iap = sattr,
2116 .server = server,
2117 .bitmask = server->attr_bitmask,
2118 };
2119 struct nfs_setattrres res = {
2120 .fattr = fattr,
2121 .server = server,
2122 };
2123 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2125 .rpc_argp = &arg,
2126 .rpc_resp = &res,
2127 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002129 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002130 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Trond Myklebust0e574af2005-10-27 22:12:38 -04002132 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Trond Myklebust4fc87962012-03-08 17:42:01 -05002134 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002135 struct nfs_lockowner lockowner = {
2136 .l_owner = current->files,
2137 .l_pid = current->tgid,
2138 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002139 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002140 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002141 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2142 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002143 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002144 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002145 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Bryan Schumaker7c513052011-03-24 17:12:24 +00002147 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002148 if (status == 0 && state != NULL)
2149 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002150 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002153static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2154 struct nfs_fattr *fattr, struct iattr *sattr,
2155 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002157 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002158 struct nfs4_exception exception = {
2159 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002160 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002161 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 int err;
2163 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002164 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2165 switch (err) {
2166 case -NFS4ERR_OPENMODE:
2167 if (state && !(state->state & FMODE_WRITE)) {
2168 err = -EBADF;
2169 if (sattr->ia_valid & ATTR_OPEN)
2170 err = -EACCES;
2171 goto out;
2172 }
2173 }
2174 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002176out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return err;
2178}
2179
2180struct nfs4_closedata {
2181 struct inode *inode;
2182 struct nfs4_state *state;
2183 struct nfs_closeargs arg;
2184 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002185 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002186 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002187 bool roc;
2188 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189};
2190
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002191static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002192{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002193 struct nfs4_closedata *calldata = data;
2194 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002195 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002196
Fred Isamanf7e89172011-01-06 11:36:32 +00002197 if (calldata->roc)
2198 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002199 nfs4_put_open_state(calldata->state);
2200 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002201 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002202 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002203 kfree(calldata);
2204}
2205
Trond Myklebust88069f72009-12-08 08:33:16 -05002206static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2207 fmode_t fmode)
2208{
2209 spin_lock(&state->owner->so_lock);
2210 if (!(fmode & FMODE_READ))
2211 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2212 if (!(fmode & FMODE_WRITE))
2213 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2214 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2215 spin_unlock(&state->owner->so_lock);
2216}
2217
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002218static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002220 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 struct nfs_server *server = NFS_SERVER(calldata->inode);
2223
Chuck Levera3ca5652012-03-01 17:00:40 -05002224 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002225 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2226 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /* hmm. we are done with the inode, and in the process of freeing
2228 * the state_owner. we keep this around to process errors
2229 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 switch (task->tk_status) {
2231 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002232 if (calldata->roc)
2233 pnfs_roc_set_barrier(state->inode,
2234 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002235 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002236 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002237 nfs4_close_clear_stateid_flags(state,
2238 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 break;
2240 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002241 case -NFS4ERR_OLD_STATEID:
2242 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002244 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002247 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2248 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002250 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002251 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002252 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
2254
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002255static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002257 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002258 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002259 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002260 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002261
Chuck Levera3ca5652012-03-01 17:00:40 -05002262 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002263 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002264 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002265
Trond Myklebust88069f72009-12-08 08:33:16 -05002266 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2267 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002268 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002269 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002270 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002271 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002272 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2273 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2274 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002275 }
2276 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002277 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2278 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2279 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002280 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002281 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002282 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002283
2284 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002285 /* Note: exit _without_ calling nfs4_close_done */
2286 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002287 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002288 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002289
Fred Isamanf7e89172011-01-06 11:36:32 +00002290 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002291 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002292 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002293 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002294 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002295 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002296
Trond Myklebust516a6af2005-10-27 22:12:41 -04002297 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002298 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002299 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002300 &calldata->arg.seq_args,
2301 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002302 task) != 0)
2303 nfs_release_seqid(calldata->arg.seqid);
2304 else
2305 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002306out:
2307 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002310static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002311 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002312 .rpc_call_done = nfs4_close_done,
2313 .rpc_release = nfs4_free_closedata,
2314};
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316/*
2317 * It is possible for data to be read/written from a mem-mapped file
2318 * after the sys_close call (which hits the vfs layer as a flush).
2319 * This means that we can't safely call nfsv4 close on a file until
2320 * the inode is cleared. This in turn means that we are not good
2321 * NFSv4 citizens - we do not indicate to the server to update the file's
2322 * share state even when we are done with one of the three share
2323 * stateid's in the inode.
2324 *
2325 * NOTE: Caller must be holding the sp->so_owner semaphore!
2326 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002327int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002329 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002331 struct nfs4_state_owner *sp = state->owner;
2332 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002333 struct rpc_message msg = {
2334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2335 .rpc_cred = state->owner->so_cred,
2336 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002337 struct rpc_task_setup task_setup_data = {
2338 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002339 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002340 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002341 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002342 .flags = RPC_TASK_ASYNC,
2343 };
Trond Myklebust95121352005-10-18 14:20:12 -07002344 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002346 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002348 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002349 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002350 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002352 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002353 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002355 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002356 if (calldata->arg.seqid == NULL)
2357 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002358 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002359 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002360 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002361 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002362 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002363 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002364 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002365
Trond Myklebust1174dd12010-12-21 10:52:24 -05002366 msg.rpc_argp = &calldata->arg;
2367 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002368 task_setup_data.callback_data = calldata;
2369 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002370 if (IS_ERR(task))
2371 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002372 status = 0;
2373 if (wait)
2374 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002375 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002376 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002377out_free_calldata:
2378 kfree(calldata);
2379out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002380 nfs4_put_open_state(state);
2381 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002382 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Trond Myklebust2b484292010-09-17 10:56:51 -04002385static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002386nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 struct nfs4_state *state;
2389
Trond Myklebust565277f2007-10-15 18:17:53 -04002390 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002391 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2392 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002393 if (IS_ERR(state))
2394 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002395 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002396 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Trond Myklebust1185a552009-12-03 15:54:02 -05002399static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002400{
2401 if (ctx->state == NULL)
2402 return;
2403 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002404 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002405 else
Al Viro643168c2011-06-22 18:20:23 -04002406 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002407}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2410{
Benny Halevy43652ad2009-04-01 09:21:54 -04002411 struct nfs4_server_caps_arg args = {
2412 .fhandle = fhandle,
2413 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 struct nfs4_server_caps_res res = {};
2415 struct rpc_message msg = {
2416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002417 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 .rpc_resp = &res,
2419 };
2420 int status;
2421
Bryan Schumaker7c513052011-03-24 17:12:24 +00002422 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (status == 0) {
2424 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002425 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2426 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2427 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2428 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2429 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2431 server->caps |= NFS_CAP_ACLS;
2432 if (res.has_links != 0)
2433 server->caps |= NFS_CAP_HARDLINKS;
2434 if (res.has_symlinks != 0)
2435 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002436 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2437 server->caps |= NFS_CAP_FILEID;
2438 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2439 server->caps |= NFS_CAP_MODE;
2440 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2441 server->caps |= NFS_CAP_NLINK;
2442 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2443 server->caps |= NFS_CAP_OWNER;
2444 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2445 server->caps |= NFS_CAP_OWNER_GROUP;
2446 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2447 server->caps |= NFS_CAP_ATIME;
2448 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2449 server->caps |= NFS_CAP_CTIME;
2450 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2451 server->caps |= NFS_CAP_MTIME;
2452
Trond Myklebusta65318b2009-03-11 14:10:28 -04002453 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2454 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2455 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002457 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return status;
2461}
2462
Trond Myklebust55a97592006-06-09 09:34:19 -04002463int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 struct nfs4_exception exception = { };
2466 int err;
2467 do {
2468 err = nfs4_handle_exception(server,
2469 _nfs4_server_capabilities(server, fhandle),
2470 &exception);
2471 } while (exception.retry);
2472 return err;
2473}
2474
2475static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2476 struct nfs_fsinfo *info)
2477{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 struct nfs4_lookup_root_arg args = {
2479 .bitmask = nfs4_fattr_bitmap,
2480 };
2481 struct nfs4_lookup_res res = {
2482 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002483 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 .fh = fhandle,
2485 };
2486 struct rpc_message msg = {
2487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2488 .rpc_argp = &args,
2489 .rpc_resp = &res,
2490 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002491
Trond Myklebust0e574af2005-10-27 22:12:38 -04002492 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002493 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
2496static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2497 struct nfs_fsinfo *info)
2498{
2499 struct nfs4_exception exception = { };
2500 int err;
2501 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002502 err = _nfs4_lookup_root(server, fhandle, info);
2503 switch (err) {
2504 case 0:
2505 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002506 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002507 default:
2508 err = nfs4_handle_exception(server, err, &exception);
2509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002511out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 return err;
2513}
2514
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002515static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2516 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2517{
2518 struct rpc_auth *auth;
2519 int ret;
2520
2521 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002522 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002523 ret = -EIO;
2524 goto out;
2525 }
2526 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002527out:
2528 return ret;
2529}
2530
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002531static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002532 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002534 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002535 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002537 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002538 if (len < 0)
2539 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002540
2541 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002542 /* AUTH_UNIX is the default flavor if none was specified,
2543 * thus has already been tried. */
2544 if (flav_array[i] == RPC_AUTH_UNIX)
2545 continue;
2546
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002547 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002548 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002549 continue;
2550 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002551 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002552 /*
2553 * -EACCESS could mean that the user doesn't have correct permissions
2554 * to access the mount. It could also mean that we tried to mount
2555 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2556 * existing mount programs don't handle -EACCES very well so it should
2557 * be mapped to -EPERM instead.
2558 */
2559 if (status == -EACCES)
2560 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002561 return status;
2562}
2563
2564/*
2565 * get the file handle for the "/" directory on the server
2566 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002567int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2568 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002569{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002570 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002571 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002572 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2573 /*
2574 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2575 * by nfs4_map_errors() as this function exits.
2576 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002577 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (status == 0)
2579 status = nfs4_server_capabilities(server, fhandle);
2580 if (status == 0)
2581 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002582 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Bryan Schumakerbae36242012-05-10 15:07:31 -04002585static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2586 struct nfs_fsinfo *info)
2587{
2588 int error;
2589 struct nfs_fattr *fattr = info->fattr;
2590
2591 error = nfs4_server_capabilities(server, mntfh);
2592 if (error < 0) {
2593 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2594 return error;
2595 }
2596
2597 error = nfs4_proc_getattr(server, mntfh, fattr);
2598 if (error < 0) {
2599 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2600 return error;
2601 }
2602
2603 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2604 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2605 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2606
2607 return error;
2608}
2609
Manoj Naik6b97fd32006-06-09 09:34:29 -04002610/*
2611 * Get locations and (maybe) other attributes of a referral.
2612 * Note that we'll actually follow the referral later when
2613 * we detect fsid mismatch in inode revalidation
2614 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002615static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2616 const struct qstr *name, struct nfs_fattr *fattr,
2617 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002618{
2619 int status = -ENOMEM;
2620 struct page *page = NULL;
2621 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002622
2623 page = alloc_page(GFP_KERNEL);
2624 if (page == NULL)
2625 goto out;
2626 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2627 if (locations == NULL)
2628 goto out;
2629
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002630 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002631 if (status != 0)
2632 goto out;
2633 /* Make sure server returned a different fsid for the referral */
2634 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002635 dprintk("%s: server did not return a different fsid for"
2636 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002637 status = -EIO;
2638 goto out;
2639 }
Andy Adamson533eb462011-06-13 18:25:56 -04002640 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2641 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002642
Andy Adamson533eb462011-06-13 18:25:56 -04002643 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002644 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002645 memset(fhandle, 0, sizeof(struct nfs_fh));
2646out:
2647 if (page)
2648 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002649 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002650 return status;
2651}
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2654{
2655 struct nfs4_getattr_arg args = {
2656 .fh = fhandle,
2657 .bitmask = server->attr_bitmask,
2658 };
2659 struct nfs4_getattr_res res = {
2660 .fattr = fattr,
2661 .server = server,
2662 };
2663 struct rpc_message msg = {
2664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2665 .rpc_argp = &args,
2666 .rpc_resp = &res,
2667 };
2668
Trond Myklebust0e574af2005-10-27 22:12:38 -04002669 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002670 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
2673static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2674{
2675 struct nfs4_exception exception = { };
2676 int err;
2677 do {
2678 err = nfs4_handle_exception(server,
2679 _nfs4_proc_getattr(server, fhandle, fattr),
2680 &exception);
2681 } while (exception.retry);
2682 return err;
2683}
2684
2685/*
2686 * The file is not closed if it is opened due to the a request to change
2687 * the size of the file. The open call will not be needed once the
2688 * VFS layer lookup-intents are implemented.
2689 *
2690 * Close is called when the inode is destroyed.
2691 * If we haven't opened the file for O_WRONLY, we
2692 * need to in the size_change case to obtain a stateid.
2693 *
2694 * Got race?
2695 * Because OPEN is always done by name in nfsv4, it is
2696 * possible that we opened a different file by the same
2697 * name. We can recognize this race condition, but we
2698 * can't do anything about it besides returning an error.
2699 *
2700 * This will be fixed with VFS changes (lookup-intent).
2701 */
2702static int
2703nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2704 struct iattr *sattr)
2705{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002706 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002707 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002708 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 int status;
2710
Benny Halevy8a1636c2010-07-14 15:43:57 -04002711 if (pnfs_ld_layoutret_on_setattr(inode))
2712 pnfs_return_layout(inode);
2713
Trond Myklebust0e574af2005-10-27 22:12:38 -04002714 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Andy Adamson26699402012-05-30 16:12:24 -04002716 /* Deal with open(O_TRUNC) */
2717 if (sattr->ia_valid & ATTR_OPEN)
2718 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2719
2720 /* Optimization: if the end result is no change, don't RPC */
2721 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2722 return 0;
2723
Trond Myklebustd5308382005-11-04 15:33:38 -05002724 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002725 if (sattr->ia_valid & ATTR_FILE) {
2726 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002727
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002728 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002729 if (ctx) {
2730 cred = ctx->cred;
2731 state = ctx->state;
2732 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002733 }
2734
2735 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002736 if (status == 0)
2737 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return status;
2739}
2740
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002741static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2742 const struct qstr *name, struct nfs_fh *fhandle,
2743 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002744{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002745 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002746 int status;
2747 struct nfs4_lookup_arg args = {
2748 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002749 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002750 .name = name,
2751 };
2752 struct nfs4_lookup_res res = {
2753 .server = server,
2754 .fattr = fattr,
2755 .fh = fhandle,
2756 };
2757 struct rpc_message msg = {
2758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2759 .rpc_argp = &args,
2760 .rpc_resp = &res,
2761 };
2762
2763 nfs_fattr_init(fattr);
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002766 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 dprintk("NFS reply lookup: %d\n", status);
2768 return status;
2769}
2770
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002771static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002772{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002773 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002774 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002775 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2776 fattr->nlink = 2;
2777}
2778
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002779static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2780 struct qstr *name, struct nfs_fh *fhandle,
2781 struct nfs_fattr *fattr)
2782{
2783 struct nfs4_exception exception = { };
2784 struct rpc_clnt *client = *clnt;
2785 int err;
2786 do {
2787 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2788 switch (err) {
2789 case -NFS4ERR_BADNAME:
2790 err = -ENOENT;
2791 goto out;
2792 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002793 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002794 goto out;
2795 case -NFS4ERR_WRONGSEC:
2796 err = -EPERM;
2797 if (client != *clnt)
2798 goto out;
2799
2800 client = nfs4_create_sec_client(client, dir, name);
2801 if (IS_ERR(client))
2802 return PTR_ERR(client);
2803
2804 exception.retry = 1;
2805 break;
2806 default:
2807 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2808 }
2809 } while (exception.retry);
2810
2811out:
2812 if (err == 0)
2813 *clnt = client;
2814 else if (client != *clnt)
2815 rpc_shutdown_client(client);
2816
2817 return err;
2818}
2819
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002820static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002821 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002823 int status;
2824 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002825
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002826 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2827 if (client != NFS_CLIENT(dir)) {
2828 rpc_shutdown_client(client);
2829 nfs_fixup_secinfo_attributes(fattr);
2830 }
2831 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002834struct rpc_clnt *
2835nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2836 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2837{
2838 int status;
2839 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2840
2841 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2842 if (status < 0) {
2843 rpc_shutdown_client(client);
2844 return ERR_PTR(status);
2845 }
2846 return client;
2847}
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2850{
Trond Myklebust76b32992007-08-10 17:45:11 -04002851 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 struct nfs4_accessargs args = {
2853 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002854 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002856 struct nfs4_accessres res = {
2857 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002858 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct rpc_message msg = {
2860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2861 .rpc_argp = &args,
2862 .rpc_resp = &res,
2863 .rpc_cred = entry->cred,
2864 };
2865 int mode = entry->mask;
2866 int status;
2867
2868 /*
2869 * Determine which access bits we want to ask for...
2870 */
2871 if (mode & MAY_READ)
2872 args.access |= NFS4_ACCESS_READ;
2873 if (S_ISDIR(inode->i_mode)) {
2874 if (mode & MAY_WRITE)
2875 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2876 if (mode & MAY_EXEC)
2877 args.access |= NFS4_ACCESS_LOOKUP;
2878 } else {
2879 if (mode & MAY_WRITE)
2880 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2881 if (mode & MAY_EXEC)
2882 args.access |= NFS4_ACCESS_EXECUTE;
2883 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002884
2885 res.fattr = nfs_alloc_fattr();
2886 if (res.fattr == NULL)
2887 return -ENOMEM;
2888
Bryan Schumaker7c513052011-03-24 17:12:24 +00002889 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002891 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002892 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002894 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return status;
2896}
2897
2898static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2899{
2900 struct nfs4_exception exception = { };
2901 int err;
2902 do {
2903 err = nfs4_handle_exception(NFS_SERVER(inode),
2904 _nfs4_proc_access(inode, entry),
2905 &exception);
2906 } while (exception.retry);
2907 return err;
2908}
2909
2910/*
2911 * TODO: For the time being, we don't try to get any attributes
2912 * along with any of the zero-copy operations READ, READDIR,
2913 * READLINK, WRITE.
2914 *
2915 * In the case of the first three, we want to put the GETATTR
2916 * after the read-type operation -- this is because it is hard
2917 * to predict the length of a GETATTR response in v4, and thus
2918 * align the READ data correctly. This means that the GETATTR
2919 * may end up partially falling into the page cache, and we should
2920 * shift it into the 'tail' of the xdr_buf before processing.
2921 * To do this efficiently, we need to know the total length
2922 * of data received, which doesn't seem to be available outside
2923 * of the RPC layer.
2924 *
2925 * In the case of WRITE, we also want to put the GETATTR after
2926 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002927 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 *
2929 * Both of these changes to the XDR layer would in fact be quite
2930 * minor, but I decided to leave them for a subsequent patch.
2931 */
2932static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2933 unsigned int pgbase, unsigned int pglen)
2934{
2935 struct nfs4_readlink args = {
2936 .fh = NFS_FH(inode),
2937 .pgbase = pgbase,
2938 .pglen = pglen,
2939 .pages = &page,
2940 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002941 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 struct rpc_message msg = {
2943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2944 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002945 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 };
2947
Bryan Schumaker7c513052011-03-24 17:12:24 +00002948 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 -07002949}
2950
2951static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2952 unsigned int pgbase, unsigned int pglen)
2953{
2954 struct nfs4_exception exception = { };
2955 int err;
2956 do {
2957 err = nfs4_handle_exception(NFS_SERVER(inode),
2958 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2959 &exception);
2960 } while (exception.retry);
2961 return err;
2962}
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002965 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967static int
2968nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002969 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002971 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 int status = 0;
2974
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002975 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2976 if (IS_ERR(ctx))
2977 return PTR_ERR(ctx);
2978
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002979 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002980 state = nfs4_do_open(dir, dentry, ctx->mode,
2981 flags, sattr, ctx->cred,
2982 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002983 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (IS_ERR(state)) {
2985 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002986 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002988 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002989 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002990 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002992 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 return status;
2994}
2995
2996static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2997{
Trond Myklebust16e42952005-10-27 22:12:44 -04002998 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002999 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003001 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003003 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003004 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003005 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3008 .rpc_argp = &args,
3009 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003011 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003012
Bryan Schumaker7c513052011-03-24 17:12:24 +00003013 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003014 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003015 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return status;
3017}
3018
3019static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3020{
3021 struct nfs4_exception exception = { };
3022 int err;
3023 do {
3024 err = nfs4_handle_exception(NFS_SERVER(dir),
3025 _nfs4_proc_remove(dir, name),
3026 &exception);
3027 } while (exception.retry);
3028 return err;
3029}
3030
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003031static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003033 struct nfs_server *server = NFS_SERVER(dir);
3034 struct nfs_removeargs *args = msg->rpc_argp;
3035 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003037 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003039 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003042static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3043{
3044 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3045 &data->args.seq_args,
3046 &data->res.seq_res,
3047 task))
3048 return;
3049 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003052static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003054 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3055
Trond Myklebust14516c32010-07-31 14:29:06 -04003056 if (!nfs4_sequence_done(task, &res->seq_res))
3057 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003058 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003059 return 0;
3060 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003061 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
Jeff Laytond3d41522010-09-17 17:31:57 -04003064static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3065{
3066 struct nfs_server *server = NFS_SERVER(dir);
3067 struct nfs_renameargs *arg = msg->rpc_argp;
3068 struct nfs_renameres *res = msg->rpc_resp;
3069
3070 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003071 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003072 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003073}
3074
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003075static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3076{
3077 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3078 &data->args.seq_args,
3079 &data->res.seq_res,
3080 task))
3081 return;
3082 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003083}
3084
3085static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3086 struct inode *new_dir)
3087{
3088 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3089
3090 if (!nfs4_sequence_done(task, &res->seq_res))
3091 return 0;
3092 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3093 return 0;
3094
3095 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003096 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003097 return 1;
3098}
3099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3101 struct inode *new_dir, struct qstr *new_name)
3102{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003103 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003104 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 .old_dir = NFS_FH(old_dir),
3106 .new_dir = NFS_FH(new_dir),
3107 .old_name = old_name,
3108 .new_name = new_name,
3109 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003110 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003111 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003112 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 struct rpc_message msg = {
3114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3115 .rpc_argp = &arg,
3116 .rpc_resp = &res,
3117 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003118 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Bryan Schumaker7c513052011-03-24 17:12:24 +00003120 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (!status) {
3122 update_changeattr(old_dir, &res.old_cinfo);
3123 update_changeattr(new_dir, &res.new_cinfo);
3124 }
3125 return status;
3126}
3127
3128static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3129 struct inode *new_dir, struct qstr *new_name)
3130{
3131 struct nfs4_exception exception = { };
3132 int err;
3133 do {
3134 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3135 _nfs4_proc_rename(old_dir, old_name,
3136 new_dir, new_name),
3137 &exception);
3138 } while (exception.retry);
3139 return err;
3140}
3141
3142static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3143{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003144 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 struct nfs4_link_arg arg = {
3146 .fh = NFS_FH(inode),
3147 .dir_fh = NFS_FH(dir),
3148 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003149 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003151 struct nfs4_link_res res = {
3152 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003153 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 struct rpc_message msg = {
3155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3156 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003157 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003159 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Trond Myklebust136f2622010-04-16 16:22:49 -04003161 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003162 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003163 goto out;
3164
Bryan Schumaker7c513052011-03-24 17:12:24 +00003165 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003166 if (!status) {
3167 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003168 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003169 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003170out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003171 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return status;
3173}
3174
3175static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3176{
3177 struct nfs4_exception exception = { };
3178 int err;
3179 do {
3180 err = nfs4_handle_exception(NFS_SERVER(inode),
3181 _nfs4_proc_link(inode, dir, name),
3182 &exception);
3183 } while (exception.retry);
3184 return err;
3185}
3186
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003187struct nfs4_createdata {
3188 struct rpc_message msg;
3189 struct nfs4_create_arg arg;
3190 struct nfs4_create_res res;
3191 struct nfs_fh fh;
3192 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003193};
3194
3195static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3196 struct qstr *name, struct iattr *sattr, u32 ftype)
3197{
3198 struct nfs4_createdata *data;
3199
3200 data = kzalloc(sizeof(*data), GFP_KERNEL);
3201 if (data != NULL) {
3202 struct nfs_server *server = NFS_SERVER(dir);
3203
3204 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3205 data->msg.rpc_argp = &data->arg;
3206 data->msg.rpc_resp = &data->res;
3207 data->arg.dir_fh = NFS_FH(dir);
3208 data->arg.server = server;
3209 data->arg.name = name;
3210 data->arg.attrs = sattr;
3211 data->arg.ftype = ftype;
3212 data->arg.bitmask = server->attr_bitmask;
3213 data->res.server = server;
3214 data->res.fh = &data->fh;
3215 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003216 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003217 }
3218 return data;
3219}
3220
3221static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3222{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003223 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003224 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003225 if (status == 0) {
3226 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003227 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3228 }
3229 return status;
3230}
3231
3232static void nfs4_free_createdata(struct nfs4_createdata *data)
3233{
3234 kfree(data);
3235}
3236
Chuck Lever4f390c12006-08-22 20:06:22 -04003237static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003238 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003240 struct nfs4_createdata *data;
3241 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Chuck Lever94a6d752006-08-22 20:06:23 -04003243 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003244 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003245
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003246 status = -ENOMEM;
3247 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3248 if (data == NULL)
3249 goto out;
3250
3251 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3252 data->arg.u.symlink.pages = &page;
3253 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003255 status = nfs4_do_create(dir, dentry, data);
3256
3257 nfs4_free_createdata(data);
3258out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return status;
3260}
3261
Chuck Lever4f390c12006-08-22 20:06:22 -04003262static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003263 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
3265 struct nfs4_exception exception = { };
3266 int err;
3267 do {
3268 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003269 _nfs4_proc_symlink(dir, dentry, page,
3270 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 &exception);
3272 } while (exception.retry);
3273 return err;
3274}
3275
3276static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3277 struct iattr *sattr)
3278{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003279 struct nfs4_createdata *data;
3280 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003282 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3283 if (data == NULL)
3284 goto out;
3285
3286 status = nfs4_do_create(dir, dentry, data);
3287
3288 nfs4_free_createdata(data);
3289out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return status;
3291}
3292
3293static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3294 struct iattr *sattr)
3295{
3296 struct nfs4_exception exception = { };
3297 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003298
3299 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 do {
3301 err = nfs4_handle_exception(NFS_SERVER(dir),
3302 _nfs4_proc_mkdir(dir, dentry, sattr),
3303 &exception);
3304 } while (exception.retry);
3305 return err;
3306}
3307
3308static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003309 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
3311 struct inode *dir = dentry->d_inode;
3312 struct nfs4_readdir_arg args = {
3313 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003314 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 .pgbase = 0,
3316 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003317 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003318 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 };
3320 struct nfs4_readdir_res res;
3321 struct rpc_message msg = {
3322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3323 .rpc_argp = &args,
3324 .rpc_resp = &res,
3325 .rpc_cred = cred,
3326 };
3327 int status;
3328
Harvey Harrison3110ff82008-05-02 13:42:44 -07003329 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003330 dentry->d_parent->d_name.name,
3331 dentry->d_name.name,
3332 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003333 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003335 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 -05003336 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003337 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003338 status += args.pgbase;
3339 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003340
3341 nfs_invalidate_atime(dir);
3342
Harvey Harrison3110ff82008-05-02 13:42:44 -07003343 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return status;
3345}
3346
3347static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003348 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
3350 struct nfs4_exception exception = { };
3351 int err;
3352 do {
3353 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3354 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003355 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 &exception);
3357 } while (exception.retry);
3358 return err;
3359}
3360
3361static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3362 struct iattr *sattr, dev_t rdev)
3363{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003364 struct nfs4_createdata *data;
3365 int mode = sattr->ia_mode;
3366 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003368 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3369 if (data == NULL)
3370 goto out;
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003373 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003375 data->arg.ftype = NF4BLK;
3376 data->arg.u.device.specdata1 = MAJOR(rdev);
3377 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 }
3379 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003380 data->arg.ftype = NF4CHR;
3381 data->arg.u.device.specdata1 = MAJOR(rdev);
3382 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003383 } else if (!S_ISSOCK(mode)) {
3384 status = -EINVAL;
3385 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003388 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003389out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003390 nfs4_free_createdata(data);
3391out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 return status;
3393}
3394
3395static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3396 struct iattr *sattr, dev_t rdev)
3397{
3398 struct nfs4_exception exception = { };
3399 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003400
3401 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 do {
3403 err = nfs4_handle_exception(NFS_SERVER(dir),
3404 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3405 &exception);
3406 } while (exception.retry);
3407 return err;
3408}
3409
3410static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3411 struct nfs_fsstat *fsstat)
3412{
3413 struct nfs4_statfs_arg args = {
3414 .fh = fhandle,
3415 .bitmask = server->attr_bitmask,
3416 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003417 struct nfs4_statfs_res res = {
3418 .fsstat = fsstat,
3419 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 struct rpc_message msg = {
3421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3422 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003423 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 };
3425
Trond Myklebust0e574af2005-10-27 22:12:38 -04003426 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003427 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428}
3429
3430static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3431{
3432 struct nfs4_exception exception = { };
3433 int err;
3434 do {
3435 err = nfs4_handle_exception(server,
3436 _nfs4_proc_statfs(server, fhandle, fsstat),
3437 &exception);
3438 } while (exception.retry);
3439 return err;
3440}
3441
3442static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3443 struct nfs_fsinfo *fsinfo)
3444{
3445 struct nfs4_fsinfo_arg args = {
3446 .fh = fhandle,
3447 .bitmask = server->attr_bitmask,
3448 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003449 struct nfs4_fsinfo_res res = {
3450 .fsinfo = fsinfo,
3451 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 struct rpc_message msg = {
3453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3454 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003455 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 };
3457
Bryan Schumaker7c513052011-03-24 17:12:24 +00003458 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
3460
3461static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3462{
3463 struct nfs4_exception exception = { };
3464 int err;
3465
3466 do {
3467 err = nfs4_handle_exception(server,
3468 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3469 &exception);
3470 } while (exception.retry);
3471 return err;
3472}
3473
3474static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3475{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003476 int error;
3477
Trond Myklebust0e574af2005-10-27 22:12:38 -04003478 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003479 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003480 if (error == 0) {
3481 /* block layout checks this! */
3482 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003483 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003484 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003485
3486 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
3488
3489static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3490 struct nfs_pathconf *pathconf)
3491{
3492 struct nfs4_pathconf_arg args = {
3493 .fh = fhandle,
3494 .bitmask = server->attr_bitmask,
3495 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003496 struct nfs4_pathconf_res res = {
3497 .pathconf = pathconf,
3498 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 struct rpc_message msg = {
3500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3501 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003502 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 };
3504
3505 /* None of the pathconf attributes are mandatory to implement */
3506 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3507 memset(pathconf, 0, sizeof(*pathconf));
3508 return 0;
3509 }
3510
Trond Myklebust0e574af2005-10-27 22:12:38 -04003511 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003512 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
3514
3515static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3516 struct nfs_pathconf *pathconf)
3517{
3518 struct nfs4_exception exception = { };
3519 int err;
3520
3521 do {
3522 err = nfs4_handle_exception(server,
3523 _nfs4_proc_pathconf(server, fhandle, pathconf),
3524 &exception);
3525 } while (exception.retry);
3526 return err;
3527}
3528
Benny Halevyd20581a2011-05-22 19:52:03 +03003529void __nfs4_read_done_cb(struct nfs_read_data *data)
3530{
Fred Isamancd841602012-04-20 14:47:44 -04003531 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003532}
3533
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003534static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
Fred Isamancd841602012-04-20 14:47:44 -04003536 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003538 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003539 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003540 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003542
Benny Halevyd20581a2011-05-22 19:52:03 +03003543 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003545 renew_lease(server, data->timestamp);
3546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003549static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3550{
3551
3552 dprintk("--> %s\n", __func__);
3553
3554 if (!nfs4_sequence_done(task, &data->res.seq_res))
3555 return -EAGAIN;
3556
Benny Halevyd20581a2011-05-22 19:52:03 +03003557 return data->read_done_cb ? data->read_done_cb(task, data) :
3558 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003559}
3560
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003561static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003564 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003565 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003566 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567}
3568
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003569static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3570{
Fred Isamancd841602012-04-20 14:47:44 -04003571 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003572 &data->args.seq_args,
3573 &data->res.seq_res,
3574 task))
3575 return;
3576 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Fred Isamanb029bc92011-03-03 15:13:42 +00003579static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
Fred Isamancd841602012-04-20 14:47:44 -04003581 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003583 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003584 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003585 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003587 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003589 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003590 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592}
3593
Fred Isamanb029bc92011-03-03 15:13:42 +00003594static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3595{
3596 if (!nfs4_sequence_done(task, &data->res.seq_res))
3597 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003598 return data->write_done_cb ? data->write_done_cb(task, data) :
3599 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003600}
3601
Trond Myklebust5a37f852012-04-28 14:55:16 -04003602static
3603bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003604{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003605 const struct nfs_pgio_header *hdr = data->header;
3606
3607 /* Don't request attributes for pNFS or O_DIRECT writes */
3608 if (data->ds_clp != NULL || hdr->dreq != NULL)
3609 return false;
3610 /* Otherwise, request attributes if and only if we don't hold
3611 * a delegation
3612 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003613 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003614}
Fred Isamana69aef12011-03-03 15:13:47 +00003615
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003616static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617{
Fred Isamancd841602012-04-20 14:47:44 -04003618 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003619
Trond Myklebust5a37f852012-04-28 14:55:16 -04003620 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003621 data->args.bitmask = NULL;
3622 data->res.fattr = NULL;
3623 } else
3624 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003625
Fred Isamanb029bc92011-03-03 15:13:42 +00003626 if (!data->write_done_cb)
3627 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003628 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 data->timestamp = jiffies;
3630
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003631 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003632 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
3634
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003635static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3636{
Fred Isamancd841602012-04-20 14:47:44 -04003637 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003638 &data->args.seq_args,
3639 &data->res.seq_res,
3640 task))
3641 return;
3642 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
Fred Isaman0b7c0152012-04-20 14:47:39 -04003645static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3646{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3648 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003649 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 return;
3652 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003653}
3654
Fred Isaman0b7c0152012-04-20 14:47:39 -04003655static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656{
3657 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003658
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003659 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003660 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003661 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664}
3665
Fred Isaman0b7c0152012-04-20 14:47:39 -04003666static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003667{
3668 if (!nfs4_sequence_done(task, &data->res.seq_res))
3669 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003670 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003671}
3672
Fred Isaman0b7c0152012-04-20 14:47:39 -04003673static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003675 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003676
Fred Isaman0b7c0152012-04-20 14:47:39 -04003677 if (data->commit_done_cb == NULL)
3678 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003679 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003680 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003681 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682}
3683
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003684struct nfs4_renewdata {
3685 struct nfs_client *client;
3686 unsigned long timestamp;
3687};
3688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689/*
3690 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3691 * standalone procedure for queueing an asynchronous RENEW.
3692 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003693static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003694{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003695 struct nfs4_renewdata *data = calldata;
3696 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003697
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003698 if (atomic_read(&clp->cl_count) > 1)
3699 nfs4_schedule_state_renewal(clp);
3700 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003701 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003702}
3703
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003704static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003706 struct nfs4_renewdata *data = calldata;
3707 struct nfs_client *clp = data->client;
3708 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
3710 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003711 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003712 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3713 return;
3714 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003715 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003716 return;
3717 }
3718 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003720 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721}
3722
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003723static const struct rpc_call_ops nfs4_renew_ops = {
3724 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003725 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003726};
3727
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003728static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
3730 struct rpc_message msg = {
3731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3732 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003733 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003735 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003737 if (renew_flags == 0)
3738 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003739 if (!atomic_inc_not_zero(&clp->cl_count))
3740 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003741 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003742 if (data == NULL)
3743 return -ENOMEM;
3744 data->client = clp;
3745 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003747 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
3749
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003750static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751{
3752 struct rpc_message msg = {
3753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3754 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003755 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 };
3757 unsigned long now = jiffies;
3758 int status;
3759
3760 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3761 if (status < 0)
3762 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003763 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 return 0;
3765}
3766
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003767static inline int nfs4_server_supports_acls(struct nfs_server *server)
3768{
3769 return (server->caps & NFS_CAP_ACLS)
3770 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3771 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3772}
3773
Trond Myklebust21f498c2012-08-24 10:59:25 -04003774/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3775 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003776 * the stack.
3777 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003778#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003779
Neil Hormane9e3d722011-03-04 19:26:03 -05003780static int buf_to_pages_noslab(const void *buf, size_t buflen,
3781 struct page **pages, unsigned int *pgbase)
3782{
3783 struct page *newpage, **spages;
3784 int rc = 0;
3785 size_t len;
3786 spages = pages;
3787
3788 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003789 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003790 newpage = alloc_page(GFP_KERNEL);
3791
3792 if (newpage == NULL)
3793 goto unwind;
3794 memcpy(page_address(newpage), buf, len);
3795 buf += len;
3796 buflen -= len;
3797 *pages++ = newpage;
3798 rc++;
3799 } while (buflen != 0);
3800
3801 return rc;
3802
3803unwind:
3804 for(; rc > 0; rc--)
3805 __free_page(spages[rc-1]);
3806 return -ENOMEM;
3807}
3808
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003809struct nfs4_cached_acl {
3810 int cached;
3811 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003812 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003813};
3814
3815static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003816{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003817 struct nfs_inode *nfsi = NFS_I(inode);
3818
3819 spin_lock(&inode->i_lock);
3820 kfree(nfsi->nfs4_acl);
3821 nfsi->nfs4_acl = acl;
3822 spin_unlock(&inode->i_lock);
3823}
3824
3825static void nfs4_zap_acl_attr(struct inode *inode)
3826{
3827 nfs4_set_cached_acl(inode, NULL);
3828}
3829
3830static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3831{
3832 struct nfs_inode *nfsi = NFS_I(inode);
3833 struct nfs4_cached_acl *acl;
3834 int ret = -ENOENT;
3835
3836 spin_lock(&inode->i_lock);
3837 acl = nfsi->nfs4_acl;
3838 if (acl == NULL)
3839 goto out;
3840 if (buf == NULL) /* user is just asking for length */
3841 goto out_len;
3842 if (acl->cached == 0)
3843 goto out;
3844 ret = -ERANGE; /* see getxattr(2) man page */
3845 if (acl->len > buflen)
3846 goto out;
3847 memcpy(buf, acl->data, acl->len);
3848out_len:
3849 ret = acl->len;
3850out:
3851 spin_unlock(&inode->i_lock);
3852 return ret;
3853}
3854
Sachin Prabhu5794d212012-04-17 14:36:40 +01003855static 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 +00003856{
3857 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003858 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003859
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003860 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003861 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003862 if (acl == NULL)
3863 goto out;
3864 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003865 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003866 } else {
3867 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3868 if (acl == NULL)
3869 goto out;
3870 acl->cached = 0;
3871 }
3872 acl->len = acl_len;
3873out:
3874 nfs4_set_cached_acl(inode, acl);
3875}
3876
Andy Adamsonbf118a32011-12-07 11:55:27 -05003877/*
3878 * The getxattr API returns the required buffer length when called with a
3879 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3880 * the required buf. On a NULL buf, we send a page of data to the server
3881 * guessing that the ACL request can be serviced by a page. If so, we cache
3882 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3883 * the cache. If not so, we throw away the page, and cache the required
3884 * length. The next getxattr call will then produce another round trip to
3885 * the server, this time with the input buf of the required size.
3886 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003887static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003888{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003889 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003890 struct nfs_getaclargs args = {
3891 .fh = NFS_FH(inode),
3892 .acl_pages = pages,
3893 .acl_len = buflen,
3894 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003895 struct nfs_getaclres res = {
3896 .acl_len = buflen,
3897 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003898 struct rpc_message msg = {
3899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3900 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003901 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003902 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003903 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3904 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003905
Andy Adamsonbf118a32011-12-07 11:55:27 -05003906 /* As long as we're doing a round trip to the server anyway,
3907 * let's be prepared for a page of acl data. */
3908 if (npages == 0)
3909 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003910 if (npages > ARRAY_SIZE(pages))
3911 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003912
Andy Adamsonbf118a32011-12-07 11:55:27 -05003913 for (i = 0; i < npages; i++) {
3914 pages[i] = alloc_page(GFP_KERNEL);
3915 if (!pages[i])
3916 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003917 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003918
3919 /* for decoding across pages */
3920 res.acl_scratch = alloc_page(GFP_KERNEL);
3921 if (!res.acl_scratch)
3922 goto out_free;
3923
Andy Adamsonbf118a32011-12-07 11:55:27 -05003924 args.acl_len = npages * PAGE_SIZE;
3925 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003926
Peng Taode040be2012-01-10 22:42:47 +08003927 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003928 __func__, buf, buflen, npages, args.acl_len);
3929 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3930 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003931 if (ret)
3932 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003933
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003934 /* Handle the case where the passed-in buffer is too short */
3935 if (res.acl_flags & NFS4_ACL_TRUNC) {
3936 /* Did the user only issue a request for the acl length? */
3937 if (buf == NULL)
3938 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003939 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003940 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003941 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003942 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3943 if (buf)
3944 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3945out_ok:
3946 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003947out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003948 for (i = 0; i < npages; i++)
3949 if (pages[i])
3950 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003951 if (res.acl_scratch)
3952 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003953 return ret;
3954}
3955
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003956static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3957{
3958 struct nfs4_exception exception = { };
3959 ssize_t ret;
3960 do {
3961 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3962 if (ret >= 0)
3963 break;
3964 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3965 } while (exception.retry);
3966 return ret;
3967}
3968
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003969static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3970{
3971 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003972 int ret;
3973
3974 if (!nfs4_server_supports_acls(server))
3975 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003976 ret = nfs_revalidate_inode(server, inode);
3977 if (ret < 0)
3978 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003979 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3980 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003981 ret = nfs4_read_cached_acl(inode, buf, buflen);
3982 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003983 /* -ENOENT is returned if there is no ACL or if there is an ACL
3984 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003985 return ret;
3986 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003987}
3988
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003989static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003990{
3991 struct nfs_server *server = NFS_SERVER(inode);
3992 struct page *pages[NFS4ACL_MAXPAGES];
3993 struct nfs_setaclargs arg = {
3994 .fh = NFS_FH(inode),
3995 .acl_pages = pages,
3996 .acl_len = buflen,
3997 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003998 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003999 struct rpc_message msg = {
4000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4001 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004002 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004003 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004004 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004005 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004006
4007 if (!nfs4_server_supports_acls(server))
4008 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004009 if (npages > ARRAY_SIZE(pages))
4010 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004011 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4012 if (i < 0)
4013 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004014 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004015 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004016
4017 /*
4018 * Free each page after tx, so the only ref left is
4019 * held by the network stack
4020 */
4021 for (; i > 0; i--)
4022 put_page(pages[i-1]);
4023
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004024 /*
4025 * Acl update can result in inode attribute update.
4026 * so mark the attribute cache invalid.
4027 */
4028 spin_lock(&inode->i_lock);
4029 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4030 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004031 nfs_access_zap_cache(inode);
4032 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004033 return ret;
4034}
4035
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004036static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4037{
4038 struct nfs4_exception exception = { };
4039 int err;
4040 do {
4041 err = nfs4_handle_exception(NFS_SERVER(inode),
4042 __nfs4_proc_set_acl(inode, buf, buflen),
4043 &exception);
4044 } while (exception.retry);
4045 return err;
4046}
4047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004049nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004051 struct nfs_client *clp = server->nfs_client;
4052
4053 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 return 0;
4055 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004056 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004057 case -NFS4ERR_ADMIN_REVOKED:
4058 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004059 if (state == NULL)
4060 break;
4061 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004062 case -NFS4ERR_OPENMODE:
4063 if (state == NULL)
4064 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004065 nfs4_schedule_stateid_recovery(server, state);
4066 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004067 case -NFS4ERR_EXPIRED:
4068 if (state != NULL)
4069 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004071 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004072 nfs4_schedule_lease_recovery(clp);
4073 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004074#if defined(CONFIG_NFS_V4_1)
4075 case -NFS4ERR_BADSESSION:
4076 case -NFS4ERR_BADSLOT:
4077 case -NFS4ERR_BAD_HIGH_SLOT:
4078 case -NFS4ERR_DEADSESSION:
4079 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4080 case -NFS4ERR_SEQ_FALSE_RETRY:
4081 case -NFS4ERR_SEQ_MISORDERED:
4082 dprintk("%s ERROR %d, Reset session\n", __func__,
4083 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004084 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004085 task->tk_status = 0;
4086 return -EAGAIN;
4087#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004089 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004090 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004091 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4093 task->tk_status = 0;
4094 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004095 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 case -NFS4ERR_OLD_STATEID:
4097 task->tk_status = 0;
4098 return -EAGAIN;
4099 }
4100 task->tk_status = nfs4_map_errors(task->tk_status);
4101 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004102wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004103 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004104 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4105 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4106 task->tk_status = 0;
4107 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108}
4109
Chuck Leverf0920752012-05-21 22:45:41 -04004110static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4111 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004112{
4113 __be32 verf[2];
4114
Chuck Lever2c820d92012-05-21 22:45:33 -04004115 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4116 /* An impossible timestamp guarantees this value
4117 * will never match a generated boot time. */
4118 verf[0] = 0;
4119 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4120 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004121 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4122 verf[0] = (__be32)nn->boot_time.tv_sec;
4123 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004124 }
Chuck Levercd937102012-03-02 17:14:31 -05004125 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4126}
4127
Chuck Levere984a552012-09-14 17:24:21 -04004128static unsigned int
4129nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4130 char *buf, size_t len)
4131{
4132 unsigned int result;
4133
4134 rcu_read_lock();
4135 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4136 clp->cl_ipaddr,
4137 rpc_peeraddr2str(clp->cl_rpcclient,
4138 RPC_DISPLAY_ADDR),
4139 rpc_peeraddr2str(clp->cl_rpcclient,
4140 RPC_DISPLAY_PROTO));
4141 rcu_read_unlock();
4142 return result;
4143}
4144
4145static unsigned int
4146nfs4_init_uniform_client_string(const struct nfs_client *clp,
4147 char *buf, size_t len)
4148{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004149 char *nodename = clp->cl_rpcclient->cl_nodename;
4150
4151 if (nfs4_client_id_uniquifier[0] != '\0')
4152 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004153 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4154 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004155 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004156}
4157
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004158/**
4159 * nfs4_proc_setclientid - Negotiate client ID
4160 * @clp: state data structure
4161 * @program: RPC program for NFSv4 callback service
4162 * @port: IP port number for NFS4 callback service
4163 * @cred: RPC credential to use for this call
4164 * @res: where to place the result
4165 *
4166 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4167 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004168int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4169 unsigned short port, struct rpc_cred *cred,
4170 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171{
4172 nfs4_verifier sc_verifier;
4173 struct nfs4_setclientid setclientid = {
4174 .sc_verifier = &sc_verifier,
4175 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004176 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 };
4178 struct rpc_message msg = {
4179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4180 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004181 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004182 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004184 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Chuck Leverde734832012-07-11 16:30:50 -04004186 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004187 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004188 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4189 setclientid.sc_name_len =
4190 nfs4_init_uniform_client_string(clp,
4191 setclientid.sc_name,
4192 sizeof(setclientid.sc_name));
4193 else
4194 setclientid.sc_name_len =
4195 nfs4_init_nonuniform_client_string(clp,
4196 setclientid.sc_name,
4197 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004198 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004199 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004200 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004201 sizeof(setclientid.sc_netid),
4202 rpc_peeraddr2str(clp->cl_rpcclient,
4203 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004204 rcu_read_unlock();
4205 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004206 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 clp->cl_ipaddr, port >> 8, port & 255);
4208
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004209 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4210 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4211 setclientid.sc_name_len, setclientid.sc_name);
4212 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4213 dprintk("NFS reply setclientid: %d\n", status);
4214 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004217/**
4218 * nfs4_proc_setclientid_confirm - Confirm client ID
4219 * @clp: state data structure
4220 * @res: result of a previous SETCLIENTID
4221 * @cred: RPC credential to use for this call
4222 *
4223 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4224 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004225int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004226 struct nfs4_setclientid_res *arg,
4227 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228{
4229 struct nfs_fsinfo fsinfo;
4230 struct rpc_message msg = {
4231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004232 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004234 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 };
4236 unsigned long now;
4237 int status;
4238
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004239 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4240 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4241 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004243 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 if (status == 0) {
4245 spin_lock(&clp->cl_lock);
4246 clp->cl_lease_time = fsinfo.lease_time * HZ;
4247 clp->cl_last_renewal = now;
4248 spin_unlock(&clp->cl_lock);
4249 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004250 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 return status;
4252}
4253
Trond Myklebustfe650402006-01-03 09:55:18 +01004254struct nfs4_delegreturndata {
4255 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004256 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004257 struct nfs_fh fh;
4258 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004259 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004260 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004261 int rpc_status;
4262};
4263
Trond Myklebustfe650402006-01-03 09:55:18 +01004264static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4265{
4266 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004267
Trond Myklebust14516c32010-07-31 14:29:06 -04004268 if (!nfs4_sequence_done(task, &data->res.seq_res))
4269 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004270
Ricardo Labiaga79708862009-12-07 09:23:21 -05004271 switch (task->tk_status) {
4272 case -NFS4ERR_STALE_STATEID:
4273 case -NFS4ERR_EXPIRED:
4274 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004275 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004276 break;
4277 default:
4278 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4279 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004280 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004281 return;
4282 }
4283 }
4284 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004285}
4286
4287static void nfs4_delegreturn_release(void *calldata)
4288{
Trond Myklebustfe650402006-01-03 09:55:18 +01004289 kfree(calldata);
4290}
4291
Andy Adamson938e1012009-04-01 09:22:28 -04004292#if defined(CONFIG_NFS_V4_1)
4293static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4294{
4295 struct nfs4_delegreturndata *d_data;
4296
4297 d_data = (struct nfs4_delegreturndata *)data;
4298
Trond Myklebust035168a2010-06-16 09:52:26 -04004299 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004300 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004301 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004302 return;
4303 rpc_call_start(task);
4304}
4305#endif /* CONFIG_NFS_V4_1 */
4306
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004307static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004308#if defined(CONFIG_NFS_V4_1)
4309 .rpc_call_prepare = nfs4_delegreturn_prepare,
4310#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004311 .rpc_call_done = nfs4_delegreturn_done,
4312 .rpc_release = nfs4_delegreturn_release,
4313};
4314
Trond Myklebuste6f81072008-01-24 18:14:34 -05004315static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004316{
4317 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004318 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004319 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004320 struct rpc_message msg = {
4321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4322 .rpc_cred = cred,
4323 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004324 struct rpc_task_setup task_setup_data = {
4325 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004326 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004327 .callback_ops = &nfs4_delegreturn_ops,
4328 .flags = RPC_TASK_ASYNC,
4329 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004330 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004331
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004332 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004333 if (data == NULL)
4334 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004335 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004336 data->args.fhandle = &data->fh;
4337 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004338 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004339 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004340 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004341 data->res.fattr = &data->fattr;
4342 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004343 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004344 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004345 data->rpc_status = 0;
4346
Trond Myklebustc970aa82007-07-14 15:39:59 -04004347 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004348 msg.rpc_argp = &data->args;
4349 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004350 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004351 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004352 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004353 if (!issync)
4354 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004355 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004356 if (status != 0)
4357 goto out;
4358 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004359 if (status == 0)
4360 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4361 else
4362 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004363out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004364 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004365 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366}
4367
Trond Myklebuste6f81072008-01-24 18:14:34 -05004368int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369{
4370 struct nfs_server *server = NFS_SERVER(inode);
4371 struct nfs4_exception exception = { };
4372 int err;
4373 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004374 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 switch (err) {
4376 case -NFS4ERR_STALE_STATEID:
4377 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 case 0:
4379 return 0;
4380 }
4381 err = nfs4_handle_exception(server, err, &exception);
4382 } while (exception.retry);
4383 return err;
4384}
4385
4386#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4387#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4388
4389/*
4390 * sleep, with exponential backoff, and retry the LOCK operation.
4391 */
4392static unsigned long
4393nfs4_set_lock_task_retry(unsigned long timeout)
4394{
Jeff Laytond3103102011-12-01 22:44:39 +01004395 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 timeout <<= 1;
4397 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4398 return NFS4_LOCK_MAXTIMEOUT;
4399 return timeout;
4400}
4401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4403{
4404 struct inode *inode = state->inode;
4405 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004406 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004407 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004409 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004411 struct nfs_lockt_res res = {
4412 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 };
4414 struct rpc_message msg = {
4415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4416 .rpc_argp = &arg,
4417 .rpc_resp = &res,
4418 .rpc_cred = state->owner->so_cred,
4419 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 struct nfs4_lock_state *lsp;
4421 int status;
4422
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004423 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004424 status = nfs4_set_lock_state(state, request);
4425 if (status != 0)
4426 goto out;
4427 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004428 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004429 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004430 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004431 switch (status) {
4432 case 0:
4433 request->fl_type = F_UNLCK;
4434 break;
4435 case -NFS4ERR_DENIED:
4436 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004438 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004439out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 return status;
4441}
4442
4443static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4444{
4445 struct nfs4_exception exception = { };
4446 int err;
4447
4448 do {
4449 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4450 _nfs4_proc_getlk(state, cmd, request),
4451 &exception);
4452 } while (exception.retry);
4453 return err;
4454}
4455
4456static int do_vfs_lock(struct file *file, struct file_lock *fl)
4457{
4458 int res = 0;
4459 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4460 case FL_POSIX:
4461 res = posix_lock_file_wait(file, fl);
4462 break;
4463 case FL_FLOCK:
4464 res = flock_lock_file_wait(file, fl);
4465 break;
4466 default:
4467 BUG();
4468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return res;
4470}
4471
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004472struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004473 struct nfs_locku_args arg;
4474 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004475 struct nfs4_lock_state *lsp;
4476 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004477 struct file_lock fl;
4478 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004479 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004480};
4481
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004482static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4483 struct nfs_open_context *ctx,
4484 struct nfs4_lock_state *lsp,
4485 struct nfs_seqid *seqid)
4486{
4487 struct nfs4_unlockdata *p;
4488 struct inode *inode = lsp->ls_state->inode;
4489
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004490 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004491 if (p == NULL)
4492 return NULL;
4493 p->arg.fh = NFS_FH(inode);
4494 p->arg.fl = &p->fl;
4495 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004496 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004497 p->arg.stateid = &lsp->ls_stateid;
4498 p->lsp = lsp;
4499 atomic_inc(&lsp->ls_count);
4500 /* Ensure we don't close file until we're done freeing locks! */
4501 p->ctx = get_nfs_open_context(ctx);
4502 memcpy(&p->fl, fl, sizeof(p->fl));
4503 p->server = NFS_SERVER(inode);
4504 return p;
4505}
4506
Trond Myklebust06f814a2006-01-03 09:55:07 +01004507static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004508{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004509 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004510 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004511 nfs4_put_lock_state(calldata->lsp);
4512 put_nfs_open_context(calldata->ctx);
4513 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004514}
4515
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004516static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004517{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004518 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004519
Trond Myklebust14516c32010-07-31 14:29:06 -04004520 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4521 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004522 switch (task->tk_status) {
4523 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004524 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4525 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004526 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004527 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004528 case -NFS4ERR_BAD_STATEID:
4529 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004530 case -NFS4ERR_STALE_STATEID:
4531 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004532 break;
4533 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004534 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004535 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004536 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004537 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004538}
4539
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004540static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004541{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004542 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004544 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004545 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004546 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004547 /* Note: exit _without_ running nfs4_locku_done */
4548 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004549 return;
4550 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004551 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004552 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004553 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004554 &calldata->res.seq_res,
4555 task) != 0)
4556 nfs_release_seqid(calldata->arg.seqid);
4557 else
4558 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559}
4560
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004561static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004562 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004563 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004564 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004565};
4566
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4568 struct nfs_open_context *ctx,
4569 struct nfs4_lock_state *lsp,
4570 struct nfs_seqid *seqid)
4571{
4572 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004573 struct rpc_message msg = {
4574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4575 .rpc_cred = ctx->cred,
4576 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004577 struct rpc_task_setup task_setup_data = {
4578 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004579 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004580 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004581 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004582 .flags = RPC_TASK_ASYNC,
4583 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004584
Frank Filz137d6ac2007-07-09 15:32:29 -07004585 /* Ensure this is an unlock - when canceling a lock, the
4586 * canceled lock is passed in, and it won't be an unlock.
4587 */
4588 fl->fl_type = F_UNLCK;
4589
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004590 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4591 if (data == NULL) {
4592 nfs_free_seqid(seqid);
4593 return ERR_PTR(-ENOMEM);
4594 }
4595
Trond Myklebust9d12b212012-01-17 22:04:25 -05004596 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004597 msg.rpc_argp = &data->arg;
4598 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004599 task_setup_data.callback_data = data;
4600 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004601}
4602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4604{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004605 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004606 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004607 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004608 struct rpc_task *task;
4609 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004610 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Trond Myklebust9b073572006-06-29 16:38:34 -04004612 status = nfs4_set_lock_state(state, request);
4613 /* Unlock _before_ we do the RPC call */
4614 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004615 down_read(&nfsi->rwsem);
4616 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4617 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004618 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004619 }
4620 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004621 if (status != 0)
4622 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004623 /* Is this a delegated lock? */
4624 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004625 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004626 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004627 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004628 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004629 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004630 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004631 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004632 status = PTR_ERR(task);
4633 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004634 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004635 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004636 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004637out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004638 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004639 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640}
4641
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004642struct nfs4_lockdata {
4643 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004644 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004645 struct nfs4_lock_state *lsp;
4646 struct nfs_open_context *ctx;
4647 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004648 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004649 int rpc_status;
4650 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004651 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004652};
4653
4654static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004655 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4656 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004657{
4658 struct nfs4_lockdata *p;
4659 struct inode *inode = lsp->ls_state->inode;
4660 struct nfs_server *server = NFS_SERVER(inode);
4661
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004662 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004663 if (p == NULL)
4664 return NULL;
4665
4666 p->arg.fh = NFS_FH(inode);
4667 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004668 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004669 if (p->arg.open_seqid == NULL)
4670 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004671 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004672 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004673 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004674 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004675 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004676 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004677 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004678 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004679 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004680 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004681 atomic_inc(&lsp->ls_count);
4682 p->ctx = get_nfs_open_context(ctx);
4683 memcpy(&p->fl, fl, sizeof(p->fl));
4684 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004685out_free_seqid:
4686 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004687out_free:
4688 kfree(p);
4689 return NULL;
4690}
4691
4692static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4693{
4694 struct nfs4_lockdata *data = calldata;
4695 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Harvey Harrison3110ff82008-05-02 13:42:44 -07004697 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004698 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4699 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004700 /* Do we need to do an open_to_lock_owner? */
4701 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004702 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004703 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004704 data->arg.open_stateid = &state->stateid;
4705 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004706 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004707 } else
4708 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004709 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004710 if (nfs4_setup_sequence(data->server,
4711 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004712 &data->res.seq_res,
4713 task) == 0) {
4714 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004715 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004716 }
4717 nfs_release_seqid(data->arg.open_seqid);
4718out_release_lock_seqid:
4719 nfs_release_seqid(data->arg.lock_seqid);
4720 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004721}
4722
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004723static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4724{
4725 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4726 nfs4_lock_prepare(task, calldata);
4727}
4728
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004729static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4730{
4731 struct nfs4_lockdata *data = calldata;
4732
Harvey Harrison3110ff82008-05-02 13:42:44 -07004733 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004734
Trond Myklebust14516c32010-07-31 14:29:06 -04004735 if (!nfs4_sequence_done(task, &data->res.seq_res))
4736 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004737
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004738 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004739 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004740 if (data->rpc_status == 0)
4741 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4742 else
4743 goto out;
4744 }
4745 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004746 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004747 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004748 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004749 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004750out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004751 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004752}
4753
4754static void nfs4_lock_release(void *calldata)
4755{
4756 struct nfs4_lockdata *data = calldata;
4757
Harvey Harrison3110ff82008-05-02 13:42:44 -07004758 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004759 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004760 if (data->cancelled != 0) {
4761 struct rpc_task *task;
4762 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4763 data->arg.lock_seqid);
4764 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004765 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004766 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004767 } else
4768 nfs_free_seqid(data->arg.lock_seqid);
4769 nfs4_put_lock_state(data->lsp);
4770 put_nfs_open_context(data->ctx);
4771 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004772 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004773}
4774
4775static const struct rpc_call_ops nfs4_lock_ops = {
4776 .rpc_call_prepare = nfs4_lock_prepare,
4777 .rpc_call_done = nfs4_lock_done,
4778 .rpc_release = nfs4_lock_release,
4779};
4780
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004781static const struct rpc_call_ops nfs4_recover_lock_ops = {
4782 .rpc_call_prepare = nfs4_recover_lock_prepare,
4783 .rpc_call_done = nfs4_lock_done,
4784 .rpc_release = nfs4_lock_release,
4785};
4786
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004787static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4788{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004789 switch (error) {
4790 case -NFS4ERR_ADMIN_REVOKED:
4791 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004792 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004793 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004794 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004795 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004796 break;
4797 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004798 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004799 case -NFS4ERR_EXPIRED:
4800 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004801 };
4802}
4803
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004804static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004805{
4806 struct nfs4_lockdata *data;
4807 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004808 struct rpc_message msg = {
4809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4810 .rpc_cred = state->owner->so_cred,
4811 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004812 struct rpc_task_setup task_setup_data = {
4813 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004814 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004815 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004816 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004817 .flags = RPC_TASK_ASYNC,
4818 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004819 int ret;
4820
Harvey Harrison3110ff82008-05-02 13:42:44 -07004821 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004822 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004823 fl->fl_u.nfs4_fl.owner,
4824 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004825 if (data == NULL)
4826 return -ENOMEM;
4827 if (IS_SETLKW(cmd))
4828 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004829 if (recovery_type > NFS_LOCK_NEW) {
4830 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004831 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004832 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4833 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004834 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004835 msg.rpc_argp = &data->arg;
4836 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004837 task_setup_data.callback_data = data;
4838 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004839 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004840 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004841 ret = nfs4_wait_for_completion_rpc_task(task);
4842 if (ret == 0) {
4843 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004844 if (ret)
4845 nfs4_handle_setlk_error(data->server, data->lsp,
4846 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004847 } else
4848 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004849 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004850 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004851 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
4854static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4855{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004856 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004857 struct nfs4_exception exception = {
4858 .inode = state->inode,
4859 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004860 int err;
4861
4862 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004863 /* Cache the lock if possible... */
4864 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4865 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004866 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004867 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004868 break;
4869 nfs4_handle_exception(server, err, &exception);
4870 } while (exception.retry);
4871 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872}
4873
4874static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4875{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004876 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004877 struct nfs4_exception exception = {
4878 .inode = state->inode,
4879 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004880 int err;
4881
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004882 err = nfs4_set_lock_state(state, request);
4883 if (err != 0)
4884 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004885 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004886 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4887 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004888 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004889 switch (err) {
4890 default:
4891 goto out;
4892 case -NFS4ERR_GRACE:
4893 case -NFS4ERR_DELAY:
4894 nfs4_handle_exception(server, err, &exception);
4895 err = 0;
4896 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004897 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004898out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004899 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
4901
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004902#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004903/**
4904 * nfs41_check_expired_locks - possibly free a lock stateid
4905 *
4906 * @state: NFSv4 state for an inode
4907 *
4908 * Returns NFS_OK if recovery for this stateid is now finished.
4909 * Otherwise a negative NFS4ERR value is returned.
4910 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004911static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004912{
Chuck Levereb64cf92012-07-11 16:30:05 -04004913 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004914 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004915 struct nfs_server *server = NFS_SERVER(state->inode);
4916
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004917 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004918 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004919 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4920 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004921 /* Free the stateid unless the server
4922 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004923 if (status != -NFS4ERR_BAD_STATEID)
4924 nfs41_free_stateid(server,
4925 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004926 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004927 ret = status;
4928 }
4929 }
4930 };
4931
4932 return ret;
4933}
4934
4935static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4936{
4937 int status = NFS_OK;
4938
4939 if (test_bit(LK_STATE_IN_USE, &state->flags))
4940 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004941 if (status != NFS_OK)
4942 status = nfs4_lock_expired(state, request);
4943 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004944}
4945#endif
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4948{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004949 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004950 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004951 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004953 if ((fl_flags & FL_POSIX) &&
4954 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4955 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004956 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004957 status = nfs4_set_lock_state(state, request);
4958 if (status != 0)
4959 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004960 request->fl_flags |= FL_ACCESS;
4961 status = do_vfs_lock(request->fl_file, request);
4962 if (status < 0)
4963 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004964 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004965 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004966 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004967 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004968 request->fl_flags = fl_flags & ~FL_SLEEP;
4969 status = do_vfs_lock(request->fl_file, request);
4970 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004971 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004972 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004973 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004974 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004975 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004976 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004977 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004978 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4979 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004980out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004981 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004982out:
4983 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 return status;
4985}
4986
4987static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4988{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004989 struct nfs4_exception exception = {
4990 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004991 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004992 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 int err;
4994
4995 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004996 err = _nfs4_proc_setlk(state, cmd, request);
4997 if (err == -NFS4ERR_DENIED)
4998 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005000 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 } while (exception.retry);
5002 return err;
5003}
5004
5005static int
5006nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5007{
5008 struct nfs_open_context *ctx;
5009 struct nfs4_state *state;
5010 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5011 int status;
5012
5013 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005014 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 state = ctx->state;
5016
5017 if (request->fl_start < 0 || request->fl_end < 0)
5018 return -EINVAL;
5019
Trond Myklebustd9531262009-07-21 19:22:38 -04005020 if (IS_GETLK(cmd)) {
5021 if (state != NULL)
5022 return nfs4_proc_getlk(state, F_GETLK, request);
5023 return 0;
5024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5027 return -EINVAL;
5028
Trond Myklebustd9531262009-07-21 19:22:38 -04005029 if (request->fl_type == F_UNLCK) {
5030 if (state != NULL)
5031 return nfs4_proc_unlck(state, cmd, request);
5032 return 0;
5033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Trond Myklebustd9531262009-07-21 19:22:38 -04005035 if (state == NULL)
5036 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005037 /*
5038 * Don't rely on the VFS having checked the file open mode,
5039 * since it won't do this for flock() locks.
5040 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005041 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005042 case F_RDLCK:
5043 if (!(filp->f_mode & FMODE_READ))
5044 return -EBADF;
5045 break;
5046 case F_WRLCK:
5047 if (!(filp->f_mode & FMODE_WRITE))
5048 return -EBADF;
5049 }
5050
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 do {
5052 status = nfs4_proc_setlk(state, cmd, request);
5053 if ((status != -EAGAIN) || IS_SETLK(cmd))
5054 break;
5055 timeout = nfs4_set_lock_task_retry(timeout);
5056 status = -ERESTARTSYS;
5057 if (signalled())
5058 break;
5059 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 return status;
5061}
5062
Trond Myklebust888e6942005-11-04 15:38:11 -05005063int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5064{
5065 struct nfs_server *server = NFS_SERVER(state->inode);
5066 struct nfs4_exception exception = { };
5067 int err;
5068
5069 err = nfs4_set_lock_state(state, fl);
5070 if (err != 0)
5071 goto out;
5072 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005073 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005074 switch (err) {
5075 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005076 printk(KERN_ERR "NFS: %s: unhandled error "
5077 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005078 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005079 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005080 goto out;
5081 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005082 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005083 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005084 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005085 nfs4_schedule_lease_recovery(server->nfs_client);
5086 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005087 case -NFS4ERR_BADSESSION:
5088 case -NFS4ERR_BADSLOT:
5089 case -NFS4ERR_BAD_HIGH_SLOT:
5090 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5091 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005092 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005093 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005094 case -ERESTARTSYS:
5095 /*
5096 * The show must go on: exit, but mark the
5097 * stateid as needing recovery.
5098 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005099 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005100 case -NFS4ERR_ADMIN_REVOKED:
5101 case -NFS4ERR_BAD_STATEID:
5102 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005103 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005104 err = 0;
5105 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005106 case -EKEYEXPIRED:
5107 /*
5108 * User RPCSEC_GSS context has expired.
5109 * We cannot recover this stateid now, so
5110 * skip it and allow recovery thread to
5111 * proceed.
5112 */
5113 err = 0;
5114 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005115 case -ENOMEM:
5116 case -NFS4ERR_DENIED:
5117 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5118 err = 0;
5119 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005120 case -NFS4ERR_DELAY:
5121 break;
5122 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005123 err = nfs4_handle_exception(server, err, &exception);
5124 } while (exception.retry);
5125out:
5126 return err;
5127}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005128
Trond Myklebustcf470c32012-03-07 13:49:12 -05005129struct nfs_release_lockowner_data {
5130 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005131 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005132 struct nfs_release_lockowner_args args;
5133};
5134
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005135static void nfs4_release_lockowner_release(void *calldata)
5136{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005137 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005138 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005139 kfree(calldata);
5140}
5141
Trond Myklebust17280172012-03-11 13:11:00 -04005142static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005143 .rpc_release = nfs4_release_lockowner_release,
5144};
5145
Trond Myklebustcf470c32012-03-07 13:49:12 -05005146int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005147{
5148 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005149 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005150 struct rpc_message msg = {
5151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5152 };
5153
5154 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005155 return -EINVAL;
5156 data = kmalloc(sizeof(*data), GFP_NOFS);
5157 if (!data)
5158 return -ENOMEM;
5159 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005160 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005161 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5162 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5163 data->args.lock_owner.s_dev = server->s_dev;
5164 msg.rpc_argp = &data->args;
5165 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5166 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005167}
5168
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005169#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5170
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005171static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5172 const void *buf, size_t buflen,
5173 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005174{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005175 if (strcmp(key, "") != 0)
5176 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005177
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005178 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005179}
5180
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005181static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5182 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005183{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005184 if (strcmp(key, "") != 0)
5185 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005186
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005187 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005188}
5189
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005190static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5191 size_t list_len, const char *name,
5192 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005193{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005194 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005195
J. Bruce Fields096455a2006-03-20 23:23:42 -05005196 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5197 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005198
5199 if (list && len <= list_len)
5200 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005201 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005202}
5203
Andy Adamson533eb462011-06-13 18:25:56 -04005204/*
5205 * nfs_fhget will use either the mounted_on_fileid or the fileid
5206 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005207static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5208{
Andy Adamson533eb462011-06-13 18:25:56 -04005209 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5210 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5211 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005212 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005213 return;
5214
5215 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005216 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005217 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5218 fattr->nlink = 2;
5219}
5220
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005221static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5222 const struct qstr *name,
5223 struct nfs4_fs_locations *fs_locations,
5224 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005225{
5226 struct nfs_server *server = NFS_SERVER(dir);
5227 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005228 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005229 };
5230 struct nfs4_fs_locations_arg args = {
5231 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005232 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005233 .page = page,
5234 .bitmask = bitmask,
5235 };
Benny Halevy22958462009-04-01 09:22:02 -04005236 struct nfs4_fs_locations_res res = {
5237 .fs_locations = fs_locations,
5238 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005239 struct rpc_message msg = {
5240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5241 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005242 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005243 };
5244 int status;
5245
Harvey Harrison3110ff82008-05-02 13:42:44 -07005246 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005247
5248 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5249 * is not supported */
5250 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5251 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5252 else
5253 bitmask[0] |= FATTR4_WORD0_FILEID;
5254
Trond Myklebustc228fd32007-01-13 02:28:11 -05005255 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005256 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005257 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005258 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005259 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005260 return status;
5261}
5262
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005263int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5264 const struct qstr *name,
5265 struct nfs4_fs_locations *fs_locations,
5266 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005267{
5268 struct nfs4_exception exception = { };
5269 int err;
5270 do {
5271 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005272 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005273 &exception);
5274 } while (exception.retry);
5275 return err;
5276}
5277
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005278static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5279{
5280 int status;
5281 struct nfs4_secinfo_arg args = {
5282 .dir_fh = NFS_FH(dir),
5283 .name = name,
5284 };
5285 struct nfs4_secinfo_res res = {
5286 .flavors = flavors,
5287 };
5288 struct rpc_message msg = {
5289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5290 .rpc_argp = &args,
5291 .rpc_resp = &res,
5292 };
5293
5294 dprintk("NFS call secinfo %s\n", name->name);
5295 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5296 dprintk("NFS reply secinfo: %d\n", status);
5297 return status;
5298}
5299
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005300int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5301 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005302{
5303 struct nfs4_exception exception = { };
5304 int err;
5305 do {
5306 err = nfs4_handle_exception(NFS_SERVER(dir),
5307 _nfs4_proc_secinfo(dir, name, flavors),
5308 &exception);
5309 } while (exception.retry);
5310 return err;
5311}
5312
Andy Adamson557134a2009-04-01 09:21:53 -04005313#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005314/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005315 * Check the exchange flags returned by the server for invalid flags, having
5316 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5317 * DS flags set.
5318 */
5319static int nfs4_check_cl_exchange_flags(u32 flags)
5320{
5321 if (flags & ~EXCHGID4_FLAG_MASK_R)
5322 goto out_inval;
5323 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5324 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5325 goto out_inval;
5326 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5327 goto out_inval;
5328 return NFS_OK;
5329out_inval:
5330 return -NFS4ERR_INVAL;
5331}
5332
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005333static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005334nfs41_same_server_scope(struct nfs41_server_scope *a,
5335 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005336{
5337 if (a->server_scope_sz == b->server_scope_sz &&
5338 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5339 return true;
5340
5341 return false;
5342}
5343
Andy Adamson357f54d2010-12-14 10:11:57 -05005344/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005345 * nfs4_proc_bind_conn_to_session()
5346 *
5347 * The 4.1 client currently uses the same TCP connection for the
5348 * fore and backchannel.
5349 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005350int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005351{
5352 int status;
5353 struct nfs41_bind_conn_to_session_res res;
5354 struct rpc_message msg = {
5355 .rpc_proc =
5356 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5357 .rpc_argp = clp,
5358 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005359 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005360 };
5361
5362 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005363
5364 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5365 if (unlikely(res.session == NULL)) {
5366 status = -ENOMEM;
5367 goto out;
5368 }
5369
5370 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5371 if (status == 0) {
5372 if (memcmp(res.session->sess_id.data,
5373 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5374 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5375 status = -EIO;
5376 goto out_session;
5377 }
5378 if (res.dir != NFS4_CDFS4_BOTH) {
5379 dprintk("NFS: %s: Unexpected direction from server\n",
5380 __func__);
5381 status = -EIO;
5382 goto out_session;
5383 }
5384 if (res.use_conn_in_rdma_mode) {
5385 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5386 __func__);
5387 status = -EIO;
5388 goto out_session;
5389 }
5390 }
5391out_session:
5392 kfree(res.session);
5393out:
5394 dprintk("<-- %s status= %d\n", __func__, status);
5395 return status;
5396}
5397
5398/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005399 * nfs4_proc_exchange_id()
5400 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005401 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5402 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005403 * Since the clientid has expired, all compounds using sessions
5404 * associated with the stale clientid will be returning
5405 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5406 * be in some phase of session reset.
5407 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005408int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005409{
5410 nfs4_verifier verifier;
5411 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005412 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005413 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005414 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005415 };
5416 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005417 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005418 };
5419 int status;
5420 struct rpc_message msg = {
5421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5422 .rpc_argp = &args,
5423 .rpc_resp = &res,
5424 .rpc_cred = cred,
5425 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005426
Chuck Leverf0920752012-05-21 22:45:41 -04005427 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005428 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5429 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005430 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5431 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5432 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005433
Chuck Leveracdeb692012-05-21 22:46:16 -04005434 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005435 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005436 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005437 status = -ENOMEM;
5438 goto out;
5439 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005440
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005441 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005442 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005443 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005444 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005445 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005446 }
5447
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005448 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005449 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005450 status = -ENOMEM;
5451 goto out_server_scope;
5452 }
5453
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005454 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005455 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005456 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005457
Chuck Lever177313f2012-05-21 22:44:58 -04005458 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005459 clp->cl_clientid = res.clientid;
5460 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5461 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5462 clp->cl_seqid = res.seqid;
5463
Chuck Leveracdeb692012-05-21 22:46:16 -04005464 kfree(clp->cl_serverowner);
5465 clp->cl_serverowner = res.server_owner;
5466 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005467
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005468 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005469 kfree(clp->cl_implid);
5470 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005471
Chuck Lever177313f2012-05-21 22:44:58 -04005472 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005473 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005474 res.server_scope)) {
5475 dprintk("%s: server_scope mismatch detected\n",
5476 __func__);
5477 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005478 kfree(clp->cl_serverscope);
5479 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005480 }
5481
Chuck Lever177313f2012-05-21 22:44:58 -04005482 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005483 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005484 goto out;
5485 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005486 } else
5487 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005488
Chuck Leveracdeb692012-05-21 22:46:16 -04005489out_server_owner:
5490 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005491out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005492 kfree(res.server_scope);
5493out:
Chuck Lever177313f2012-05-21 22:44:58 -04005494 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005495 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005496 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005497 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005498 clp->cl_implid->date.seconds,
5499 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005500 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005501 return status;
5502}
5503
Trond Myklebust66245532012-05-25 17:18:09 -04005504static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5505 struct rpc_cred *cred)
5506{
5507 struct rpc_message msg = {
5508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5509 .rpc_argp = clp,
5510 .rpc_cred = cred,
5511 };
5512 int status;
5513
5514 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5515 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005516 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005517 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5518 return status;
5519}
5520
5521static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5522 struct rpc_cred *cred)
5523{
5524 unsigned int loop;
5525 int ret;
5526
5527 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5528 ret = _nfs4_proc_destroy_clientid(clp, cred);
5529 switch (ret) {
5530 case -NFS4ERR_DELAY:
5531 case -NFS4ERR_CLIENTID_BUSY:
5532 ssleep(1);
5533 break;
5534 default:
5535 return ret;
5536 }
5537 }
5538 return 0;
5539}
5540
5541int nfs4_destroy_clientid(struct nfs_client *clp)
5542{
5543 struct rpc_cred *cred;
5544 int ret = 0;
5545
5546 if (clp->cl_mvops->minor_version < 1)
5547 goto out;
5548 if (clp->cl_exchange_flags == 0)
5549 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005550 if (clp->cl_preserve_clid)
5551 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005552 cred = nfs4_get_exchange_id_cred(clp);
5553 ret = nfs4_proc_destroy_clientid(clp, cred);
5554 if (cred)
5555 put_rpccred(cred);
5556 switch (ret) {
5557 case 0:
5558 case -NFS4ERR_STALE_CLIENTID:
5559 clp->cl_exchange_flags = 0;
5560 }
5561out:
5562 return ret;
5563}
5564
Andy Adamson2050f0c2009-04-01 09:22:30 -04005565struct nfs4_get_lease_time_data {
5566 struct nfs4_get_lease_time_args *args;
5567 struct nfs4_get_lease_time_res *res;
5568 struct nfs_client *clp;
5569};
5570
5571static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5572 void *calldata)
5573{
5574 int ret;
5575 struct nfs4_get_lease_time_data *data =
5576 (struct nfs4_get_lease_time_data *)calldata;
5577
5578 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005579 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005580 /* just setup sequence, do not trigger session recovery
5581 since we're invoked within one */
5582 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005583 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005584 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005585
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005586 if (ret != -EAGAIN)
5587 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005588 dprintk("<-- %s\n", __func__);
5589}
5590
5591/*
5592 * Called from nfs4_state_manager thread for session setup, so don't recover
5593 * from sequence operation or clientid errors.
5594 */
5595static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5596{
5597 struct nfs4_get_lease_time_data *data =
5598 (struct nfs4_get_lease_time_data *)calldata;
5599
5600 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005601 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5602 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005603 switch (task->tk_status) {
5604 case -NFS4ERR_DELAY:
5605 case -NFS4ERR_GRACE:
5606 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5607 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5608 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005609 /* fall through */
5610 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005611 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005612 return;
5613 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005614 dprintk("<-- %s\n", __func__);
5615}
5616
Trond Myklebust17280172012-03-11 13:11:00 -04005617static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005618 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5619 .rpc_call_done = nfs4_get_lease_time_done,
5620};
5621
5622int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5623{
5624 struct rpc_task *task;
5625 struct nfs4_get_lease_time_args args;
5626 struct nfs4_get_lease_time_res res = {
5627 .lr_fsinfo = fsinfo,
5628 };
5629 struct nfs4_get_lease_time_data data = {
5630 .args = &args,
5631 .res = &res,
5632 .clp = clp,
5633 };
5634 struct rpc_message msg = {
5635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5636 .rpc_argp = &args,
5637 .rpc_resp = &res,
5638 };
5639 struct rpc_task_setup task_setup = {
5640 .rpc_client = clp->cl_rpcclient,
5641 .rpc_message = &msg,
5642 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005643 .callback_data = &data,
5644 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005645 };
5646 int status;
5647
Trond Myklebust9d12b212012-01-17 22:04:25 -05005648 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005649 dprintk("--> %s\n", __func__);
5650 task = rpc_run_task(&task_setup);
5651
5652 if (IS_ERR(task))
5653 status = PTR_ERR(task);
5654 else {
5655 status = task->tk_status;
5656 rpc_put_task(task);
5657 }
5658 dprintk("<-- %s return %d\n", __func__, status);
5659
5660 return status;
5661}
5662
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005663struct nfs4_slot *nfs4_alloc_slots(struct nfs4_slot_table *table,
5664 u32 max_slots, gfp_t gfp_flags)
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005665{
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005666 struct nfs4_slot *tbl;
5667 u32 i;
5668
5669 tbl = kmalloc_array(max_slots, sizeof(*tbl), gfp_flags);
5670 if (tbl != NULL) {
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005671 for (i = 0; i < max_slots; i++) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005672 tbl[i].table = table;
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005673 tbl[i].slot_nr = i;
5674 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005675 }
5676 return tbl;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005677}
5678
5679static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5680 struct nfs4_slot *new,
5681 u32 max_slots,
5682 u32 ivalue)
5683{
5684 struct nfs4_slot *old = NULL;
5685 u32 i;
5686
5687 spin_lock(&tbl->slot_tbl_lock);
5688 if (new) {
5689 old = tbl->slots;
5690 tbl->slots = new;
5691 tbl->max_slots = max_slots;
5692 }
Trond Myklebustf9b1ef52012-10-29 16:48:40 -04005693 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005694 for (i = 0; i < tbl->max_slots; i++)
5695 tbl->slots[i].seq_nr = ivalue;
5696 spin_unlock(&tbl->slot_tbl_lock);
5697 kfree(old);
5698}
5699
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005700/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005701 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005702 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005703static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5704 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005705{
Andy Adamson104aeba2010-01-14 17:45:10 -05005706 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005707 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005708
Andy Adamson104aeba2010-01-14 17:45:10 -05005709 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5710 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005711
Andy Adamson104aeba2010-01-14 17:45:10 -05005712 /* Does the newly negotiated max_reqs match the existing slot table? */
5713 if (max_reqs != tbl->max_slots) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005714 new = nfs4_alloc_slots(tbl, max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005715 if (!new)
5716 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005717 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005718 ret = 0;
5719
5720 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005721 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5722 tbl, tbl->slots, tbl->max_slots);
5723out:
5724 dprintk("<-- %s: return %d\n", __func__, ret);
5725 return ret;
5726}
5727
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005728/* Destroy the slot table */
5729static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5730{
5731 if (session->fc_slot_table.slots != NULL) {
5732 kfree(session->fc_slot_table.slots);
5733 session->fc_slot_table.slots = NULL;
5734 }
5735 if (session->bc_slot_table.slots != NULL) {
5736 kfree(session->bc_slot_table.slots);
5737 session->bc_slot_table.slots = NULL;
5738 }
5739 return;
5740}
5741
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005742/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005743 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005744 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005745static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005746{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005747 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005748 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005749
Andy Adamsonaacd5532011-11-09 13:58:21 -05005750 dprintk("--> %s\n", __func__);
5751 /* Fore channel */
5752 tbl = &ses->fc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005753 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005754 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5755 if (status) /* -ENOMEM */
5756 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005757 /* Back channel */
5758 tbl = &ses->bc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005759 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005760 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5761 if (status && tbl->slots == NULL)
5762 /* Fore and back channel share a connection so get
5763 * both slot tables or neither */
5764 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005765 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005766}
5767
5768struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5769{
5770 struct nfs4_session *session;
5771 struct nfs4_slot_table *tbl;
5772
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005773 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005774 if (!session)
5775 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005776
Andy Adamson557134a2009-04-01 09:21:53 -04005777 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005778 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005779 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005780 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005781 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005782
5783 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005784 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005785 spin_lock_init(&tbl->slot_tbl_lock);
5786 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005787 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005788
Trond Myklebust1055d762010-06-16 09:52:27 -04005789 session->session_state = 1<<NFS4_SESSION_INITING;
5790
Andy Adamson557134a2009-04-01 09:21:53 -04005791 session->clp = clp;
5792 return session;
5793}
5794
5795void nfs4_destroy_session(struct nfs4_session *session)
5796{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005797 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005798 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005799
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005800 cred = nfs4_get_exchange_id_cred(session->clp);
5801 nfs4_proc_destroy_session(session, cred);
5802 if (cred)
5803 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005804
5805 rcu_read_lock();
5806 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5807 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005808 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005809 __func__, xprt);
5810 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005811 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005812 kfree(session);
5813}
5814
Andy Adamsonfc931582009-04-01 09:22:31 -04005815/*
5816 * Initialize the values to be used by the client in CREATE_SESSION
5817 * If nfs4_init_session set the fore channel request and response sizes,
5818 * use them.
5819 *
5820 * Set the back channel max_resp_sz_cached to zero to force the client to
5821 * always set csa_cachethis to FALSE because the current implementation
5822 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5823 */
5824static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5825{
5826 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005827 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5828 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005829
5830 if (mxrqst_sz == 0)
5831 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5832 if (mxresp_sz == 0)
5833 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5834 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005835 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5836 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005837 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005838 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005839
5840 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005841 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005842 __func__,
5843 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005844 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005845
5846 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005847 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5848 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5849 args->bc_attrs.max_resp_sz_cached = 0;
5850 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5851 args->bc_attrs.max_reqs = 1;
5852
5853 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5854 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5855 __func__,
5856 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5857 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5858 args->bc_attrs.max_reqs);
5859}
5860
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005861static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005862{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005863 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5864 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5865
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005866 if (rcvd->max_resp_sz > sent->max_resp_sz)
5867 return -EINVAL;
5868 /*
5869 * Our requested max_ops is the minimum we need; we're not
5870 * prepared to break up compounds into smaller pieces than that.
5871 * So, no point even trying to continue if the server won't
5872 * cooperate:
5873 */
5874 if (rcvd->max_ops < sent->max_ops)
5875 return -EINVAL;
5876 if (rcvd->max_reqs == 0)
5877 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005878 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5879 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005880 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005881}
5882
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005883static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5884{
5885 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5886 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005887
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005888 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5889 return -EINVAL;
5890 if (rcvd->max_resp_sz < sent->max_resp_sz)
5891 return -EINVAL;
5892 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5893 return -EINVAL;
5894 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005895 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005896 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005897 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005898 return -EINVAL;
5899 return 0;
5900}
Andy Adamson8d353012009-04-01 09:22:32 -04005901
Andy Adamson8d353012009-04-01 09:22:32 -04005902static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5903 struct nfs4_session *session)
5904{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005905 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005906
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005907 ret = nfs4_verify_fore_channel_attrs(args, session);
5908 if (ret)
5909 return ret;
5910 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005911}
5912
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005913static int _nfs4_proc_create_session(struct nfs_client *clp,
5914 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005915{
5916 struct nfs4_session *session = clp->cl_session;
5917 struct nfs41_create_session_args args = {
5918 .client = clp,
5919 .cb_program = NFS4_CALLBACK,
5920 };
5921 struct nfs41_create_session_res res = {
5922 .client = clp,
5923 };
5924 struct rpc_message msg = {
5925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5926 .rpc_argp = &args,
5927 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005928 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005929 };
5930 int status;
5931
5932 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005933 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005934
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005935 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005936
Trond Myklebust43095d32012-11-20 11:13:12 -05005937 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005938 /* Verify the session's negotiated channel_attrs values */
5939 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005940 /* Increment the clientid slot sequence id */
5941 clp->cl_seqid++;
5942 }
5943
5944 return status;
5945}
5946
5947/*
5948 * Issues a CREATE_SESSION operation to the server.
5949 * It is the responsibility of the caller to verify the session is
5950 * expired before calling this routine.
5951 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005952int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005953{
5954 int status;
5955 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005956 struct nfs4_session *session = clp->cl_session;
5957
5958 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5959
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005960 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005961 if (status)
5962 goto out;
5963
Andy Adamsonaacd5532011-11-09 13:58:21 -05005964 /* Init or reset the session slot tables */
5965 status = nfs4_setup_session_slot_tables(session);
5966 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005967 if (status)
5968 goto out;
5969
5970 ptr = (unsigned *)&session->sess_id.data[0];
5971 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5972 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005973out:
5974 dprintk("<-- %s\n", __func__);
5975 return status;
5976}
5977
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005978/*
5979 * Issue the over-the-wire RPC DESTROY_SESSION.
5980 * The caller must serialize access to this routine.
5981 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005982int nfs4_proc_destroy_session(struct nfs4_session *session,
5983 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005984{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005985 struct rpc_message msg = {
5986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5987 .rpc_argp = session,
5988 .rpc_cred = cred,
5989 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005990 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005991
5992 dprintk("--> nfs4_proc_destroy_session\n");
5993
5994 /* session is still being setup */
5995 if (session->clp->cl_cons_state != NFS_CS_READY)
5996 return status;
5997
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005998 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005999
6000 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006001 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006002 "Session has been destroyed regardless...\n", status);
6003
6004 dprintk("<-- nfs4_proc_destroy_session\n");
6005 return status;
6006}
6007
Trond Myklebust7b38c362012-05-23 13:23:31 -04006008/*
6009 * With sessions, the client is not marked ready until after a
6010 * successful EXCHANGE_ID and CREATE_SESSION.
6011 *
6012 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6013 * other versions of NFS can be tried.
6014 */
6015static int nfs41_check_session_ready(struct nfs_client *clp)
6016{
6017 int ret;
6018
6019 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6020 ret = nfs4_client_recover_expired_lease(clp);
6021 if (ret)
6022 return ret;
6023 }
6024 if (clp->cl_cons_state < NFS_CS_READY)
6025 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006026 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006027 return 0;
6028}
6029
Trond Myklebustfccba802009-07-21 16:48:07 -04006030int nfs4_init_session(struct nfs_server *server)
6031{
6032 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006033 struct nfs4_session *session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006034 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6035 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
Trond Myklebustfccba802009-07-21 16:48:07 -04006036
6037 if (!nfs4_has_session(clp))
6038 return 0;
6039
Trond Myklebustae72ae672012-11-20 11:02:55 -05006040 if (server->rsize != 0)
6041 target_max_resp_sz = server->rsize;
6042 target_max_resp_sz += nfs41_maxread_overhead;
6043
6044 if (server->wsize != 0)
6045 target_max_rqst_sz = server->wsize;
6046 target_max_rqst_sz += nfs41_maxwrite_overhead;
6047
Trond Myklebust1055d762010-06-16 09:52:27 -04006048 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006049 spin_lock(&clp->cl_lock);
6050 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebustae72ae672012-11-20 11:02:55 -05006051 /* Initialise targets and channel attributes */
6052 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6053 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6054 session->fc_target_max_resp_sz = target_max_resp_sz;
6055 session->fc_attrs.max_resp_sz = target_max_resp_sz;
Trond Myklebust688a9022012-11-20 10:53:39 -05006056 } else {
6057 /* Just adjust the targets */
6058 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6059 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6060 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6061 }
6062 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6063 session->fc_target_max_resp_sz = target_max_resp_sz;
6064 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6065 }
Trond Myklebust7b38c362012-05-23 13:23:31 -04006066 }
6067 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006068
Trond Myklebust688a9022012-11-20 10:53:39 -05006069 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6070 nfs4_schedule_lease_recovery(clp);
6071
Trond Myklebust7b38c362012-05-23 13:23:31 -04006072 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006073}
6074
Trond Myklebust7b38c362012-05-23 13:23:31 -04006075int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006076{
6077 struct nfs4_session *session = clp->cl_session;
6078 int ret;
6079
Trond Myklebust7b38c362012-05-23 13:23:31 -04006080 spin_lock(&clp->cl_lock);
6081 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6082 /*
6083 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6084 * DS lease to be equal to the MDS lease.
6085 */
6086 clp->cl_lease_time = lease_time;
6087 clp->cl_last_renewal = jiffies;
6088 }
6089 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006090
Trond Myklebust7b38c362012-05-23 13:23:31 -04006091 ret = nfs41_check_session_ready(clp);
6092 if (ret)
6093 return ret;
6094 /* Test for the DS role */
6095 if (!is_ds_client(clp))
6096 return -ENODEV;
6097 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006098}
6099EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6100
6101
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006102/*
6103 * Renew the cl_session lease.
6104 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006105struct nfs4_sequence_data {
6106 struct nfs_client *clp;
6107 struct nfs4_sequence_args args;
6108 struct nfs4_sequence_res res;
6109};
6110
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006111static void nfs41_sequence_release(void *data)
6112{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006113 struct nfs4_sequence_data *calldata = data;
6114 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006115
Alexandros Batsakis71358402010-02-05 03:45:05 -08006116 if (atomic_read(&clp->cl_count) > 1)
6117 nfs4_schedule_state_renewal(clp);
6118 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006119 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006120}
6121
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006122static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6123{
6124 switch(task->tk_status) {
6125 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006126 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6127 return -EAGAIN;
6128 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006129 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006130 }
6131 return 0;
6132}
6133
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006134static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006135{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006136 struct nfs4_sequence_data *calldata = data;
6137 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006138
Trond Myklebust14516c32010-07-31 14:29:06 -04006139 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6140 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006141
6142 if (task->tk_status < 0) {
6143 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006144 if (atomic_read(&clp->cl_count) == 1)
6145 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006146
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006147 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6148 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006149 return;
6150 }
6151 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006152 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006153out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006154 dprintk("<-- %s\n", __func__);
6155}
6156
6157static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6158{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006159 struct nfs4_sequence_data *calldata = data;
6160 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006161 struct nfs4_sequence_args *args;
6162 struct nfs4_sequence_res *res;
6163
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006164 args = task->tk_msg.rpc_argp;
6165 res = task->tk_msg.rpc_resp;
6166
Trond Myklebust9d12b212012-01-17 22:04:25 -05006167 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006168 return;
6169 rpc_call_start(task);
6170}
6171
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006172static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6173{
6174 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6175 nfs41_sequence_prepare(task, data);
6176}
6177
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006178static const struct rpc_call_ops nfs41_sequence_ops = {
6179 .rpc_call_done = nfs41_sequence_call_done,
6180 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006181 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006182};
6183
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006184static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6185 .rpc_call_done = nfs41_sequence_call_done,
6186 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6187 .rpc_release = nfs41_sequence_release,
6188};
6189
6190static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6191 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006192{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006193 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006194 struct rpc_message msg = {
6195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6196 .rpc_cred = cred,
6197 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006198 struct rpc_task_setup task_setup_data = {
6199 .rpc_client = clp->cl_rpcclient,
6200 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006201 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006202 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6203 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006204
Alexandros Batsakis71358402010-02-05 03:45:05 -08006205 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006206 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006207 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006208 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006209 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006210 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006211 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006212 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006213 msg.rpc_argp = &calldata->args;
6214 msg.rpc_resp = &calldata->res;
6215 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006216 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006217
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006218 return rpc_run_task(&task_setup_data);
6219}
6220
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006221static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006222{
6223 struct rpc_task *task;
6224 int ret = 0;
6225
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006226 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6227 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006228 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006229 if (IS_ERR(task))
6230 ret = PTR_ERR(task);
6231 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006232 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006233 dprintk("<-- %s status=%d\n", __func__, ret);
6234 return ret;
6235}
6236
6237static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6238{
6239 struct rpc_task *task;
6240 int ret;
6241
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006242 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006243 if (IS_ERR(task)) {
6244 ret = PTR_ERR(task);
6245 goto out;
6246 }
6247 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006248 if (!ret) {
6249 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6250
6251 if (task->tk_status == 0)
6252 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006253 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006254 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006255 rpc_put_task(task);
6256out:
6257 dprintk("<-- %s status=%d\n", __func__, ret);
6258 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006259}
6260
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006261struct nfs4_reclaim_complete_data {
6262 struct nfs_client *clp;
6263 struct nfs41_reclaim_complete_args arg;
6264 struct nfs41_reclaim_complete_res res;
6265};
6266
6267static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6268{
6269 struct nfs4_reclaim_complete_data *calldata = data;
6270
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006271 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006272 if (nfs41_setup_sequence(calldata->clp->cl_session,
6273 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006274 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006275 return;
6276
6277 rpc_call_start(task);
6278}
6279
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006280static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6281{
6282 switch(task->tk_status) {
6283 case 0:
6284 case -NFS4ERR_COMPLETE_ALREADY:
6285 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6286 break;
6287 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006288 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006289 /* fall through */
6290 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006291 return -EAGAIN;
6292 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006293 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006294 }
6295 return 0;
6296}
6297
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006298static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6299{
6300 struct nfs4_reclaim_complete_data *calldata = data;
6301 struct nfs_client *clp = calldata->clp;
6302 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6303
6304 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006305 if (!nfs41_sequence_done(task, res))
6306 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006307
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006308 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6309 rpc_restart_call_prepare(task);
6310 return;
6311 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006312 dprintk("<-- %s\n", __func__);
6313}
6314
6315static void nfs4_free_reclaim_complete_data(void *data)
6316{
6317 struct nfs4_reclaim_complete_data *calldata = data;
6318
6319 kfree(calldata);
6320}
6321
6322static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6323 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6324 .rpc_call_done = nfs4_reclaim_complete_done,
6325 .rpc_release = nfs4_free_reclaim_complete_data,
6326};
6327
6328/*
6329 * Issue a global reclaim complete.
6330 */
6331static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6332{
6333 struct nfs4_reclaim_complete_data *calldata;
6334 struct rpc_task *task;
6335 struct rpc_message msg = {
6336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6337 };
6338 struct rpc_task_setup task_setup_data = {
6339 .rpc_client = clp->cl_rpcclient,
6340 .rpc_message = &msg,
6341 .callback_ops = &nfs4_reclaim_complete_call_ops,
6342 .flags = RPC_TASK_ASYNC,
6343 };
6344 int status = -ENOMEM;
6345
6346 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006347 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006348 if (calldata == NULL)
6349 goto out;
6350 calldata->clp = clp;
6351 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006352
Trond Myklebust9d12b212012-01-17 22:04:25 -05006353 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006354 msg.rpc_argp = &calldata->arg;
6355 msg.rpc_resp = &calldata->res;
6356 task_setup_data.callback_data = calldata;
6357 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006358 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006359 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006360 goto out;
6361 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006362 status = nfs4_wait_for_completion_rpc_task(task);
6363 if (status == 0)
6364 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006365 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006366 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006367out:
6368 dprintk("<-- %s status=%d\n", __func__, status);
6369 return status;
6370}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006371
6372static void
6373nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6374{
6375 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006376 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006377
6378 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006379 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6380 * right now covering the LAYOUTGET we are about to send.
6381 * However, that is not so catastrophic, and there seems
6382 * to be no way to prevent it completely.
6383 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006384 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006385 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006386 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006387 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6388 NFS_I(lgp->args.inode)->layout,
6389 lgp->args.ctx->state)) {
6390 rpc_exit(task, NFS4_OK);
6391 return;
6392 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006393 rpc_call_start(task);
6394}
6395
6396static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6397{
6398 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006399 struct inode *inode = lgp->args.inode;
6400 struct nfs_server *server = NFS_SERVER(inode);
6401 struct pnfs_layout_hdr *lo;
6402 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006403
6404 dprintk("--> %s\n", __func__);
6405
6406 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006407 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006408
6409 switch (task->tk_status) {
6410 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006411 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006412 case -NFS4ERR_LAYOUTTRYLATER:
6413 case -NFS4ERR_RECALLCONFLICT:
6414 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006415 break;
6416 case -NFS4ERR_EXPIRED:
6417 case -NFS4ERR_BAD_STATEID:
6418 spin_lock(&inode->i_lock);
6419 lo = NFS_I(inode)->layout;
6420 if (!lo || list_empty(&lo->plh_segs)) {
6421 spin_unlock(&inode->i_lock);
6422 /* If the open stateid was bad, then recover it. */
6423 state = lgp->args.ctx->state;
6424 } else {
6425 LIST_HEAD(head);
6426
6427 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6428 spin_unlock(&inode->i_lock);
6429 /* Mark the bad layout state as invalid, then
6430 * retry using the open stateid. */
6431 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006432 }
6433 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006434 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6435 rpc_restart_call_prepare(task);
6436out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006437 dprintk("<-- %s\n", __func__);
6438}
6439
Idan Kedar85541162012-08-02 11:47:10 +03006440static size_t max_response_pages(struct nfs_server *server)
6441{
6442 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6443 return nfs_page_array_len(0, max_resp_sz);
6444}
6445
6446static void nfs4_free_pages(struct page **pages, size_t size)
6447{
6448 int i;
6449
6450 if (!pages)
6451 return;
6452
6453 for (i = 0; i < size; i++) {
6454 if (!pages[i])
6455 break;
6456 __free_page(pages[i]);
6457 }
6458 kfree(pages);
6459}
6460
6461static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6462{
6463 struct page **pages;
6464 int i;
6465
6466 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6467 if (!pages) {
6468 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6469 return NULL;
6470 }
6471
6472 for (i = 0; i < size; i++) {
6473 pages[i] = alloc_page(gfp_flags);
6474 if (!pages[i]) {
6475 dprintk("%s: failed to allocate page\n", __func__);
6476 nfs4_free_pages(pages, size);
6477 return NULL;
6478 }
6479 }
6480
6481 return pages;
6482}
6483
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006484static void nfs4_layoutget_release(void *calldata)
6485{
6486 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006487 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6488 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006489
6490 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006491 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006492 put_nfs_open_context(lgp->args.ctx);
6493 kfree(calldata);
6494 dprintk("<-- %s\n", __func__);
6495}
6496
6497static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6498 .rpc_call_prepare = nfs4_layoutget_prepare,
6499 .rpc_call_done = nfs4_layoutget_done,
6500 .rpc_release = nfs4_layoutget_release,
6501};
6502
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006503struct pnfs_layout_segment *
6504nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006505{
6506 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006507 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006508 struct rpc_task *task;
6509 struct rpc_message msg = {
6510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6511 .rpc_argp = &lgp->args,
6512 .rpc_resp = &lgp->res,
6513 };
6514 struct rpc_task_setup task_setup_data = {
6515 .rpc_client = server->client,
6516 .rpc_message = &msg,
6517 .callback_ops = &nfs4_layoutget_call_ops,
6518 .callback_data = lgp,
6519 .flags = RPC_TASK_ASYNC,
6520 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006521 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006522 int status = 0;
6523
6524 dprintk("--> %s\n", __func__);
6525
Idan Kedar85541162012-08-02 11:47:10 +03006526 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6527 if (!lgp->args.layout.pages) {
6528 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006529 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006530 }
6531 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6532
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006533 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006534 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006535 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006536 task = rpc_run_task(&task_setup_data);
6537 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006538 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006539 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006540 if (status == 0)
6541 status = task->tk_status;
6542 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006543 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006544 rpc_put_task(task);
6545 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006546 if (status)
6547 return ERR_PTR(status);
6548 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006549}
6550
Benny Halevycbe82602011-05-22 19:52:37 +03006551static void
6552nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6553{
6554 struct nfs4_layoutreturn *lrp = calldata;
6555
6556 dprintk("--> %s\n", __func__);
6557 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006558 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006559 return;
6560 rpc_call_start(task);
6561}
6562
6563static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6564{
6565 struct nfs4_layoutreturn *lrp = calldata;
6566 struct nfs_server *server;
6567
6568 dprintk("--> %s\n", __func__);
6569
6570 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6571 return;
6572
6573 server = NFS_SERVER(lrp->args.inode);
6574 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006575 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006576 return;
6577 }
Benny Halevycbe82602011-05-22 19:52:37 +03006578 dprintk("<-- %s\n", __func__);
6579}
6580
6581static void nfs4_layoutreturn_release(void *calldata)
6582{
6583 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006584 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006585
6586 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006587 spin_lock(&lo->plh_inode->i_lock);
6588 if (lrp->res.lrs_present)
6589 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6590 lo->plh_block_lgets--;
6591 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006592 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006593 kfree(calldata);
6594 dprintk("<-- %s\n", __func__);
6595}
6596
6597static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6598 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6599 .rpc_call_done = nfs4_layoutreturn_done,
6600 .rpc_release = nfs4_layoutreturn_release,
6601};
6602
6603int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6604{
6605 struct rpc_task *task;
6606 struct rpc_message msg = {
6607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6608 .rpc_argp = &lrp->args,
6609 .rpc_resp = &lrp->res,
6610 };
6611 struct rpc_task_setup task_setup_data = {
6612 .rpc_client = lrp->clp->cl_rpcclient,
6613 .rpc_message = &msg,
6614 .callback_ops = &nfs4_layoutreturn_call_ops,
6615 .callback_data = lrp,
6616 };
6617 int status;
6618
6619 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006620 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006621 task = rpc_run_task(&task_setup_data);
6622 if (IS_ERR(task))
6623 return PTR_ERR(task);
6624 status = task->tk_status;
6625 dprintk("<-- %s status=%d\n", __func__, status);
6626 rpc_put_task(task);
6627 return status;
6628}
6629
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006630/*
6631 * Retrieve the list of Data Server devices from the MDS.
6632 */
6633static int _nfs4_getdevicelist(struct nfs_server *server,
6634 const struct nfs_fh *fh,
6635 struct pnfs_devicelist *devlist)
6636{
6637 struct nfs4_getdevicelist_args args = {
6638 .fh = fh,
6639 .layoutclass = server->pnfs_curr_ld->id,
6640 };
6641 struct nfs4_getdevicelist_res res = {
6642 .devlist = devlist,
6643 };
6644 struct rpc_message msg = {
6645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6646 .rpc_argp = &args,
6647 .rpc_resp = &res,
6648 };
6649 int status;
6650
6651 dprintk("--> %s\n", __func__);
6652 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6653 &res.seq_res, 0);
6654 dprintk("<-- %s status=%d\n", __func__, status);
6655 return status;
6656}
6657
6658int nfs4_proc_getdevicelist(struct nfs_server *server,
6659 const struct nfs_fh *fh,
6660 struct pnfs_devicelist *devlist)
6661{
6662 struct nfs4_exception exception = { };
6663 int err;
6664
6665 do {
6666 err = nfs4_handle_exception(server,
6667 _nfs4_getdevicelist(server, fh, devlist),
6668 &exception);
6669 } while (exception.retry);
6670
6671 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6672 err, devlist->num_devs);
6673
6674 return err;
6675}
6676EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6677
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006678static int
6679_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6680{
6681 struct nfs4_getdeviceinfo_args args = {
6682 .pdev = pdev,
6683 };
6684 struct nfs4_getdeviceinfo_res res = {
6685 .pdev = pdev,
6686 };
6687 struct rpc_message msg = {
6688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6689 .rpc_argp = &args,
6690 .rpc_resp = &res,
6691 };
6692 int status;
6693
6694 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006695 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006696 dprintk("<-- %s status=%d\n", __func__, status);
6697
6698 return status;
6699}
6700
6701int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6702{
6703 struct nfs4_exception exception = { };
6704 int err;
6705
6706 do {
6707 err = nfs4_handle_exception(server,
6708 _nfs4_proc_getdeviceinfo(server, pdev),
6709 &exception);
6710 } while (exception.retry);
6711 return err;
6712}
6713EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6714
Andy Adamson863a3c62011-03-23 13:27:54 +00006715static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6716{
6717 struct nfs4_layoutcommit_data *data = calldata;
6718 struct nfs_server *server = NFS_SERVER(data->args.inode);
6719
6720 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006721 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006722 return;
6723 rpc_call_start(task);
6724}
6725
6726static void
6727nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6728{
6729 struct nfs4_layoutcommit_data *data = calldata;
6730 struct nfs_server *server = NFS_SERVER(data->args.inode);
6731
6732 if (!nfs4_sequence_done(task, &data->res.seq_res))
6733 return;
6734
6735 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006736 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6737 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6738 case -NFS4ERR_BADLAYOUT: /* no layout */
6739 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006740 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006741 break;
6742 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006743 nfs_post_op_update_inode_force_wcc(data->args.inode,
6744 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006745 break;
6746 default:
6747 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6748 rpc_restart_call_prepare(task);
6749 return;
6750 }
6751 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006752}
6753
6754static void nfs4_layoutcommit_release(void *calldata)
6755{
6756 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006757 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006758 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006759
Andy Adamsondb29c082011-07-30 20:52:38 -04006760 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006761 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006762 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6763 list_del_init(&lseg->pls_lc_list);
6764 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6765 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006766 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006767 }
Peng Tao92407e72011-10-23 20:21:17 -07006768
6769 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6770 smp_mb__after_clear_bit();
6771 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6772
Andy Adamson863a3c62011-03-23 13:27:54 +00006773 put_rpccred(data->cred);
6774 kfree(data);
6775}
6776
6777static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6778 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6779 .rpc_call_done = nfs4_layoutcommit_done,
6780 .rpc_release = nfs4_layoutcommit_release,
6781};
6782
6783int
Andy Adamsonef311532011-03-12 02:58:10 -05006784nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006785{
6786 struct rpc_message msg = {
6787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6788 .rpc_argp = &data->args,
6789 .rpc_resp = &data->res,
6790 .rpc_cred = data->cred,
6791 };
6792 struct rpc_task_setup task_setup_data = {
6793 .task = &data->task,
6794 .rpc_client = NFS_CLIENT(data->args.inode),
6795 .rpc_message = &msg,
6796 .callback_ops = &nfs4_layoutcommit_ops,
6797 .callback_data = data,
6798 .flags = RPC_TASK_ASYNC,
6799 };
6800 struct rpc_task *task;
6801 int status = 0;
6802
6803 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6804 "lbw: %llu inode %lu\n",
6805 data->task.tk_pid, sync,
6806 data->args.lastbytewritten,
6807 data->args.inode->i_ino);
6808
Trond Myklebust9d12b212012-01-17 22:04:25 -05006809 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006810 task = rpc_run_task(&task_setup_data);
6811 if (IS_ERR(task))
6812 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006813 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006814 goto out;
6815 status = nfs4_wait_for_completion_rpc_task(task);
6816 if (status != 0)
6817 goto out;
6818 status = task->tk_status;
6819out:
6820 dprintk("%s: status %d\n", __func__, status);
6821 rpc_put_task(task);
6822 return status;
6823}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006824
6825static int
6826_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6827 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6828{
6829 struct nfs41_secinfo_no_name_args args = {
6830 .style = SECINFO_STYLE_CURRENT_FH,
6831 };
6832 struct nfs4_secinfo_res res = {
6833 .flavors = flavors,
6834 };
6835 struct rpc_message msg = {
6836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6837 .rpc_argp = &args,
6838 .rpc_resp = &res,
6839 };
6840 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6841}
6842
6843static int
6844nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6845 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6846{
6847 struct nfs4_exception exception = { };
6848 int err;
6849 do {
6850 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6851 switch (err) {
6852 case 0:
6853 case -NFS4ERR_WRONGSEC:
6854 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006855 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006856 default:
6857 err = nfs4_handle_exception(server, err, &exception);
6858 }
6859 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006860out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006861 return err;
6862}
6863
6864static int
6865nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6866 struct nfs_fsinfo *info)
6867{
6868 int err;
6869 struct page *page;
6870 rpc_authflavor_t flavor;
6871 struct nfs4_secinfo_flavors *flavors;
6872
6873 page = alloc_page(GFP_KERNEL);
6874 if (!page) {
6875 err = -ENOMEM;
6876 goto out;
6877 }
6878
6879 flavors = page_address(page);
6880 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6881
6882 /*
6883 * Fall back on "guess and check" method if
6884 * the server doesn't support SECINFO_NO_NAME
6885 */
6886 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6887 err = nfs4_find_root_sec(server, fhandle, info);
6888 goto out_freepage;
6889 }
6890 if (err)
6891 goto out_freepage;
6892
6893 flavor = nfs_find_best_sec(flavors);
6894 if (err == 0)
6895 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6896
6897out_freepage:
6898 put_page(page);
6899 if (err == -EACCES)
6900 return -EPERM;
6901out:
6902 return err;
6903}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006904
6905static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006906{
6907 int status;
6908 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006909 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006910 };
6911 struct nfs41_test_stateid_res res;
6912 struct rpc_message msg = {
6913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6914 .rpc_argp = &args,
6915 .rpc_resp = &res,
6916 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006917
Chuck Lever38527b12012-07-11 16:30:23 -04006918 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006919 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006920 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006921 if (status != NFS_OK) {
6922 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006923 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006924 }
6925 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006926 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006927}
6928
Chuck Lever38527b12012-07-11 16:30:23 -04006929/**
6930 * nfs41_test_stateid - perform a TEST_STATEID operation
6931 *
6932 * @server: server / transport on which to perform the operation
6933 * @stateid: state ID to test
6934 *
6935 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6936 * Otherwise a negative NFS4ERR value is returned if the operation
6937 * failed or the state ID is not currently valid.
6938 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006939static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006940{
6941 struct nfs4_exception exception = { };
6942 int err;
6943 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006944 err = _nfs41_test_stateid(server, stateid);
6945 if (err != -NFS4ERR_DELAY)
6946 break;
6947 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006948 } while (exception.retry);
6949 return err;
6950}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006951
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006952static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006953{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006954 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006955 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006956 };
6957 struct nfs41_free_stateid_res res;
6958 struct rpc_message msg = {
6959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6960 .rpc_argp = &args,
6961 .rpc_resp = &res,
6962 };
Chuck Lever38527b12012-07-11 16:30:23 -04006963 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006964
Chuck Lever38527b12012-07-11 16:30:23 -04006965 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006966 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04006967 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04006968 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006969 dprintk("NFS reply free_stateid: %d\n", status);
6970 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006971}
6972
Chuck Lever38527b12012-07-11 16:30:23 -04006973/**
6974 * nfs41_free_stateid - perform a FREE_STATEID operation
6975 *
6976 * @server: server / transport on which to perform the operation
6977 * @stateid: state ID to release
6978 *
6979 * Returns NFS_OK if the server freed "stateid". Otherwise a
6980 * negative NFS4ERR value is returned.
6981 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006982static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006983{
6984 struct nfs4_exception exception = { };
6985 int err;
6986 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006987 err = _nfs4_free_stateid(server, stateid);
6988 if (err != -NFS4ERR_DELAY)
6989 break;
6990 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006991 } while (exception.retry);
6992 return err;
6993}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006994
6995static bool nfs41_match_stateid(const nfs4_stateid *s1,
6996 const nfs4_stateid *s2)
6997{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006998 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006999 return false;
7000
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007001 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007002 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007003 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007004 return true;
7005
7006 return false;
7007}
7008
Andy Adamson557134a2009-04-01 09:21:53 -04007009#endif /* CONFIG_NFS_V4_1 */
7010
Trond Myklebust36281ca2012-03-04 18:13:56 -05007011static bool nfs4_match_stateid(const nfs4_stateid *s1,
7012 const nfs4_stateid *s2)
7013{
Trond Myklebustf597c532012-03-04 18:13:56 -05007014 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007015}
7016
7017
Trond Myklebust17280172012-03-11 13:11:00 -04007018static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007019 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007020 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 .recover_open = nfs4_open_reclaim,
7022 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007023 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007024 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007025 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026};
7027
Andy Adamson591d71c2009-04-01 09:22:47 -04007028#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007029static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007030 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7031 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7032 .recover_open = nfs4_open_reclaim,
7033 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007034 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007035 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007036 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007037 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007038};
7039#endif /* CONFIG_NFS_V4_1 */
7040
Trond Myklebust17280172012-03-11 13:11:00 -04007041static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007042 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007043 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 .recover_open = nfs4_open_expired,
7045 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007046 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007047 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048};
7049
Andy Adamson591d71c2009-04-01 09:22:47 -04007050#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007051static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007052 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7053 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007054 .recover_open = nfs41_open_expired,
7055 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007056 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007057 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007058};
7059#endif /* CONFIG_NFS_V4_1 */
7060
Trond Myklebust17280172012-03-11 13:11:00 -04007061static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007062 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007063 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007064 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007065};
7066
7067#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007068static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007069 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007070 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007071 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007072};
7073#endif
7074
Trond Myklebust97dc1352010-06-16 09:52:26 -04007075static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7076 .minor_version = 0,
7077 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007078 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007079 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007080 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7081 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7082 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007083};
7084
7085#if defined(CONFIG_NFS_V4_1)
7086static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7087 .minor_version = 1,
7088 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007089 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007090 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007091 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7092 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7093 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007094};
7095#endif
7096
Trond Myklebust97dc1352010-06-16 09:52:26 -04007097const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7098 [0] = &nfs_v4_0_minor_ops,
7099#if defined(CONFIG_NFS_V4_1)
7100 [1] = &nfs_v4_1_minor_ops,
7101#endif
7102};
7103
Bryan Schumaker73a79702012-07-16 16:39:12 -04007104const struct inode_operations nfs4_dir_inode_operations = {
7105 .create = nfs_create,
7106 .lookup = nfs_lookup,
7107 .atomic_open = nfs_atomic_open,
7108 .link = nfs_link,
7109 .unlink = nfs_unlink,
7110 .symlink = nfs_symlink,
7111 .mkdir = nfs_mkdir,
7112 .rmdir = nfs_rmdir,
7113 .mknod = nfs_mknod,
7114 .rename = nfs_rename,
7115 .permission = nfs_permission,
7116 .getattr = nfs_getattr,
7117 .setattr = nfs_setattr,
7118 .getxattr = generic_getxattr,
7119 .setxattr = generic_setxattr,
7120 .listxattr = generic_listxattr,
7121 .removexattr = generic_removexattr,
7122};
7123
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007124static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007125 .permission = nfs_permission,
7126 .getattr = nfs_getattr,
7127 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007128 .getxattr = generic_getxattr,
7129 .setxattr = generic_setxattr,
7130 .listxattr = generic_listxattr,
7131 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007132};
7133
David Howells509de812006-08-22 20:06:11 -04007134const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 .version = 4, /* protocol version */
7136 .dentry_ops = &nfs4_dentry_operations,
7137 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007138 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007139 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007141 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007142 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 .getattr = nfs4_proc_getattr,
7144 .setattr = nfs4_proc_setattr,
7145 .lookup = nfs4_proc_lookup,
7146 .access = nfs4_proc_access,
7147 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 .create = nfs4_proc_create,
7149 .remove = nfs4_proc_remove,
7150 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007151 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 .unlink_done = nfs4_proc_unlink_done,
7153 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007154 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007155 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007156 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 .link = nfs4_proc_link,
7158 .symlink = nfs4_proc_symlink,
7159 .mkdir = nfs4_proc_mkdir,
7160 .rmdir = nfs4_proc_remove,
7161 .readdir = nfs4_proc_readdir,
7162 .mknod = nfs4_proc_mknod,
7163 .statfs = nfs4_proc_statfs,
7164 .fsinfo = nfs4_proc_fsinfo,
7165 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007166 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 .decode_dirent = nfs4_decode_dirent,
7168 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007169 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007170 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007171 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007173 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007174 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007175 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007177 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007178 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007180 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007181 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007182 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007183 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007184 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007185 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007186 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007187 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007188 .create_server = nfs4_create_server,
7189 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190};
7191
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007192static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7193 .prefix = XATTR_NAME_NFSV4_ACL,
7194 .list = nfs4_xattr_list_nfs4_acl,
7195 .get = nfs4_xattr_get_nfs4_acl,
7196 .set = nfs4_xattr_set_nfs4_acl,
7197};
7198
7199const struct xattr_handler *nfs4_xattr_handlers[] = {
7200 &nfs4_xattr_nfs4_acl_handler,
7201 NULL
7202};
7203
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204/*
7205 * Local variables:
7206 * c-basic-offset: 8
7207 * End:
7208 */