blob: 1792ece8b53ce878a5497e4d218b3c0af7f6a4d4 [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 Myklebustf4af6e2a2012-11-20 14:17:32 -0500415nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400416{
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500417 u32 slotid = slot->slot_nr;
418
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400419 /* clear used bit in bitmap */
420 __clear_bit(slotid, tbl->used_slots);
421
422 /* update highest_used_slotid when it is freed */
423 if (slotid == tbl->highest_used_slotid) {
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500424 u32 new_max = find_last_bit(tbl->used_slots, slotid);
425 if (new_max < slotid)
426 tbl->highest_used_slotid = new_max;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400427 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500428 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400429 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500430 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
431 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400432}
433
Trond Myklebust961a8282012-01-17 22:57:37 -0500434bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
435{
436 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
437 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400438}
439
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800440/*
Andy Adamson42acd022011-01-06 02:04:34 +0000441 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800442 */
Andy Adamson42acd022011-01-06 02:04:34 +0000443static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800444{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400445 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500446 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
447 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800448 return;
449 }
450
Trond Myklebust45d43c22012-02-06 19:38:51 -0500451 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800452 return;
453
Andy Adamson42acd022011-01-06 02:04:34 +0000454 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
455 complete(&ses->fc_slot_table.complete);
456}
457
458/*
459 * Signal state manager thread if session back channel is drained
460 */
461void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
462{
463 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500464 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000465 return;
466 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
467 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800468}
469
Trond Myklebustd185a332010-06-16 09:52:25 -0400470static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400471{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500472 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400473 struct nfs4_slot_table *tbl;
474
Benny Halevydfb4f3092010-09-24 09:17:01 -0400475 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400476 /* just wake up the next guy waiting since
477 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500478 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500479 return;
Andy Adamson13615872009-04-01 09:22:17 -0400480 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500481 tbl = res->sr_slot->table;
482 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500483
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500484 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500485 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500486 nfs4_check_drain_fc_complete(session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500487 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400488 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400489}
490
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500491/* Update the client's idea of target_highest_slotid */
492static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
493 u32 target_highest_slotid)
494{
Trond Myklebust97e548a2012-11-20 14:45:48 -0500495 unsigned int max_slotid, i;
496
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500497 if (tbl->target_highest_slotid == target_highest_slotid)
498 return;
499 tbl->target_highest_slotid = target_highest_slotid;
500 tbl->generation++;
Trond Myklebust97e548a2012-11-20 14:45:48 -0500501
502 max_slotid = min(tbl->max_slots - 1, tbl->target_highest_slotid);
503 for (i = tbl->max_slotid + 1; i <= max_slotid; i++)
504 rpc_wake_up_next(&tbl->slot_tbl_waitq);
505 tbl->max_slotid = max_slotid;
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500506}
507
Trond Myklebustce008c42012-11-20 15:16:30 -0500508void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
509 u32 target_highest_slotid)
510{
511 spin_lock(&tbl->slot_tbl_lock);
512 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
513 spin_unlock(&tbl->slot_tbl_lock);
514}
515
Trond Myklebustda0507b2012-11-20 18:10:30 -0500516static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
517 u32 highest_slotid)
518{
519 unsigned int max_slotid, i;
520
521 if (tbl->server_highest_slotid == highest_slotid)
522 return;
523 if (tbl->highest_used_slotid > highest_slotid)
524 return;
525 max_slotid = min(tbl->max_slots - 1, highest_slotid);
526 /* Reset the seq_nr for deallocated slots */
527 for (i = tbl->server_highest_slotid + 1; i <= max_slotid; i++)
528 tbl->slots[i].seq_nr = 1;
529 tbl->server_highest_slotid = highest_slotid;
530}
531
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500532static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
533 struct nfs4_slot *slot,
534 struct nfs4_sequence_res *res)
535{
536 spin_lock(&tbl->slot_tbl_lock);
537 if (tbl->generation != slot->generation)
538 goto out;
Trond Myklebustda0507b2012-11-20 18:10:30 -0500539 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500540 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
541out:
542 spin_unlock(&tbl->slot_tbl_lock);
543}
544
Trond Myklebust14516c32010-07-31 14:29:06 -0400545static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400546{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500547 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500548 struct nfs4_slot *slot;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400549 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400550 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400551
552 /*
553 * sr_status remains 1 if an RPC level error occurred. The server
554 * may or may not have processed the sequence operation..
555 * Proceed as if the server received and processed the sequence
556 * operation.
557 */
558 if (res->sr_status == 1)
559 res->sr_status = NFS_OK;
560
Bryan Schumaker468f8612011-04-18 15:57:32 -0400561 /* don't increment the sequence number if the task wasn't sent */
562 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400563 goto out;
564
Trond Myklebust933602e2012-11-16 12:12:38 -0500565 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500566 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500567
Andy Adamson691daf32009-12-04 15:55:39 -0500568 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400569 switch (res->sr_status) {
570 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400571 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500572 ++slot->seq_nr;
573 timestamp = slot->renewal_time;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500574 clp = session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400575 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500576 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500577 if (res->sr_status_flags != 0)
578 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500579 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400580 break;
581 case -NFS4ERR_DELAY:
582 /* The server detected a resend of the RPC call and
583 * returned NFS4ERR_DELAY as per Section 2.10.6.2
584 * of RFC5661.
585 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500586 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400587 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500588 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500589 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400590 goto out_retry;
591 default:
592 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500593 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400594 }
595out:
596 /* The session may be reset by one of the error handlers. */
597 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400598 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400599 return 1;
600out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400601 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400602 goto out;
603 rpc_delay(task, NFS4_POLL_RETRY_MAX);
604 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400605}
606
Trond Myklebust14516c32010-07-31 14:29:06 -0400607static int nfs4_sequence_done(struct rpc_task *task,
608 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400609{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500610 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400611 return 1;
612 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400613}
614
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400615/*
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500616 * nfs4_alloc_slot - efficiently look for a free slot
Benny Halevy510b8172009-04-01 09:22:14 -0400617 *
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500618 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
Benny Halevy510b8172009-04-01 09:22:14 -0400619 * If found, we mark the slot as used, update the highest_used_slotid,
620 * and respectively set up the sequence operation args.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400621 *
622 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400623 */
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500624static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400625{
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500626 struct nfs4_slot *ret = NULL;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500627 u32 slotid;
Benny Halevy510b8172009-04-01 09:22:14 -0400628
Trond Myklebust45d43c22012-02-06 19:38:51 -0500629 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400630 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
631 tbl->max_slots);
Trond Myklebust97e548a2012-11-20 14:45:48 -0500632 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
633 if (slotid > tbl->max_slotid)
Benny Halevy510b8172009-04-01 09:22:14 -0400634 goto out;
635 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500636 if (slotid > tbl->highest_used_slotid ||
637 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400638 tbl->highest_used_slotid = slotid;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500639 ret = &tbl->slots[slotid];
640 ret->renewal_time = jiffies;
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500641 ret->generation = tbl->generation;
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500642
Benny Halevy510b8172009-04-01 09:22:14 -0400643out:
644 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500645 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
646 ret ? ret->slot_nr : -1);
647 return ret;
Benny Halevy510b8172009-04-01 09:22:14 -0400648}
649
Trond Myklebust9d12b212012-01-17 22:04:25 -0500650static void nfs41_init_sequence(struct nfs4_sequence_args *args,
651 struct nfs4_sequence_res *res, int cache_reply)
652{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500653 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500654 args->sa_cache_this = 0;
655 if (cache_reply)
656 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500657 res->sr_slot = NULL;
658}
659
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000660int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 struct nfs4_sequence_args *args,
662 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663 struct rpc_task *task)
664{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400665 struct nfs4_slot *slot;
666 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400667
668 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400670 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671 return 0;
672
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400673 tbl = &session->fc_slot_table;
674
675 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400676 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800677 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500678 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500679 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400680 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500681 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500682 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400683 }
684
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800685 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
686 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
687 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
688 spin_unlock(&tbl->slot_tbl_lock);
689 dprintk("%s enforce FIFO order\n", __func__);
690 return -EAGAIN;
691 }
692
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500693 slot = nfs4_alloc_slot(tbl);
694 if (slot == NULL) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400695 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
696 spin_unlock(&tbl->slot_tbl_lock);
697 dprintk("<-- %s: no free slots\n", __func__);
698 return -EAGAIN;
699 }
700 spin_unlock(&tbl->slot_tbl_lock);
701
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800702 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500703
704 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400705
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500706 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
707 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400708
Benny Halevydfb4f3092010-09-24 09:17:01 -0400709 res->sr_slot = slot;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400710 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400711 /*
712 * sr_status is only set in decode_sequence, and so will remain
713 * set to 1 if an rpc level failure occurs.
714 */
715 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400716 return 0;
717}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000718EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400719
Trond Myklebust035168a2010-06-16 09:52:26 -0400720int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400721 struct nfs4_sequence_args *args,
722 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400723 struct rpc_task *task)
724{
Trond Myklebust035168a2010-06-16 09:52:26 -0400725 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400726 int ret = 0;
727
Trond Myklebust9d12b212012-01-17 22:04:25 -0500728 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400729 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400730
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500731 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400732 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500733 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400734
Trond Myklebust9d12b212012-01-17 22:04:25 -0500735 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400736out:
737 dprintk("<-- %s status=%d\n", __func__, ret);
738 return ret;
739}
740
741struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400742 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400743 struct nfs4_sequence_args *seq_args;
744 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400745};
746
747static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
748{
749 struct nfs41_call_sync_data *data = calldata;
750
Trond Myklebust035168a2010-06-16 09:52:26 -0400751 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
752
753 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500754 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400755 return;
756 rpc_call_start(task);
757}
758
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800759static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
760{
761 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
762 nfs41_call_sync_prepare(task, calldata);
763}
764
Andy Adamson69ab40c2009-04-01 09:22:19 -0400765static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
766{
767 struct nfs41_call_sync_data *data = calldata;
768
Trond Myklebust14516c32010-07-31 14:29:06 -0400769 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400770}
771
Trond Myklebust17280172012-03-11 13:11:00 -0400772static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400773 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400774 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400775};
776
Trond Myklebust17280172012-03-11 13:11:00 -0400777static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800778 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
779 .rpc_call_done = nfs41_call_sync_done,
780};
781
Bryan Schumaker7c513052011-03-24 17:12:24 +0000782static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
783 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400784 struct rpc_message *msg,
785 struct nfs4_sequence_args *args,
786 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800787 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400788{
789 int ret;
790 struct rpc_task *task;
791 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400792 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400793 .seq_args = args,
794 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400795 };
796 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000797 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400798 .rpc_message = msg,
799 .callback_ops = &nfs41_call_sync_ops,
800 .callback_data = &data
801 };
802
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800803 if (privileged)
804 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400805 task = rpc_run_task(&task_setup);
806 if (IS_ERR(task))
807 ret = PTR_ERR(task);
808 else {
809 ret = task->tk_status;
810 rpc_put_task(task);
811 }
812 return ret;
813}
814
Bryan Schumaker7c513052011-03-24 17:12:24 +0000815int _nfs4_call_sync_session(struct rpc_clnt *clnt,
816 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400817 struct rpc_message *msg,
818 struct nfs4_sequence_args *args,
819 struct nfs4_sequence_res *res,
820 int cache_reply)
821{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500822 nfs41_init_sequence(args, res, cache_reply);
823 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400824}
825
Trond Myklebustdf896452010-06-16 09:52:26 -0400826#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500827static inline
828void nfs41_init_sequence(struct nfs4_sequence_args *args,
829 struct nfs4_sequence_res *res, int cache_reply)
830{
831}
832
Trond Myklebust14516c32010-07-31 14:29:06 -0400833static int nfs4_sequence_done(struct rpc_task *task,
834 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400835{
Trond Myklebust14516c32010-07-31 14:29:06 -0400836 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400837}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400838#endif /* CONFIG_NFS_V4_1 */
839
Bryan Schumaker7c513052011-03-24 17:12:24 +0000840int _nfs4_call_sync(struct rpc_clnt *clnt,
841 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400842 struct rpc_message *msg,
843 struct nfs4_sequence_args *args,
844 struct nfs4_sequence_res *res,
845 int cache_reply)
846{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500847 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000848 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400849}
850
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000851static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000852int nfs4_call_sync(struct rpc_clnt *clnt,
853 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000854 struct rpc_message *msg,
855 struct nfs4_sequence_args *args,
856 struct nfs4_sequence_res *res,
857 int cache_reply)
858{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000859 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
860 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000861}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400862
Trond Myklebust38478b22006-05-25 01:40:57 -0400863static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Trond Myklebust38478b22006-05-25 01:40:57 -0400865 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Trond Myklebust38478b22006-05-25 01:40:57 -0400867 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400868 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700869 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400870 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700871 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400872 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400876 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100877 struct nfs_openargs o_arg;
878 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100879 struct nfs_open_confirmargs c_arg;
880 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500881 struct nfs4_string owner_name;
882 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100883 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100884 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400885 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400887 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100888 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100889 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400890 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100891 int rpc_status;
892 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893};
894
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400895
896static void nfs4_init_opendata_res(struct nfs4_opendata *p)
897{
898 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400899 p->o_res.seqid = p->o_arg.seqid;
900 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400901 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400902 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400903 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500904 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400905}
906
Al Viro82a2c1b2011-06-22 18:30:55 -0400907static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400909 const struct iattr *attrs,
910 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100911{
Al Viro82a2c1b2011-06-22 18:30:55 -0400912 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100913 struct inode *dir = parent->d_inode;
914 struct nfs_server *server = NFS_SERVER(dir);
915 struct nfs4_opendata *p;
916
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400917 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100918 if (p == NULL)
919 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400920 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100921 if (p->o_arg.seqid == NULL)
922 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400923 nfs_sb_active(dentry->d_sb);
924 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100925 p->dir = parent;
926 p->owner = sp;
927 atomic_inc(&sp->so_count);
928 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929 p->o_arg.open_flags = flags;
930 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700931 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
932 * will return permission denied for all bits until close */
933 if (!(flags & O_EXCL)) {
934 /* ask server to check for all possible rights as results
935 * are cached */
936 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
937 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700938 }
David Howells7539bba2006-08-22 20:06:09 -0400939 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400940 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
941 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400942 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100943 p->o_arg.server = server;
944 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400945 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100946 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500947 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500948 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400949
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100950 p->o_arg.u.attrs = &p->attrs;
951 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500952
953 verf[0] = jiffies;
954 verf[1] = current->pid;
955 memcpy(p->o_arg.u.verifier.data, verf,
956 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100957 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100958 p->c_arg.fh = &p->o_res.fh;
959 p->c_arg.stateid = &p->o_res.stateid;
960 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400961 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400962 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100963 return p;
964err_free:
965 kfree(p);
966err:
967 dput(parent);
968 return NULL;
969}
970
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400971static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100972{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400973 struct nfs4_opendata *p = container_of(kref,
974 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400975 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400976
977 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400978 if (p->state != NULL)
979 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400980 nfs4_put_state_owner(p->owner);
981 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400982 dput(p->dentry);
983 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500984 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400985 kfree(p);
986}
987
988static void nfs4_opendata_put(struct nfs4_opendata *p)
989{
990 if (p != NULL)
991 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100992}
993
Trond Myklebust06f814a2006-01-03 09:55:07 +0100994static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
995{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100996 int ret;
997
Trond Myklebust06f814a2006-01-03 09:55:07 +0100998 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100999 return ret;
1000}
1001
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001003{
1004 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005
Trond Myklebust536e43d2012-01-17 22:04:26 -05001006 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 goto out;
1008 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001009 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001010 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1011 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001012 break;
1013 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001014 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1015 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001016 break;
1017 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001018 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1019 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001020 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001022 return ret;
1023}
1024
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001026{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001027 if (delegation == NULL)
1028 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001031 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001032 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001033 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001034 return 1;
1035}
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001038{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001040 case FMODE_WRITE:
1041 state->n_wronly++;
1042 break;
1043 case FMODE_READ:
1044 state->n_rdonly++;
1045 break;
1046 case FMODE_READ|FMODE_WRITE:
1047 state->n_rdwr++;
1048 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001049 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001050}
1051
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001053{
1054 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001055 nfs4_stateid_copy(&state->stateid, stateid);
1056 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001057 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001058 case FMODE_READ:
1059 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1060 break;
1061 case FMODE_WRITE:
1062 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1063 break;
1064 case FMODE_READ|FMODE_WRITE:
1065 set_bit(NFS_O_RDWR_STATE, &state->flags);
1066 }
1067}
1068
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001069static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001070{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001071 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001072 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001073 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001074}
1075
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076static 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 -07001077{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001078 /*
1079 * Protect the call to nfs4_state_set_mode_locked and
1080 * serialise the stateid update
1081 */
1082 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001083 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001084 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001085 set_bit(NFS_DELEGATED_STATE, &state->flags);
1086 }
1087 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001089 write_sequnlock(&state->seqlock);
1090 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001091 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001092 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095static 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 -05001096{
1097 struct nfs_inode *nfsi = NFS_I(state->inode);
1098 struct nfs_delegation *deleg_cur;
1099 int ret = 0;
1100
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001101 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001102
1103 rcu_read_lock();
1104 deleg_cur = rcu_dereference(nfsi->delegation);
1105 if (deleg_cur == NULL)
1106 goto no_delegation;
1107
1108 spin_lock(&deleg_cur->lock);
1109 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001111 goto no_delegation_unlock;
1112
1113 if (delegation == NULL)
1114 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001115 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001116 goto no_delegation_unlock;
1117
Trond Myklebustb7391f42008-12-23 15:21:52 -05001118 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001119 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001120 ret = 1;
1121no_delegation_unlock:
1122 spin_unlock(&deleg_cur->lock);
1123no_delegation:
1124 rcu_read_unlock();
1125
1126 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001127 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001128 ret = 1;
1129 }
1130
1131 return ret;
1132}
1133
1134
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001136{
1137 struct nfs_delegation *delegation;
1138
1139 rcu_read_lock();
1140 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001142 rcu_read_unlock();
1143 return;
1144 }
1145 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001146 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001147}
1148
Trond Myklebust6ee41262007-07-08 14:11:36 -04001149static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001150{
1151 struct nfs4_state *state = opendata->state;
1152 struct nfs_inode *nfsi = NFS_I(state->inode);
1153 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001154 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001156 nfs4_stateid stateid;
1157 int ret = -EAGAIN;
1158
Trond Myklebustaac00a82007-07-05 19:02:21 -04001159 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001160 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001161 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001162 if (can_open_cached(state, fmode, open_mode)) {
1163 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001164 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001165 goto out_return_state;
1166 }
1167 spin_unlock(&state->owner->so_lock);
1168 }
Trond Myklebust34310432008-12-23 15:21:38 -05001169 rcu_read_lock();
1170 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001171 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001172 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001173 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001174 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001175 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001176 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001177 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001178 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001179 if (ret != 0)
1180 goto out;
1181 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001182
1183 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001184 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001185 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001186 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001187out:
1188 return ERR_PTR(ret);
1189out_return_state:
1190 atomic_inc(&state->count);
1191 return state;
1192}
1193
Andy Adamsone23008e2012-10-02 21:07:32 -04001194static void
1195nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1196{
1197 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1198 struct nfs_delegation *delegation;
1199 int delegation_flags = 0;
1200
1201 rcu_read_lock();
1202 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1203 if (delegation)
1204 delegation_flags = delegation->flags;
1205 rcu_read_unlock();
1206 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1207 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1208 "returning a delegation for "
1209 "OPEN(CLAIM_DELEGATE_CUR)\n",
1210 clp->cl_hostname);
1211 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1212 nfs_inode_set_delegation(state->inode,
1213 data->owner->so_cred,
1214 &data->o_res);
1215 else
1216 nfs_inode_reclaim_delegation(state->inode,
1217 data->owner->so_cred,
1218 &data->o_res);
1219}
1220
1221/*
1222 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1223 * and update the nfs4_state.
1224 */
1225static struct nfs4_state *
1226_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1227{
1228 struct inode *inode = data->state->inode;
1229 struct nfs4_state *state = data->state;
1230 int ret;
1231
1232 if (!data->rpc_done) {
1233 ret = data->rpc_status;
1234 goto err;
1235 }
1236
1237 ret = -ESTALE;
1238 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1239 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1240 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1241 goto err;
1242
1243 ret = -ENOMEM;
1244 state = nfs4_get_open_state(inode, data->owner);
1245 if (state == NULL)
1246 goto err;
1247
1248 ret = nfs_refresh_inode(inode, &data->f_attr);
1249 if (ret)
1250 goto err;
1251
1252 if (data->o_res.delegation_type != 0)
1253 nfs4_opendata_check_deleg(data, state);
1254 update_open_stateid(state, &data->o_res.stateid, NULL,
1255 data->o_arg.fmode);
1256
1257 return state;
1258err:
1259 return ERR_PTR(ret);
1260
1261}
1262
1263static struct nfs4_state *
1264_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001265{
1266 struct inode *inode;
1267 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001268 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001269
Trond Myklebustaac00a82007-07-05 19:02:21 -04001270 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001271 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001272 goto out;
1273 }
1274
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001275 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001276 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001277 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001278 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001279 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001280 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001281 goto err;
1282 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001283 state = nfs4_get_open_state(inode, data->owner);
1284 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001285 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001286 if (data->o_res.delegation_type != 0)
1287 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001288 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001289 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001290 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001291out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001292 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001293err_put_inode:
1294 iput(inode);
1295err:
1296 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001297}
1298
Andy Adamsone23008e2012-10-02 21:07:32 -04001299static struct nfs4_state *
1300nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1301{
1302 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1303 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1304 return _nfs4_opendata_to_nfs4_state(data);
1305}
1306
Trond Myklebust864472e2006-01-03 09:55:15 +01001307static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1308{
1309 struct nfs_inode *nfsi = NFS_I(state->inode);
1310 struct nfs_open_context *ctx;
1311
1312 spin_lock(&state->inode->i_lock);
1313 list_for_each_entry(ctx, &nfsi->open_files, list) {
1314 if (ctx->state != state)
1315 continue;
1316 get_nfs_open_context(ctx);
1317 spin_unlock(&state->inode->i_lock);
1318 return ctx;
1319 }
1320 spin_unlock(&state->inode->i_lock);
1321 return ERR_PTR(-ENOENT);
1322}
1323
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001324static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1325{
1326 struct nfs4_opendata *opendata;
1327
Al Viro3d4ff432011-06-22 18:40:12 -04001328 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001329 if (opendata == NULL)
1330 return ERR_PTR(-ENOMEM);
1331 opendata->state = state;
1332 atomic_inc(&state->count);
1333 return opendata;
1334}
1335
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001336static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001337{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001338 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001339 int ret;
1340
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001341 opendata->o_arg.open_flags = 0;
1342 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001343 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1344 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1345 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001346 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001347 if (ret != 0)
1348 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001349 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001350 if (IS_ERR(newstate))
1351 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001352 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001353 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001354 return 0;
1355}
1356
1357static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1358{
Trond Myklebust864472e2006-01-03 09:55:15 +01001359 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001360 int ret;
1361
1362 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001363 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001364 smp_rmb();
1365 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001366 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001367 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001368 if (ret != 0)
1369 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001370 if (newstate != state)
1371 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001372 }
1373 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001374 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001375 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001376 if (ret != 0)
1377 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001378 if (newstate != state)
1379 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001380 }
1381 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001382 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001383 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001384 if (ret != 0)
1385 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001386 if (newstate != state)
1387 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001388 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001389 /*
1390 * We may have performed cached opens for all three recoveries.
1391 * Check if we need to update the current stateid.
1392 */
1393 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001394 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001395 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001396 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001397 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001398 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001399 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001400 return 0;
1401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403/*
1404 * OPEN_RECLAIM:
1405 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001407static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001409 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001410 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001411 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 int status;
1413
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001414 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1415 if (IS_ERR(opendata))
1416 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001417 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1418 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001419 rcu_read_lock();
1420 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001421 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001422 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001423 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001424 opendata->o_arg.u.delegation_type = delegation_type;
1425 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001426 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return status;
1428}
1429
Trond Myklebust539cd032007-06-05 11:46:42 -04001430static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 struct nfs_server *server = NFS_SERVER(state->inode);
1433 struct nfs4_exception exception = { };
1434 int err;
1435 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001436 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001437 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001438 break;
1439 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 } while (exception.retry);
1441 return err;
1442}
1443
Trond Myklebust864472e2006-01-03 09:55:15 +01001444static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1445{
1446 struct nfs_open_context *ctx;
1447 int ret;
1448
1449 ctx = nfs4_state_find_open_context(state);
1450 if (IS_ERR(ctx))
1451 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001452 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001453 put_nfs_open_context(ctx);
1454 return ret;
1455}
1456
Trond Myklebust13437e12007-07-06 15:10:43 -04001457static 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 -07001458{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001459 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001460 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001462 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1463 if (IS_ERR(opendata))
1464 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001465 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001466 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001467 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001468 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001469 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Trond Myklebust13437e12007-07-06 15:10:43 -04001472int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001475 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 int err;
1477 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001478 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 switch (err) {
1480 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001481 case -ENOENT:
1482 case -ESTALE:
1483 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001484 case -NFS4ERR_BADSESSION:
1485 case -NFS4ERR_BADSLOT:
1486 case -NFS4ERR_BAD_HIGH_SLOT:
1487 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1488 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001489 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001490 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 case -NFS4ERR_STALE_CLIENTID:
1492 case -NFS4ERR_STALE_STATEID:
1493 case -NFS4ERR_EXPIRED:
1494 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001495 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001496 goto out;
1497 case -ERESTARTSYS:
1498 /*
1499 * The show must go on: exit, but mark the
1500 * stateid as needing recovery.
1501 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001502 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001503 case -NFS4ERR_ADMIN_REVOKED:
1504 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001505 nfs_inode_find_state_and_recover(state->inode,
1506 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001507 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001508 case -EKEYEXPIRED:
1509 /*
1510 * User RPCSEC_GSS context has expired.
1511 * We cannot recover this stateid now, so
1512 * skip it and allow recovery thread to
1513 * proceed.
1514 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001515 case -ENOMEM:
1516 err = 0;
1517 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519 err = nfs4_handle_exception(server, err, &exception);
1520 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001521out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return err;
1523}
1524
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001525static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1526{
1527 struct nfs4_opendata *data = calldata;
1528
1529 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001530 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001531 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001532 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001533 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001534 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001535 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001536}
1537
1538static void nfs4_open_confirm_release(void *calldata)
1539{
1540 struct nfs4_opendata *data = calldata;
1541 struct nfs4_state *state = NULL;
1542
1543 /* If this request hasn't been cancelled, do nothing */
1544 if (data->cancelled == 0)
1545 goto out_free;
1546 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001547 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001548 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001549 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001550 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001551 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001552out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001553 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001554}
1555
1556static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001557 .rpc_call_done = nfs4_open_confirm_done,
1558 .rpc_release = nfs4_open_confirm_release,
1559};
1560
1561/*
1562 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1563 */
1564static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1565{
1566 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1567 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001568 struct rpc_message msg = {
1569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1570 .rpc_argp = &data->c_arg,
1571 .rpc_resp = &data->c_res,
1572 .rpc_cred = data->owner->so_cred,
1573 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001574 struct rpc_task_setup task_setup_data = {
1575 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001576 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001577 .callback_ops = &nfs4_open_confirm_ops,
1578 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001579 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001580 .flags = RPC_TASK_ASYNC,
1581 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int status;
1583
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001584 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001585 data->rpc_done = 0;
1586 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001587 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001588 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001589 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001590 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001591 status = nfs4_wait_for_completion_rpc_task(task);
1592 if (status != 0) {
1593 data->cancelled = 1;
1594 smp_wmb();
1595 } else
1596 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001597 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return status;
1599}
1600
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001601static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001603 struct nfs4_opendata *data = calldata;
1604 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001605
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001606 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1607 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001608 /*
1609 * Check if we still need to send an OPEN call, or if we can use
1610 * a delegation instead.
1611 */
1612 if (data->state != NULL) {
1613 struct nfs_delegation *delegation;
1614
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001615 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001616 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001617 rcu_read_lock();
1618 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001619 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1620 can_open_delegated(delegation, data->o_arg.fmode))
1621 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001622 rcu_read_unlock();
1623 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001624 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001625 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001626 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001627 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001628 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001629 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1630 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001631 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001632 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001633 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001634 &data->o_res.seq_res,
1635 task) != 0)
1636 nfs_release_seqid(data->o_arg.seqid);
1637 else
1638 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001639 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001640unlock_no_action:
1641 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001642out_no_action:
1643 task->tk_action = NULL;
1644
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001647static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1648{
1649 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1650 nfs4_open_prepare(task, calldata);
1651}
1652
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001653static void nfs4_open_done(struct rpc_task *task, void *calldata)
1654{
1655 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001657 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001658
Trond Myklebust14516c32010-07-31 14:29:06 -04001659 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1660 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001661
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001663 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1664 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001665 case S_IFREG:
1666 break;
1667 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001668 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001669 break;
1670 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001671 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001672 break;
1673 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001674 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001675 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001676 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001677 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001678 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1679 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001680 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001681 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001682}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001683
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001684static void nfs4_open_release(void *calldata)
1685{
1686 struct nfs4_opendata *data = calldata;
1687 struct nfs4_state *state = NULL;
1688
1689 /* If this request hasn't been cancelled, do nothing */
1690 if (data->cancelled == 0)
1691 goto out_free;
1692 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001693 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001694 goto out_free;
1695 /* In case we need an open_confirm, no cleanup! */
1696 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1697 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001699 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001700 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001701out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001702 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703}
1704
1705static const struct rpc_call_ops nfs4_open_ops = {
1706 .rpc_call_prepare = nfs4_open_prepare,
1707 .rpc_call_done = nfs4_open_done,
1708 .rpc_release = nfs4_open_release,
1709};
1710
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001711static const struct rpc_call_ops nfs4_recover_open_ops = {
1712 .rpc_call_prepare = nfs4_recover_open_prepare,
1713 .rpc_call_done = nfs4_open_done,
1714 .rpc_release = nfs4_open_release,
1715};
1716
1717static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001718{
1719 struct inode *dir = data->dir->d_inode;
1720 struct nfs_server *server = NFS_SERVER(dir);
1721 struct nfs_openargs *o_arg = &data->o_arg;
1722 struct nfs_openres *o_res = &data->o_res;
1723 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001724 struct rpc_message msg = {
1725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1726 .rpc_argp = o_arg,
1727 .rpc_resp = o_res,
1728 .rpc_cred = data->owner->so_cred,
1729 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001730 struct rpc_task_setup task_setup_data = {
1731 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001732 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001733 .callback_ops = &nfs4_open_ops,
1734 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001735 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001736 .flags = RPC_TASK_ASYNC,
1737 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001738 int status;
1739
Trond Myklebust9d12b212012-01-17 22:04:25 -05001740 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001741 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001742 data->rpc_done = 0;
1743 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001744 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001745 if (isrecover)
1746 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001747 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001748 if (IS_ERR(task))
1749 return PTR_ERR(task);
1750 status = nfs4_wait_for_completion_rpc_task(task);
1751 if (status != 0) {
1752 data->cancelled = 1;
1753 smp_wmb();
1754 } else
1755 status = data->rpc_status;
1756 rpc_put_task(task);
1757
1758 return status;
1759}
1760
1761static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1762{
1763 struct inode *dir = data->dir->d_inode;
1764 struct nfs_openres *o_res = &data->o_res;
1765 int status;
1766
1767 status = nfs4_run_open_task(data, 1);
1768 if (status != 0 || !data->rpc_done)
1769 return status;
1770
Trond Myklebust6926afd2012-01-07 13:22:46 -05001771 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1772
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001773 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1774 status = _nfs4_proc_open_confirm(data);
1775 if (status != 0)
1776 return status;
1777 }
1778
1779 return status;
1780}
1781
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001782static int nfs4_opendata_access(struct rpc_cred *cred,
1783 struct nfs4_opendata *opendata,
1784 struct nfs4_state *state, fmode_t fmode)
1785{
1786 struct nfs_access_entry cache;
1787 u32 mask;
1788
1789 /* access call failed or for some reason the server doesn't
1790 * support any access modes -- defer access call until later */
1791 if (opendata->o_res.access_supported == 0)
1792 return 0;
1793
1794 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001795 /* don't check MAY_WRITE - a newly created file may not have
1796 * write mode bits, but POSIX allows the creating process to write */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001797 if (fmode & FMODE_READ)
1798 mask |= MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001799 if (fmode & FMODE_EXEC)
1800 mask |= MAY_EXEC;
1801
1802 cache.cred = cred;
1803 cache.jiffies = jiffies;
1804 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1805 nfs_access_add_cache(state->inode, &cache);
1806
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001807 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001808 return 0;
1809
1810 /* even though OPEN succeeded, access is denied. Close the file */
1811 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001812 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001813}
1814
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001815/*
1816 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1817 */
1818static int _nfs4_proc_open(struct nfs4_opendata *data)
1819{
1820 struct inode *dir = data->dir->d_inode;
1821 struct nfs_server *server = NFS_SERVER(dir);
1822 struct nfs_openargs *o_arg = &data->o_arg;
1823 struct nfs_openres *o_res = &data->o_res;
1824 int status;
1825
1826 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001827 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001828 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001829 if (status != 0) {
1830 if (status == -NFS4ERR_BADNAME &&
1831 !(o_arg->open_flags & O_CREAT))
1832 return -ENOENT;
1833 return status;
1834 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001835
Trond Myklebust6926afd2012-01-07 13:22:46 -05001836 nfs_fattr_map_and_free_names(server, &data->f_attr);
1837
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001838 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001839 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001840 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1841 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001843 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001848 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Andy Adamsond83217c2011-03-01 01:34:17 +00001852static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001853{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001854 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001855 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001856
Trond Myklebusta78cb572009-08-09 15:06:19 -04001857 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001858 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001859 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001860 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001861 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1862 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001863 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001864 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001865 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001866 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001867 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001868}
1869
Andy Adamsond83217c2011-03-01 01:34:17 +00001870static int nfs4_recover_expired_lease(struct nfs_server *server)
1871{
1872 return nfs4_client_recover_expired_lease(server->nfs_client);
1873}
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875/*
1876 * OPEN_EXPIRED:
1877 * reclaim state on the server after a network partition.
1878 * Assumes caller holds the appropriate lock
1879 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001880static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001882 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001883 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001885 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1886 if (IS_ERR(opendata))
1887 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001888 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001889 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001890 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001891 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001892 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001895static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001896{
Trond Myklebust539cd032007-06-05 11:46:42 -04001897 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001898 struct nfs4_exception exception = { };
1899 int err;
1900
1901 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001902 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001903 switch (err) {
1904 default:
1905 goto out;
1906 case -NFS4ERR_GRACE:
1907 case -NFS4ERR_DELAY:
1908 nfs4_handle_exception(server, err, &exception);
1909 err = 0;
1910 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001911 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001912out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001913 return err;
1914}
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1917{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001919 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Trond Myklebust864472e2006-01-03 09:55:15 +01001921 ctx = nfs4_state_find_open_context(state);
1922 if (IS_ERR(ctx))
1923 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001924 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001925 put_nfs_open_context(ctx);
1926 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001929#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001930static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001931{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001932 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001933 nfs4_stateid *stateid = &state->stateid;
1934 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001935
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001936 /* If a state reset has been done, test_stateid is unneeded */
1937 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1938 return;
1939
1940 status = nfs41_test_stateid(server, stateid);
1941 if (status != NFS_OK) {
1942 /* Free the stateid unless the server explicitly
1943 * informs us the stateid is unrecognized. */
1944 if (status != -NFS4ERR_BAD_STATEID)
1945 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001946 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001947
Bryan Schumaker69388672012-09-26 15:25:52 -04001948 write_seqlock(&state->seqlock);
1949 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1950 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001951 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1952 }
1953}
1954
1955/**
1956 * nfs41_check_open_stateid - possibly free an open stateid
1957 *
1958 * @state: NFSv4 state for an inode
1959 *
1960 * Returns NFS_OK if recovery for this stateid is now finished.
1961 * Otherwise a negative NFS4ERR value is returned.
1962 */
1963static int nfs41_check_open_stateid(struct nfs4_state *state)
1964{
1965 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001966 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001967 int status;
1968
1969 /* If a state reset has been done, test_stateid is unneeded */
1970 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1971 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1972 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1973 return -NFS4ERR_BAD_STATEID;
1974
1975 status = nfs41_test_stateid(server, stateid);
1976 if (status != NFS_OK) {
1977 /* Free the stateid unless the server explicitly
1978 * informs us the stateid is unrecognized. */
1979 if (status != -NFS4ERR_BAD_STATEID)
1980 nfs41_free_stateid(server, stateid);
1981
1982 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1983 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1984 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001985 }
1986 return status;
1987}
1988
1989static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1990{
Chuck Levereb64cf92012-07-11 16:30:05 -04001991 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001992
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001993 nfs41_clear_delegation_stateid(state);
1994 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001995 if (status != NFS_OK)
1996 status = nfs4_open_expired(sp, state);
1997 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001998}
1999#endif
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002002 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2003 * fields corresponding to attributes that were used to store the verifier.
2004 * Make sure we clobber those fields in the later setattr call
2005 */
2006static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2007{
2008 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2009 !(sattr->ia_valid & ATTR_ATIME_SET))
2010 sattr->ia_valid |= ATTR_ATIME;
2011
2012 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2013 !(sattr->ia_valid & ATTR_MTIME_SET))
2014 sattr->ia_valid |= ATTR_MTIME;
2015}
2016
2017/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002018 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 */
Andy Adamson82be4172012-05-23 05:02:35 -04002020static int _nfs4_do_open(struct inode *dir,
2021 struct dentry *dentry,
2022 fmode_t fmode,
2023 int flags,
2024 struct iattr *sattr,
2025 struct rpc_cred *cred,
2026 struct nfs4_state **res,
2027 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct nfs4_state_owner *sp;
2030 struct nfs4_state *state = NULL;
2031 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002032 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002033 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002037 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2038 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2040 goto out_err;
2041 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002042 status = nfs4_recover_expired_lease(server);
2043 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002044 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002045 if (dentry->d_inode != NULL)
2046 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002047 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04002048 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002049 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002050 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Andy Adamson82be4172012-05-23 05:02:35 -04002052 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2053 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2054 if (!opendata->f_attr.mdsthreshold)
2055 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002056 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002057 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002058 if (dentry->d_inode != NULL)
2059 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002060
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002061 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002063 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002065 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002066 status = PTR_ERR(state);
2067 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002068 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002069 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05002070 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002071
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002072 status = nfs4_opendata_access(cred, opendata, state, fmode);
2073 if (status != 0)
2074 goto err_opendata_put;
2075
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002076 if (opendata->o_arg.open_flags & O_EXCL) {
2077 nfs4_exclusive_attrset(opendata, sattr);
2078
2079 nfs_fattr_init(opendata->o_res.f_attr);
2080 status = nfs4_do_setattr(state->inode, cred,
2081 opendata->o_res.f_attr, sattr,
2082 state);
2083 if (status == 0)
2084 nfs_setattr_update_inode(state->inode, sattr);
2085 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2086 }
Andy Adamson82be4172012-05-23 05:02:35 -04002087
2088 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2089 *ctx_th = opendata->f_attr.mdsthreshold;
2090 else
2091 kfree(opendata->f_attr.mdsthreshold);
2092 opendata->f_attr.mdsthreshold = NULL;
2093
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002094 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 *res = state;
2097 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002098err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002099 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002100 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002101err_put_state_owner:
2102 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 *res = NULL;
2105 return status;
2106}
2107
2108
Andy Adamson82be4172012-05-23 05:02:35 -04002109static struct nfs4_state *nfs4_do_open(struct inode *dir,
2110 struct dentry *dentry,
2111 fmode_t fmode,
2112 int flags,
2113 struct iattr *sattr,
2114 struct rpc_cred *cred,
2115 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
2117 struct nfs4_exception exception = { };
2118 struct nfs4_state *res;
2119 int status;
2120
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002121 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002123 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2124 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (status == 0)
2126 break;
2127 /* NOTE: BAD_SEQID means the server and client disagree about the
2128 * book-keeping w.r.t. state-changing operations
2129 * (OPEN/CLOSE/LOCK/LOCKU...)
2130 * It is actually a sign of a bug on the client or on the server.
2131 *
2132 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002133 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 * have unhashed the old state_owner for us, and that we can
2135 * therefore safely retry using a new one. We should still warn
2136 * the user though...
2137 */
2138 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002139 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002140 " returned a bad sequence-id error!\n",
2141 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 exception.retry = 1;
2143 continue;
2144 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002145 /*
2146 * BAD_STATEID on OPEN means that the server cancelled our
2147 * state before it received the OPEN_CONFIRM.
2148 * Recover by retrying the request as per the discussion
2149 * on Page 181 of RFC3530.
2150 */
2151 if (status == -NFS4ERR_BAD_STATEID) {
2152 exception.retry = 1;
2153 continue;
2154 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002155 if (status == -EAGAIN) {
2156 /* We must have found a delegation */
2157 exception.retry = 1;
2158 continue;
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2161 status, &exception));
2162 } while (exception.retry);
2163 return res;
2164}
2165
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002166static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2167 struct nfs_fattr *fattr, struct iattr *sattr,
2168 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002170 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002172 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 .iap = sattr,
2174 .server = server,
2175 .bitmask = server->attr_bitmask,
2176 };
2177 struct nfs_setattrres res = {
2178 .fattr = fattr,
2179 .server = server,
2180 };
2181 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2183 .rpc_argp = &arg,
2184 .rpc_resp = &res,
2185 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002187 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002188 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Trond Myklebust0e574af2005-10-27 22:12:38 -04002190 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Trond Myklebust4fc87962012-03-08 17:42:01 -05002192 if (state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002193 struct nfs_lockowner lockowner = {
2194 .l_owner = current->files,
2195 .l_pid = current->tgid,
2196 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002197 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002198 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002199 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2200 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002201 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002202 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002203 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Bryan Schumaker7c513052011-03-24 17:12:24 +00002205 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002206 if (status == 0 && state != NULL)
2207 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002208 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002211static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2212 struct nfs_fattr *fattr, struct iattr *sattr,
2213 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002215 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002216 struct nfs4_exception exception = {
2217 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002218 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002219 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 int err;
2221 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002222 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2223 switch (err) {
2224 case -NFS4ERR_OPENMODE:
2225 if (state && !(state->state & FMODE_WRITE)) {
2226 err = -EBADF;
2227 if (sattr->ia_valid & ATTR_OPEN)
2228 err = -EACCES;
2229 goto out;
2230 }
2231 }
2232 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002234out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return err;
2236}
2237
2238struct nfs4_closedata {
2239 struct inode *inode;
2240 struct nfs4_state *state;
2241 struct nfs_closeargs arg;
2242 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002243 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002244 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002245 bool roc;
2246 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247};
2248
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002249static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002250{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002251 struct nfs4_closedata *calldata = data;
2252 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002253 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002254
Fred Isamanf7e89172011-01-06 11:36:32 +00002255 if (calldata->roc)
2256 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002257 nfs4_put_open_state(calldata->state);
2258 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002259 nfs4_put_state_owner(sp);
Weston Andros Adamson324d0032012-10-30 17:01:39 -04002260 nfs_sb_deactive_async(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002261 kfree(calldata);
2262}
2263
Trond Myklebust88069f72009-12-08 08:33:16 -05002264static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2265 fmode_t fmode)
2266{
2267 spin_lock(&state->owner->so_lock);
2268 if (!(fmode & FMODE_READ))
2269 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2270 if (!(fmode & FMODE_WRITE))
2271 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2272 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2273 spin_unlock(&state->owner->so_lock);
2274}
2275
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002276static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002278 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 struct nfs_server *server = NFS_SERVER(calldata->inode);
2281
Chuck Levera3ca5652012-03-01 17:00:40 -05002282 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002283 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2284 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 /* hmm. we are done with the inode, and in the process of freeing
2286 * the state_owner. we keep this around to process errors
2287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 switch (task->tk_status) {
2289 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002290 if (calldata->roc)
2291 pnfs_roc_set_barrier(state->inode,
2292 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002293 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002294 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002295 nfs4_close_clear_stateid_flags(state,
2296 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 break;
2298 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002299 case -NFS4ERR_OLD_STATEID:
2300 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002302 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002305 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2306 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002308 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002309 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002310 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002313static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002315 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002316 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002317 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002318 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002319
Chuck Levera3ca5652012-03-01 17:00:40 -05002320 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002321 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002322 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002323
Trond Myklebust88069f72009-12-08 08:33:16 -05002324 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2325 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002326 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002327 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002328 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002329 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002330 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2331 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2332 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002333 }
2334 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002335 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2336 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2337 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002338 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002339 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002340 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002341
2342 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002343 /* Note: exit _without_ calling nfs4_close_done */
2344 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002345 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002346 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002347
Fred Isamanf7e89172011-01-06 11:36:32 +00002348 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002349 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002350 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002351 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002352 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002353 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002354
Trond Myklebust516a6af2005-10-27 22:12:41 -04002355 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002356 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002357 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002358 &calldata->arg.seq_args,
2359 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002360 task) != 0)
2361 nfs_release_seqid(calldata->arg.seqid);
2362 else
2363 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002364out:
2365 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002368static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002369 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002370 .rpc_call_done = nfs4_close_done,
2371 .rpc_release = nfs4_free_closedata,
2372};
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374/*
2375 * It is possible for data to be read/written from a mem-mapped file
2376 * after the sys_close call (which hits the vfs layer as a flush).
2377 * This means that we can't safely call nfsv4 close on a file until
2378 * the inode is cleared. This in turn means that we are not good
2379 * NFSv4 citizens - we do not indicate to the server to update the file's
2380 * share state even when we are done with one of the three share
2381 * stateid's in the inode.
2382 *
2383 * NOTE: Caller must be holding the sp->so_owner semaphore!
2384 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002385int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002387 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002389 struct nfs4_state_owner *sp = state->owner;
2390 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002391 struct rpc_message msg = {
2392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2393 .rpc_cred = state->owner->so_cred,
2394 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002395 struct rpc_task_setup task_setup_data = {
2396 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002397 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002398 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002399 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002400 .flags = RPC_TASK_ASYNC,
2401 };
Trond Myklebust95121352005-10-18 14:20:12 -07002402 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002404 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002406 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002407 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002408 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002410 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002411 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002413 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002414 if (calldata->arg.seqid == NULL)
2415 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002416 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002417 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002418 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002419 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002420 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002421 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002422 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002423
Trond Myklebust1174dd12010-12-21 10:52:24 -05002424 msg.rpc_argp = &calldata->arg;
2425 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002426 task_setup_data.callback_data = calldata;
2427 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002428 if (IS_ERR(task))
2429 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002430 status = 0;
2431 if (wait)
2432 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002433 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002434 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002435out_free_calldata:
2436 kfree(calldata);
2437out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002438 nfs4_put_open_state(state);
2439 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002440 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Trond Myklebust2b484292010-09-17 10:56:51 -04002443static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002444nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 struct nfs4_state *state;
2447
Trond Myklebust565277f2007-10-15 18:17:53 -04002448 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002449 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2450 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002451 if (IS_ERR(state))
2452 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002453 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002454 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
Trond Myklebust1185a552009-12-03 15:54:02 -05002457static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002458{
2459 if (ctx->state == NULL)
2460 return;
2461 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002462 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002463 else
Al Viro643168c2011-06-22 18:20:23 -04002464 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002465}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2468{
Benny Halevy43652ad2009-04-01 09:21:54 -04002469 struct nfs4_server_caps_arg args = {
2470 .fhandle = fhandle,
2471 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 struct nfs4_server_caps_res res = {};
2473 struct rpc_message msg = {
2474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002475 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 .rpc_resp = &res,
2477 };
2478 int status;
2479
Bryan Schumaker7c513052011-03-24 17:12:24 +00002480 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (status == 0) {
2482 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002483 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2484 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2485 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2486 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2487 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2489 server->caps |= NFS_CAP_ACLS;
2490 if (res.has_links != 0)
2491 server->caps |= NFS_CAP_HARDLINKS;
2492 if (res.has_symlinks != 0)
2493 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002494 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2495 server->caps |= NFS_CAP_FILEID;
2496 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2497 server->caps |= NFS_CAP_MODE;
2498 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2499 server->caps |= NFS_CAP_NLINK;
2500 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2501 server->caps |= NFS_CAP_OWNER;
2502 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2503 server->caps |= NFS_CAP_OWNER_GROUP;
2504 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2505 server->caps |= NFS_CAP_ATIME;
2506 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2507 server->caps |= NFS_CAP_CTIME;
2508 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2509 server->caps |= NFS_CAP_MTIME;
2510
Trond Myklebusta65318b2009-03-11 14:10:28 -04002511 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2512 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2513 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002515 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return status;
2519}
2520
Trond Myklebust55a97592006-06-09 09:34:19 -04002521int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
2523 struct nfs4_exception exception = { };
2524 int err;
2525 do {
2526 err = nfs4_handle_exception(server,
2527 _nfs4_server_capabilities(server, fhandle),
2528 &exception);
2529 } while (exception.retry);
2530 return err;
2531}
2532
2533static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2534 struct nfs_fsinfo *info)
2535{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 struct nfs4_lookup_root_arg args = {
2537 .bitmask = nfs4_fattr_bitmap,
2538 };
2539 struct nfs4_lookup_res res = {
2540 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002541 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 .fh = fhandle,
2543 };
2544 struct rpc_message msg = {
2545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2546 .rpc_argp = &args,
2547 .rpc_resp = &res,
2548 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002549
Trond Myklebust0e574af2005-10-27 22:12:38 -04002550 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002551 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
2554static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2555 struct nfs_fsinfo *info)
2556{
2557 struct nfs4_exception exception = { };
2558 int err;
2559 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002560 err = _nfs4_lookup_root(server, fhandle, info);
2561 switch (err) {
2562 case 0:
2563 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002564 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002565 default:
2566 err = nfs4_handle_exception(server, err, &exception);
2567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002569out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return err;
2571}
2572
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002573static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2574 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2575{
2576 struct rpc_auth *auth;
2577 int ret;
2578
2579 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002580 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002581 ret = -EIO;
2582 goto out;
2583 }
2584 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002585out:
2586 return ret;
2587}
2588
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002589static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002590 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002592 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002593 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002595 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002596 if (len < 0)
2597 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002598
2599 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002600 /* AUTH_UNIX is the default flavor if none was specified,
2601 * thus has already been tried. */
2602 if (flav_array[i] == RPC_AUTH_UNIX)
2603 continue;
2604
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002605 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002606 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002607 continue;
2608 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002609 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002610 /*
2611 * -EACCESS could mean that the user doesn't have correct permissions
2612 * to access the mount. It could also mean that we tried to mount
2613 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2614 * existing mount programs don't handle -EACCES very well so it should
2615 * be mapped to -EPERM instead.
2616 */
2617 if (status == -EACCES)
2618 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002619 return status;
2620}
2621
2622/*
2623 * get the file handle for the "/" directory on the server
2624 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002625int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2626 struct nfs_fsinfo *info)
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002627{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002628 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002629 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002630 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2631 /*
2632 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2633 * by nfs4_map_errors() as this function exits.
2634 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002635 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (status == 0)
2637 status = nfs4_server_capabilities(server, fhandle);
2638 if (status == 0)
2639 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002640 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Bryan Schumakerbae36242012-05-10 15:07:31 -04002643static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2644 struct nfs_fsinfo *info)
2645{
2646 int error;
2647 struct nfs_fattr *fattr = info->fattr;
2648
2649 error = nfs4_server_capabilities(server, mntfh);
2650 if (error < 0) {
2651 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2652 return error;
2653 }
2654
2655 error = nfs4_proc_getattr(server, mntfh, fattr);
2656 if (error < 0) {
2657 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2658 return error;
2659 }
2660
2661 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2662 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2663 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2664
2665 return error;
2666}
2667
Manoj Naik6b97fd32006-06-09 09:34:29 -04002668/*
2669 * Get locations and (maybe) other attributes of a referral.
2670 * Note that we'll actually follow the referral later when
2671 * we detect fsid mismatch in inode revalidation
2672 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002673static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2674 const struct qstr *name, struct nfs_fattr *fattr,
2675 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002676{
2677 int status = -ENOMEM;
2678 struct page *page = NULL;
2679 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002680
2681 page = alloc_page(GFP_KERNEL);
2682 if (page == NULL)
2683 goto out;
2684 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2685 if (locations == NULL)
2686 goto out;
2687
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002688 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002689 if (status != 0)
2690 goto out;
2691 /* Make sure server returned a different fsid for the referral */
2692 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002693 dprintk("%s: server did not return a different fsid for"
2694 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002695 status = -EIO;
2696 goto out;
2697 }
Andy Adamson533eb462011-06-13 18:25:56 -04002698 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2699 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002700
Andy Adamson533eb462011-06-13 18:25:56 -04002701 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002702 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002703 memset(fhandle, 0, sizeof(struct nfs_fh));
2704out:
2705 if (page)
2706 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002707 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002708 return status;
2709}
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2712{
2713 struct nfs4_getattr_arg args = {
2714 .fh = fhandle,
2715 .bitmask = server->attr_bitmask,
2716 };
2717 struct nfs4_getattr_res res = {
2718 .fattr = fattr,
2719 .server = server,
2720 };
2721 struct rpc_message msg = {
2722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2723 .rpc_argp = &args,
2724 .rpc_resp = &res,
2725 };
2726
Trond Myklebust0e574af2005-10-27 22:12:38 -04002727 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002728 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
2731static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2732{
2733 struct nfs4_exception exception = { };
2734 int err;
2735 do {
2736 err = nfs4_handle_exception(server,
2737 _nfs4_proc_getattr(server, fhandle, fattr),
2738 &exception);
2739 } while (exception.retry);
2740 return err;
2741}
2742
2743/*
2744 * The file is not closed if it is opened due to the a request to change
2745 * the size of the file. The open call will not be needed once the
2746 * VFS layer lookup-intents are implemented.
2747 *
2748 * Close is called when the inode is destroyed.
2749 * If we haven't opened the file for O_WRONLY, we
2750 * need to in the size_change case to obtain a stateid.
2751 *
2752 * Got race?
2753 * Because OPEN is always done by name in nfsv4, it is
2754 * possible that we opened a different file by the same
2755 * name. We can recognize this race condition, but we
2756 * can't do anything about it besides returning an error.
2757 *
2758 * This will be fixed with VFS changes (lookup-intent).
2759 */
2760static int
2761nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2762 struct iattr *sattr)
2763{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002764 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002765 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002766 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 int status;
2768
Benny Halevy8a1636c2010-07-14 15:43:57 -04002769 if (pnfs_ld_layoutret_on_setattr(inode))
2770 pnfs_return_layout(inode);
2771
Trond Myklebust0e574af2005-10-27 22:12:38 -04002772 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Andy Adamson26699402012-05-30 16:12:24 -04002774 /* Deal with open(O_TRUNC) */
2775 if (sattr->ia_valid & ATTR_OPEN)
2776 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2777
2778 /* Optimization: if the end result is no change, don't RPC */
2779 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2780 return 0;
2781
Trond Myklebustd5308382005-11-04 15:33:38 -05002782 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002783 if (sattr->ia_valid & ATTR_FILE) {
2784 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002785
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002786 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002787 if (ctx) {
2788 cred = ctx->cred;
2789 state = ctx->state;
2790 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002791 }
2792
2793 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002794 if (status == 0)
2795 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return status;
2797}
2798
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002799static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2800 const struct qstr *name, struct nfs_fh *fhandle,
2801 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002802{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002803 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002804 int status;
2805 struct nfs4_lookup_arg args = {
2806 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002807 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002808 .name = name,
2809 };
2810 struct nfs4_lookup_res res = {
2811 .server = server,
2812 .fattr = fattr,
2813 .fh = fhandle,
2814 };
2815 struct rpc_message msg = {
2816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2817 .rpc_argp = &args,
2818 .rpc_resp = &res,
2819 };
2820
2821 nfs_fattr_init(fattr);
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002824 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 dprintk("NFS reply lookup: %d\n", status);
2826 return status;
2827}
2828
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002829static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002830{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002831 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002832 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002833 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2834 fattr->nlink = 2;
2835}
2836
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002837static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2838 struct qstr *name, struct nfs_fh *fhandle,
2839 struct nfs_fattr *fattr)
2840{
2841 struct nfs4_exception exception = { };
2842 struct rpc_clnt *client = *clnt;
2843 int err;
2844 do {
2845 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2846 switch (err) {
2847 case -NFS4ERR_BADNAME:
2848 err = -ENOENT;
2849 goto out;
2850 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002851 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002852 goto out;
2853 case -NFS4ERR_WRONGSEC:
2854 err = -EPERM;
2855 if (client != *clnt)
2856 goto out;
2857
2858 client = nfs4_create_sec_client(client, dir, name);
2859 if (IS_ERR(client))
2860 return PTR_ERR(client);
2861
2862 exception.retry = 1;
2863 break;
2864 default:
2865 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2866 }
2867 } while (exception.retry);
2868
2869out:
2870 if (err == 0)
2871 *clnt = client;
2872 else if (client != *clnt)
2873 rpc_shutdown_client(client);
2874
2875 return err;
2876}
2877
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002878static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002879 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002881 int status;
2882 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002883
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002884 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2885 if (client != NFS_CLIENT(dir)) {
2886 rpc_shutdown_client(client);
2887 nfs_fixup_secinfo_attributes(fattr);
2888 }
2889 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002892struct rpc_clnt *
2893nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2894 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2895{
2896 int status;
2897 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2898
2899 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2900 if (status < 0) {
2901 rpc_shutdown_client(client);
2902 return ERR_PTR(status);
2903 }
2904 return client;
2905}
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2908{
Trond Myklebust76b32992007-08-10 17:45:11 -04002909 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 struct nfs4_accessargs args = {
2911 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002912 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002914 struct nfs4_accessres res = {
2915 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002916 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 struct rpc_message msg = {
2918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2919 .rpc_argp = &args,
2920 .rpc_resp = &res,
2921 .rpc_cred = entry->cred,
2922 };
2923 int mode = entry->mask;
2924 int status;
2925
2926 /*
2927 * Determine which access bits we want to ask for...
2928 */
2929 if (mode & MAY_READ)
2930 args.access |= NFS4_ACCESS_READ;
2931 if (S_ISDIR(inode->i_mode)) {
2932 if (mode & MAY_WRITE)
2933 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2934 if (mode & MAY_EXEC)
2935 args.access |= NFS4_ACCESS_LOOKUP;
2936 } else {
2937 if (mode & MAY_WRITE)
2938 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2939 if (mode & MAY_EXEC)
2940 args.access |= NFS4_ACCESS_EXECUTE;
2941 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002942
2943 res.fattr = nfs_alloc_fattr();
2944 if (res.fattr == NULL)
2945 return -ENOMEM;
2946
Bryan Schumaker7c513052011-03-24 17:12:24 +00002947 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002949 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002950 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002952 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 return status;
2954}
2955
2956static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2957{
2958 struct nfs4_exception exception = { };
2959 int err;
2960 do {
2961 err = nfs4_handle_exception(NFS_SERVER(inode),
2962 _nfs4_proc_access(inode, entry),
2963 &exception);
2964 } while (exception.retry);
2965 return err;
2966}
2967
2968/*
2969 * TODO: For the time being, we don't try to get any attributes
2970 * along with any of the zero-copy operations READ, READDIR,
2971 * READLINK, WRITE.
2972 *
2973 * In the case of the first three, we want to put the GETATTR
2974 * after the read-type operation -- this is because it is hard
2975 * to predict the length of a GETATTR response in v4, and thus
2976 * align the READ data correctly. This means that the GETATTR
2977 * may end up partially falling into the page cache, and we should
2978 * shift it into the 'tail' of the xdr_buf before processing.
2979 * To do this efficiently, we need to know the total length
2980 * of data received, which doesn't seem to be available outside
2981 * of the RPC layer.
2982 *
2983 * In the case of WRITE, we also want to put the GETATTR after
2984 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002985 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 *
2987 * Both of these changes to the XDR layer would in fact be quite
2988 * minor, but I decided to leave them for a subsequent patch.
2989 */
2990static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2991 unsigned int pgbase, unsigned int pglen)
2992{
2993 struct nfs4_readlink args = {
2994 .fh = NFS_FH(inode),
2995 .pgbase = pgbase,
2996 .pglen = pglen,
2997 .pages = &page,
2998 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002999 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 struct rpc_message msg = {
3001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3002 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003003 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 };
3005
Bryan Schumaker7c513052011-03-24 17:12:24 +00003006 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 -07003007}
3008
3009static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3010 unsigned int pgbase, unsigned int pglen)
3011{
3012 struct nfs4_exception exception = { };
3013 int err;
3014 do {
3015 err = nfs4_handle_exception(NFS_SERVER(inode),
3016 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3017 &exception);
3018 } while (exception.retry);
3019 return err;
3020}
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003023 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025static int
3026nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003027 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003029 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 int status = 0;
3032
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003033 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3034 if (IS_ERR(ctx))
3035 return PTR_ERR(ctx);
3036
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003037 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003038 state = nfs4_do_open(dir, dentry, ctx->mode,
3039 flags, sattr, ctx->cred,
3040 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003041 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if (IS_ERR(state)) {
3043 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003044 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003046 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003047 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003048 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003050 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return status;
3052}
3053
3054static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3055{
Trond Myklebust16e42952005-10-27 22:12:44 -04003056 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003057 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003059 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003061 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003062 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003063 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3066 .rpc_argp = &args,
3067 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003069 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003070
Bryan Schumaker7c513052011-03-24 17:12:24 +00003071 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003072 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003073 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return status;
3075}
3076
3077static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3078{
3079 struct nfs4_exception exception = { };
3080 int err;
3081 do {
3082 err = nfs4_handle_exception(NFS_SERVER(dir),
3083 _nfs4_proc_remove(dir, name),
3084 &exception);
3085 } while (exception.retry);
3086 return err;
3087}
3088
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003089static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003091 struct nfs_server *server = NFS_SERVER(dir);
3092 struct nfs_removeargs *args = msg->rpc_argp;
3093 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003095 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003097 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098}
3099
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003100static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3101{
3102 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3103 &data->args.seq_args,
3104 &data->res.seq_res,
3105 task))
3106 return;
3107 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003110static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003112 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3113
Trond Myklebust14516c32010-07-31 14:29:06 -04003114 if (!nfs4_sequence_done(task, &res->seq_res))
3115 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003116 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003117 return 0;
3118 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003119 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120}
3121
Jeff Laytond3d41522010-09-17 17:31:57 -04003122static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3123{
3124 struct nfs_server *server = NFS_SERVER(dir);
3125 struct nfs_renameargs *arg = msg->rpc_argp;
3126 struct nfs_renameres *res = msg->rpc_resp;
3127
3128 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003129 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003130 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003131}
3132
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003133static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3134{
3135 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3136 &data->args.seq_args,
3137 &data->res.seq_res,
3138 task))
3139 return;
3140 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003141}
3142
3143static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3144 struct inode *new_dir)
3145{
3146 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3147
3148 if (!nfs4_sequence_done(task, &res->seq_res))
3149 return 0;
3150 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3151 return 0;
3152
3153 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003154 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003155 return 1;
3156}
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3159 struct inode *new_dir, struct qstr *new_name)
3160{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003161 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003162 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 .old_dir = NFS_FH(old_dir),
3164 .new_dir = NFS_FH(new_dir),
3165 .old_name = old_name,
3166 .new_name = new_name,
3167 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003168 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003169 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003170 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 struct rpc_message msg = {
3172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3173 .rpc_argp = &arg,
3174 .rpc_resp = &res,
3175 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003176 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Bryan Schumaker7c513052011-03-24 17:12:24 +00003178 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (!status) {
3180 update_changeattr(old_dir, &res.old_cinfo);
3181 update_changeattr(new_dir, &res.new_cinfo);
3182 }
3183 return status;
3184}
3185
3186static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3187 struct inode *new_dir, struct qstr *new_name)
3188{
3189 struct nfs4_exception exception = { };
3190 int err;
3191 do {
3192 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3193 _nfs4_proc_rename(old_dir, old_name,
3194 new_dir, new_name),
3195 &exception);
3196 } while (exception.retry);
3197 return err;
3198}
3199
3200static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3201{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003202 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 struct nfs4_link_arg arg = {
3204 .fh = NFS_FH(inode),
3205 .dir_fh = NFS_FH(dir),
3206 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003207 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003209 struct nfs4_link_res res = {
3210 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003211 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 struct rpc_message msg = {
3213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3214 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003215 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003217 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Trond Myklebust136f2622010-04-16 16:22:49 -04003219 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003220 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003221 goto out;
3222
Bryan Schumaker7c513052011-03-24 17:12:24 +00003223 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003224 if (!status) {
3225 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003226 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003227 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003228out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003229 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 return status;
3231}
3232
3233static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3234{
3235 struct nfs4_exception exception = { };
3236 int err;
3237 do {
3238 err = nfs4_handle_exception(NFS_SERVER(inode),
3239 _nfs4_proc_link(inode, dir, name),
3240 &exception);
3241 } while (exception.retry);
3242 return err;
3243}
3244
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003245struct nfs4_createdata {
3246 struct rpc_message msg;
3247 struct nfs4_create_arg arg;
3248 struct nfs4_create_res res;
3249 struct nfs_fh fh;
3250 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003251};
3252
3253static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3254 struct qstr *name, struct iattr *sattr, u32 ftype)
3255{
3256 struct nfs4_createdata *data;
3257
3258 data = kzalloc(sizeof(*data), GFP_KERNEL);
3259 if (data != NULL) {
3260 struct nfs_server *server = NFS_SERVER(dir);
3261
3262 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3263 data->msg.rpc_argp = &data->arg;
3264 data->msg.rpc_resp = &data->res;
3265 data->arg.dir_fh = NFS_FH(dir);
3266 data->arg.server = server;
3267 data->arg.name = name;
3268 data->arg.attrs = sattr;
3269 data->arg.ftype = ftype;
3270 data->arg.bitmask = server->attr_bitmask;
3271 data->res.server = server;
3272 data->res.fh = &data->fh;
3273 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003274 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003275 }
3276 return data;
3277}
3278
3279static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3280{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003281 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003282 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003283 if (status == 0) {
3284 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003285 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3286 }
3287 return status;
3288}
3289
3290static void nfs4_free_createdata(struct nfs4_createdata *data)
3291{
3292 kfree(data);
3293}
3294
Chuck Lever4f390c12006-08-22 20:06:22 -04003295static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003296 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003298 struct nfs4_createdata *data;
3299 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Chuck Lever94a6d752006-08-22 20:06:23 -04003301 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003302 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003303
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003304 status = -ENOMEM;
3305 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3306 if (data == NULL)
3307 goto out;
3308
3309 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3310 data->arg.u.symlink.pages = &page;
3311 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003313 status = nfs4_do_create(dir, dentry, data);
3314
3315 nfs4_free_createdata(data);
3316out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 return status;
3318}
3319
Chuck Lever4f390c12006-08-22 20:06:22 -04003320static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003321 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
3323 struct nfs4_exception exception = { };
3324 int err;
3325 do {
3326 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003327 _nfs4_proc_symlink(dir, dentry, page,
3328 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 &exception);
3330 } while (exception.retry);
3331 return err;
3332}
3333
3334static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3335 struct iattr *sattr)
3336{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003337 struct nfs4_createdata *data;
3338 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003340 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3341 if (data == NULL)
3342 goto out;
3343
3344 status = nfs4_do_create(dir, dentry, data);
3345
3346 nfs4_free_createdata(data);
3347out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return status;
3349}
3350
3351static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3352 struct iattr *sattr)
3353{
3354 struct nfs4_exception exception = { };
3355 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003356
3357 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 do {
3359 err = nfs4_handle_exception(NFS_SERVER(dir),
3360 _nfs4_proc_mkdir(dir, dentry, sattr),
3361 &exception);
3362 } while (exception.retry);
3363 return err;
3364}
3365
3366static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003367 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368{
3369 struct inode *dir = dentry->d_inode;
3370 struct nfs4_readdir_arg args = {
3371 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003372 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 .pgbase = 0,
3374 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003375 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003376 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 };
3378 struct nfs4_readdir_res res;
3379 struct rpc_message msg = {
3380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3381 .rpc_argp = &args,
3382 .rpc_resp = &res,
3383 .rpc_cred = cred,
3384 };
3385 int status;
3386
Harvey Harrison3110ff82008-05-02 13:42:44 -07003387 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003388 dentry->d_parent->d_name.name,
3389 dentry->d_name.name,
3390 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003391 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003393 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 -05003394 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003395 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003396 status += args.pgbase;
3397 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003398
3399 nfs_invalidate_atime(dir);
3400
Harvey Harrison3110ff82008-05-02 13:42:44 -07003401 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 return status;
3403}
3404
3405static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003406 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
3408 struct nfs4_exception exception = { };
3409 int err;
3410 do {
3411 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3412 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003413 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 &exception);
3415 } while (exception.retry);
3416 return err;
3417}
3418
3419static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3420 struct iattr *sattr, dev_t rdev)
3421{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003422 struct nfs4_createdata *data;
3423 int mode = sattr->ia_mode;
3424 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003426 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3427 if (data == NULL)
3428 goto out;
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003431 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003433 data->arg.ftype = NF4BLK;
3434 data->arg.u.device.specdata1 = MAJOR(rdev);
3435 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 }
3437 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003438 data->arg.ftype = NF4CHR;
3439 data->arg.u.device.specdata1 = MAJOR(rdev);
3440 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003441 } else if (!S_ISSOCK(mode)) {
3442 status = -EINVAL;
3443 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003446 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003447out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003448 nfs4_free_createdata(data);
3449out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return status;
3451}
3452
3453static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3454 struct iattr *sattr, dev_t rdev)
3455{
3456 struct nfs4_exception exception = { };
3457 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003458
3459 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 do {
3461 err = nfs4_handle_exception(NFS_SERVER(dir),
3462 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3463 &exception);
3464 } while (exception.retry);
3465 return err;
3466}
3467
3468static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3469 struct nfs_fsstat *fsstat)
3470{
3471 struct nfs4_statfs_arg args = {
3472 .fh = fhandle,
3473 .bitmask = server->attr_bitmask,
3474 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003475 struct nfs4_statfs_res res = {
3476 .fsstat = fsstat,
3477 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 struct rpc_message msg = {
3479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3480 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003481 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 };
3483
Trond Myklebust0e574af2005-10-27 22:12:38 -04003484 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003485 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
3488static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3489{
3490 struct nfs4_exception exception = { };
3491 int err;
3492 do {
3493 err = nfs4_handle_exception(server,
3494 _nfs4_proc_statfs(server, fhandle, fsstat),
3495 &exception);
3496 } while (exception.retry);
3497 return err;
3498}
3499
3500static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3501 struct nfs_fsinfo *fsinfo)
3502{
3503 struct nfs4_fsinfo_arg args = {
3504 .fh = fhandle,
3505 .bitmask = server->attr_bitmask,
3506 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003507 struct nfs4_fsinfo_res res = {
3508 .fsinfo = fsinfo,
3509 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 struct rpc_message msg = {
3511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3512 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003513 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 };
3515
Bryan Schumaker7c513052011-03-24 17:12:24 +00003516 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
3519static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3520{
3521 struct nfs4_exception exception = { };
3522 int err;
3523
3524 do {
3525 err = nfs4_handle_exception(server,
3526 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3527 &exception);
3528 } while (exception.retry);
3529 return err;
3530}
3531
3532static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3533{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003534 int error;
3535
Trond Myklebust0e574af2005-10-27 22:12:38 -04003536 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003537 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003538 if (error == 0) {
3539 /* block layout checks this! */
3540 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003541 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003542 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003543
3544 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
3547static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3548 struct nfs_pathconf *pathconf)
3549{
3550 struct nfs4_pathconf_arg args = {
3551 .fh = fhandle,
3552 .bitmask = server->attr_bitmask,
3553 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003554 struct nfs4_pathconf_res res = {
3555 .pathconf = pathconf,
3556 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 struct rpc_message msg = {
3558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3559 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003560 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 };
3562
3563 /* None of the pathconf attributes are mandatory to implement */
3564 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3565 memset(pathconf, 0, sizeof(*pathconf));
3566 return 0;
3567 }
3568
Trond Myklebust0e574af2005-10-27 22:12:38 -04003569 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003570 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
3573static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3574 struct nfs_pathconf *pathconf)
3575{
3576 struct nfs4_exception exception = { };
3577 int err;
3578
3579 do {
3580 err = nfs4_handle_exception(server,
3581 _nfs4_proc_pathconf(server, fhandle, pathconf),
3582 &exception);
3583 } while (exception.retry);
3584 return err;
3585}
3586
Benny Halevyd20581a2011-05-22 19:52:03 +03003587void __nfs4_read_done_cb(struct nfs_read_data *data)
3588{
Fred Isamancd841602012-04-20 14:47:44 -04003589 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003590}
3591
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003592static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
Fred Isamancd841602012-04-20 14:47:44 -04003594 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003596 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003597 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003598 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003600
Benny Halevyd20581a2011-05-22 19:52:03 +03003601 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003603 renew_lease(server, data->timestamp);
3604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003607static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3608{
3609
3610 dprintk("--> %s\n", __func__);
3611
3612 if (!nfs4_sequence_done(task, &data->res.seq_res))
3613 return -EAGAIN;
3614
Benny Halevyd20581a2011-05-22 19:52:03 +03003615 return data->read_done_cb ? data->read_done_cb(task, data) :
3616 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003617}
3618
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003619static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003622 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003623 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003624 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003627static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3628{
Fred Isamancd841602012-04-20 14:47:44 -04003629 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003630 &data->args.seq_args,
3631 &data->res.seq_res,
3632 task))
3633 return;
3634 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635}
3636
Fred Isamanb029bc92011-03-03 15:13:42 +00003637static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638{
Fred Isamancd841602012-04-20 14:47:44 -04003639 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003641 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003642 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003643 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003645 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003647 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003648 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650}
3651
Fred Isamanb029bc92011-03-03 15:13:42 +00003652static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3653{
3654 if (!nfs4_sequence_done(task, &data->res.seq_res))
3655 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003656 return data->write_done_cb ? data->write_done_cb(task, data) :
3657 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003658}
3659
Trond Myklebust5a37f852012-04-28 14:55:16 -04003660static
3661bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003662{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003663 const struct nfs_pgio_header *hdr = data->header;
3664
3665 /* Don't request attributes for pNFS or O_DIRECT writes */
3666 if (data->ds_clp != NULL || hdr->dreq != NULL)
3667 return false;
3668 /* Otherwise, request attributes if and only if we don't hold
3669 * a delegation
3670 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003671 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003672}
Fred Isamana69aef12011-03-03 15:13:47 +00003673
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003674static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675{
Fred Isamancd841602012-04-20 14:47:44 -04003676 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003677
Trond Myklebust5a37f852012-04-28 14:55:16 -04003678 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003679 data->args.bitmask = NULL;
3680 data->res.fattr = NULL;
3681 } else
3682 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003683
Fred Isamanb029bc92011-03-03 15:13:42 +00003684 if (!data->write_done_cb)
3685 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003686 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 data->timestamp = jiffies;
3688
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003689 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003690 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}
3692
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003693static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3694{
Fred Isamancd841602012-04-20 14:47:44 -04003695 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003696 &data->args.seq_args,
3697 &data->res.seq_res,
3698 task))
3699 return;
3700 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702
Fred Isaman0b7c0152012-04-20 14:47:39 -04003703static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3706 &data->args.seq_args,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003707 &data->res.seq_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 return;
3710 rpc_call_start(task);
Trond Myklebust14516c32010-07-31 14:29:06 -04003711}
3712
Fred Isaman0b7c0152012-04-20 14:47:39 -04003713static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
3715 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003716
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003717 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003718 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003719 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722}
3723
Fred Isaman0b7c0152012-04-20 14:47:39 -04003724static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003725{
3726 if (!nfs4_sequence_done(task, &data->res.seq_res))
3727 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003728 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003729}
3730
Fred Isaman0b7c0152012-04-20 14:47:39 -04003731static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003733 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003734
Fred Isaman0b7c0152012-04-20 14:47:39 -04003735 if (data->commit_done_cb == NULL)
3736 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003737 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003738 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003739 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740}
3741
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003742struct nfs4_renewdata {
3743 struct nfs_client *client;
3744 unsigned long timestamp;
3745};
3746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747/*
3748 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3749 * standalone procedure for queueing an asynchronous RENEW.
3750 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003751static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003752{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003753 struct nfs4_renewdata *data = calldata;
3754 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003755
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003756 if (atomic_read(&clp->cl_count) > 1)
3757 nfs4_schedule_state_renewal(clp);
3758 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003759 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003760}
3761
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003762static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003764 struct nfs4_renewdata *data = calldata;
3765 struct nfs_client *clp = data->client;
3766 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
3768 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003769 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003770 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3771 return;
3772 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003773 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003774 return;
3775 }
3776 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003778 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779}
3780
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003781static const struct rpc_call_ops nfs4_renew_ops = {
3782 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003783 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003784};
3785
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003786static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
3788 struct rpc_message msg = {
3789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3790 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003791 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003793 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003795 if (renew_flags == 0)
3796 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003797 if (!atomic_inc_not_zero(&clp->cl_count))
3798 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003799 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003800 if (data == NULL)
3801 return -ENOMEM;
3802 data->client = clp;
3803 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003805 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806}
3807
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003808static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809{
3810 struct rpc_message msg = {
3811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3812 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003813 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 };
3815 unsigned long now = jiffies;
3816 int status;
3817
3818 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3819 if (status < 0)
3820 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003821 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 return 0;
3823}
3824
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003825static inline int nfs4_server_supports_acls(struct nfs_server *server)
3826{
3827 return (server->caps & NFS_CAP_ACLS)
3828 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3829 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3830}
3831
Trond Myklebust21f498c2012-08-24 10:59:25 -04003832/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3833 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003834 * the stack.
3835 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003836#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003837
Neil Hormane9e3d722011-03-04 19:26:03 -05003838static int buf_to_pages_noslab(const void *buf, size_t buflen,
3839 struct page **pages, unsigned int *pgbase)
3840{
3841 struct page *newpage, **spages;
3842 int rc = 0;
3843 size_t len;
3844 spages = pages;
3845
3846 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003847 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003848 newpage = alloc_page(GFP_KERNEL);
3849
3850 if (newpage == NULL)
3851 goto unwind;
3852 memcpy(page_address(newpage), buf, len);
3853 buf += len;
3854 buflen -= len;
3855 *pages++ = newpage;
3856 rc++;
3857 } while (buflen != 0);
3858
3859 return rc;
3860
3861unwind:
3862 for(; rc > 0; rc--)
3863 __free_page(spages[rc-1]);
3864 return -ENOMEM;
3865}
3866
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003867struct nfs4_cached_acl {
3868 int cached;
3869 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003870 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003871};
3872
3873static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003874{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003875 struct nfs_inode *nfsi = NFS_I(inode);
3876
3877 spin_lock(&inode->i_lock);
3878 kfree(nfsi->nfs4_acl);
3879 nfsi->nfs4_acl = acl;
3880 spin_unlock(&inode->i_lock);
3881}
3882
3883static void nfs4_zap_acl_attr(struct inode *inode)
3884{
3885 nfs4_set_cached_acl(inode, NULL);
3886}
3887
3888static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3889{
3890 struct nfs_inode *nfsi = NFS_I(inode);
3891 struct nfs4_cached_acl *acl;
3892 int ret = -ENOENT;
3893
3894 spin_lock(&inode->i_lock);
3895 acl = nfsi->nfs4_acl;
3896 if (acl == NULL)
3897 goto out;
3898 if (buf == NULL) /* user is just asking for length */
3899 goto out_len;
3900 if (acl->cached == 0)
3901 goto out;
3902 ret = -ERANGE; /* see getxattr(2) man page */
3903 if (acl->len > buflen)
3904 goto out;
3905 memcpy(buf, acl->data, acl->len);
3906out_len:
3907 ret = acl->len;
3908out:
3909 spin_unlock(&inode->i_lock);
3910 return ret;
3911}
3912
Sachin Prabhu5794d212012-04-17 14:36:40 +01003913static 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 +00003914{
3915 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003916 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003917
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003918 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003919 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003920 if (acl == NULL)
3921 goto out;
3922 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003923 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003924 } else {
3925 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3926 if (acl == NULL)
3927 goto out;
3928 acl->cached = 0;
3929 }
3930 acl->len = acl_len;
3931out:
3932 nfs4_set_cached_acl(inode, acl);
3933}
3934
Andy Adamsonbf118a32011-12-07 11:55:27 -05003935/*
3936 * The getxattr API returns the required buffer length when called with a
3937 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3938 * the required buf. On a NULL buf, we send a page of data to the server
3939 * guessing that the ACL request can be serviced by a page. If so, we cache
3940 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3941 * the cache. If not so, we throw away the page, and cache the required
3942 * length. The next getxattr call will then produce another round trip to
3943 * the server, this time with the input buf of the required size.
3944 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003945static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003946{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003947 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003948 struct nfs_getaclargs args = {
3949 .fh = NFS_FH(inode),
3950 .acl_pages = pages,
3951 .acl_len = buflen,
3952 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003953 struct nfs_getaclres res = {
3954 .acl_len = buflen,
3955 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003956 struct rpc_message msg = {
3957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3958 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003959 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003960 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003961 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3962 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003963
Andy Adamsonbf118a32011-12-07 11:55:27 -05003964 /* As long as we're doing a round trip to the server anyway,
3965 * let's be prepared for a page of acl data. */
3966 if (npages == 0)
3967 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003968 if (npages > ARRAY_SIZE(pages))
3969 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003970
Andy Adamsonbf118a32011-12-07 11:55:27 -05003971 for (i = 0; i < npages; i++) {
3972 pages[i] = alloc_page(GFP_KERNEL);
3973 if (!pages[i])
3974 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003975 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003976
3977 /* for decoding across pages */
3978 res.acl_scratch = alloc_page(GFP_KERNEL);
3979 if (!res.acl_scratch)
3980 goto out_free;
3981
Andy Adamsonbf118a32011-12-07 11:55:27 -05003982 args.acl_len = npages * PAGE_SIZE;
3983 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003984
Peng Taode040be2012-01-10 22:42:47 +08003985 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003986 __func__, buf, buflen, npages, args.acl_len);
3987 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3988 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003989 if (ret)
3990 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003991
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003992 /* Handle the case where the passed-in buffer is too short */
3993 if (res.acl_flags & NFS4_ACL_TRUNC) {
3994 /* Did the user only issue a request for the acl length? */
3995 if (buf == NULL)
3996 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003997 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003998 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003999 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004000 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4001 if (buf)
4002 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4003out_ok:
4004 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004005out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004006 for (i = 0; i < npages; i++)
4007 if (pages[i])
4008 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004009 if (res.acl_scratch)
4010 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004011 return ret;
4012}
4013
Trond Myklebust16b42892006-08-24 12:27:15 -04004014static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4015{
4016 struct nfs4_exception exception = { };
4017 ssize_t ret;
4018 do {
4019 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4020 if (ret >= 0)
4021 break;
4022 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4023 } while (exception.retry);
4024 return ret;
4025}
4026
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004027static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4028{
4029 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004030 int ret;
4031
4032 if (!nfs4_server_supports_acls(server))
4033 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004034 ret = nfs_revalidate_inode(server, inode);
4035 if (ret < 0)
4036 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004037 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4038 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004039 ret = nfs4_read_cached_acl(inode, buf, buflen);
4040 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004041 /* -ENOENT is returned if there is no ACL or if there is an ACL
4042 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004043 return ret;
4044 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004045}
4046
Trond Myklebust16b42892006-08-24 12:27:15 -04004047static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004048{
4049 struct nfs_server *server = NFS_SERVER(inode);
4050 struct page *pages[NFS4ACL_MAXPAGES];
4051 struct nfs_setaclargs arg = {
4052 .fh = NFS_FH(inode),
4053 .acl_pages = pages,
4054 .acl_len = buflen,
4055 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004056 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004057 struct rpc_message msg = {
4058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4059 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004060 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004061 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004062 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004063 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004064
4065 if (!nfs4_server_supports_acls(server))
4066 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004067 if (npages > ARRAY_SIZE(pages))
4068 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004069 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4070 if (i < 0)
4071 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004072 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004073 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004074
4075 /*
4076 * Free each page after tx, so the only ref left is
4077 * held by the network stack
4078 */
4079 for (; i > 0; i--)
4080 put_page(pages[i-1]);
4081
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004082 /*
4083 * Acl update can result in inode attribute update.
4084 * so mark the attribute cache invalid.
4085 */
4086 spin_lock(&inode->i_lock);
4087 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4088 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004089 nfs_access_zap_cache(inode);
4090 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004091 return ret;
4092}
4093
Trond Myklebust16b42892006-08-24 12:27:15 -04004094static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4095{
4096 struct nfs4_exception exception = { };
4097 int err;
4098 do {
4099 err = nfs4_handle_exception(NFS_SERVER(inode),
4100 __nfs4_proc_set_acl(inode, buf, buflen),
4101 &exception);
4102 } while (exception.retry);
4103 return err;
4104}
4105
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004107nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004109 struct nfs_client *clp = server->nfs_client;
4110
4111 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 return 0;
4113 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004114 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004115 case -NFS4ERR_ADMIN_REVOKED:
4116 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004117 if (state == NULL)
4118 break;
4119 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004120 case -NFS4ERR_OPENMODE:
4121 if (state == NULL)
4122 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004123 nfs4_schedule_stateid_recovery(server, state);
4124 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004125 case -NFS4ERR_EXPIRED:
4126 if (state != NULL)
4127 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004129 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004130 nfs4_schedule_lease_recovery(clp);
4131 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004132#if defined(CONFIG_NFS_V4_1)
4133 case -NFS4ERR_BADSESSION:
4134 case -NFS4ERR_BADSLOT:
4135 case -NFS4ERR_BAD_HIGH_SLOT:
4136 case -NFS4ERR_DEADSESSION:
4137 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4138 case -NFS4ERR_SEQ_FALSE_RETRY:
4139 case -NFS4ERR_SEQ_MISORDERED:
4140 dprintk("%s ERROR %d, Reset session\n", __func__,
4141 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004142 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004143 task->tk_status = 0;
4144 return -EAGAIN;
4145#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004147 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004148 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004149 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4151 task->tk_status = 0;
4152 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004153 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 case -NFS4ERR_OLD_STATEID:
4155 task->tk_status = 0;
4156 return -EAGAIN;
4157 }
4158 task->tk_status = nfs4_map_errors(task->tk_status);
4159 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004160wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004161 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004162 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4163 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4164 task->tk_status = 0;
4165 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166}
4167
Chuck Leverf0920752012-05-21 22:45:41 -04004168static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4169 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004170{
4171 __be32 verf[2];
4172
Chuck Lever2c820d92012-05-21 22:45:33 -04004173 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4174 /* An impossible timestamp guarantees this value
4175 * will never match a generated boot time. */
4176 verf[0] = 0;
4177 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4178 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004179 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4180 verf[0] = (__be32)nn->boot_time.tv_sec;
4181 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004182 }
Chuck Levercd937102012-03-02 17:14:31 -05004183 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4184}
4185
Chuck Levere984a552012-09-14 17:24:21 -04004186static unsigned int
4187nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4188 char *buf, size_t len)
4189{
4190 unsigned int result;
4191
4192 rcu_read_lock();
4193 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4194 clp->cl_ipaddr,
4195 rpc_peeraddr2str(clp->cl_rpcclient,
4196 RPC_DISPLAY_ADDR),
4197 rpc_peeraddr2str(clp->cl_rpcclient,
4198 RPC_DISPLAY_PROTO));
4199 rcu_read_unlock();
4200 return result;
4201}
4202
4203static unsigned int
4204nfs4_init_uniform_client_string(const struct nfs_client *clp,
4205 char *buf, size_t len)
4206{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004207 char *nodename = clp->cl_rpcclient->cl_nodename;
4208
4209 if (nfs4_client_id_uniquifier[0] != '\0')
4210 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004211 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4212 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004213 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004214}
4215
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004216/**
4217 * nfs4_proc_setclientid - Negotiate client ID
4218 * @clp: state data structure
4219 * @program: RPC program for NFSv4 callback service
4220 * @port: IP port number for NFS4 callback service
4221 * @cred: RPC credential to use for this call
4222 * @res: where to place the result
4223 *
4224 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4225 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004226int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4227 unsigned short port, struct rpc_cred *cred,
4228 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
4230 nfs4_verifier sc_verifier;
4231 struct nfs4_setclientid setclientid = {
4232 .sc_verifier = &sc_verifier,
4233 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004234 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 };
4236 struct rpc_message msg = {
4237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4238 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004239 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004240 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004242 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Chuck Leverde734832012-07-11 16:30:50 -04004244 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004245 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004246 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4247 setclientid.sc_name_len =
4248 nfs4_init_uniform_client_string(clp,
4249 setclientid.sc_name,
4250 sizeof(setclientid.sc_name));
4251 else
4252 setclientid.sc_name_len =
4253 nfs4_init_nonuniform_client_string(clp,
4254 setclientid.sc_name,
4255 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004256 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004257 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004258 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004259 sizeof(setclientid.sc_netid),
4260 rpc_peeraddr2str(clp->cl_rpcclient,
4261 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004262 rcu_read_unlock();
4263 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004264 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 clp->cl_ipaddr, port >> 8, port & 255);
4266
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004267 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4268 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4269 setclientid.sc_name_len, setclientid.sc_name);
4270 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4271 dprintk("NFS reply setclientid: %d\n", status);
4272 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273}
4274
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004275/**
4276 * nfs4_proc_setclientid_confirm - Confirm client ID
4277 * @clp: state data structure
4278 * @res: result of a previous SETCLIENTID
4279 * @cred: RPC credential to use for this call
4280 *
4281 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4282 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004283int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004284 struct nfs4_setclientid_res *arg,
4285 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
4287 struct nfs_fsinfo fsinfo;
4288 struct rpc_message msg = {
4289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004290 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004292 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 };
4294 unsigned long now;
4295 int status;
4296
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004297 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4298 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4299 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004301 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 if (status == 0) {
4303 spin_lock(&clp->cl_lock);
4304 clp->cl_lease_time = fsinfo.lease_time * HZ;
4305 clp->cl_last_renewal = now;
4306 spin_unlock(&clp->cl_lock);
4307 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004308 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return status;
4310}
4311
Trond Myklebustfe650402006-01-03 09:55:18 +01004312struct nfs4_delegreturndata {
4313 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004314 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004315 struct nfs_fh fh;
4316 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004317 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004318 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004319 int rpc_status;
4320};
4321
Trond Myklebustfe650402006-01-03 09:55:18 +01004322static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4323{
4324 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004325
Trond Myklebust14516c32010-07-31 14:29:06 -04004326 if (!nfs4_sequence_done(task, &data->res.seq_res))
4327 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004328
Ricardo Labiaga79708862009-12-07 09:23:21 -05004329 switch (task->tk_status) {
4330 case -NFS4ERR_STALE_STATEID:
4331 case -NFS4ERR_EXPIRED:
4332 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004333 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004334 break;
4335 default:
4336 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4337 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004338 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004339 return;
4340 }
4341 }
4342 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004343}
4344
4345static void nfs4_delegreturn_release(void *calldata)
4346{
Trond Myklebustfe650402006-01-03 09:55:18 +01004347 kfree(calldata);
4348}
4349
Andy Adamson938e1012009-04-01 09:22:28 -04004350#if defined(CONFIG_NFS_V4_1)
4351static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4352{
4353 struct nfs4_delegreturndata *d_data;
4354
4355 d_data = (struct nfs4_delegreturndata *)data;
4356
Trond Myklebust035168a2010-06-16 09:52:26 -04004357 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004358 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004359 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004360 return;
4361 rpc_call_start(task);
4362}
4363#endif /* CONFIG_NFS_V4_1 */
4364
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004365static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004366#if defined(CONFIG_NFS_V4_1)
4367 .rpc_call_prepare = nfs4_delegreturn_prepare,
4368#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004369 .rpc_call_done = nfs4_delegreturn_done,
4370 .rpc_release = nfs4_delegreturn_release,
4371};
4372
Trond Myklebuste6f81072008-01-24 18:14:34 -05004373static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004374{
4375 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004376 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004377 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004378 struct rpc_message msg = {
4379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4380 .rpc_cred = cred,
4381 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004382 struct rpc_task_setup task_setup_data = {
4383 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004384 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004385 .callback_ops = &nfs4_delegreturn_ops,
4386 .flags = RPC_TASK_ASYNC,
4387 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004388 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004389
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004390 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004391 if (data == NULL)
4392 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004393 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004394 data->args.fhandle = &data->fh;
4395 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004396 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004397 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004398 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004399 data->res.fattr = &data->fattr;
4400 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004401 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004402 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004403 data->rpc_status = 0;
4404
Trond Myklebustc970aa82007-07-14 15:39:59 -04004405 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004406 msg.rpc_argp = &data->args;
4407 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004408 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004409 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004410 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004411 if (!issync)
4412 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004413 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004414 if (status != 0)
4415 goto out;
4416 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004417 if (status == 0)
4418 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4419 else
4420 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004421out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004422 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004423 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424}
4425
Trond Myklebuste6f81072008-01-24 18:14:34 -05004426int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
4428 struct nfs_server *server = NFS_SERVER(inode);
4429 struct nfs4_exception exception = { };
4430 int err;
4431 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004432 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 switch (err) {
4434 case -NFS4ERR_STALE_STATEID:
4435 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 case 0:
4437 return 0;
4438 }
4439 err = nfs4_handle_exception(server, err, &exception);
4440 } while (exception.retry);
4441 return err;
4442}
4443
4444#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4445#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4446
4447/*
4448 * sleep, with exponential backoff, and retry the LOCK operation.
4449 */
4450static unsigned long
4451nfs4_set_lock_task_retry(unsigned long timeout)
4452{
Jeff Laytond3103102011-12-01 22:44:39 +01004453 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 timeout <<= 1;
4455 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4456 return NFS4_LOCK_MAXTIMEOUT;
4457 return timeout;
4458}
4459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4461{
4462 struct inode *inode = state->inode;
4463 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004464 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004465 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004467 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004469 struct nfs_lockt_res res = {
4470 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 };
4472 struct rpc_message msg = {
4473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4474 .rpc_argp = &arg,
4475 .rpc_resp = &res,
4476 .rpc_cred = state->owner->so_cred,
4477 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 struct nfs4_lock_state *lsp;
4479 int status;
4480
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004481 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004482 status = nfs4_set_lock_state(state, request);
4483 if (status != 0)
4484 goto out;
4485 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004486 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004487 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004488 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004489 switch (status) {
4490 case 0:
4491 request->fl_type = F_UNLCK;
4492 break;
4493 case -NFS4ERR_DENIED:
4494 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004496 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004497out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 return status;
4499}
4500
4501static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4502{
4503 struct nfs4_exception exception = { };
4504 int err;
4505
4506 do {
4507 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4508 _nfs4_proc_getlk(state, cmd, request),
4509 &exception);
4510 } while (exception.retry);
4511 return err;
4512}
4513
4514static int do_vfs_lock(struct file *file, struct file_lock *fl)
4515{
4516 int res = 0;
4517 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4518 case FL_POSIX:
4519 res = posix_lock_file_wait(file, fl);
4520 break;
4521 case FL_FLOCK:
4522 res = flock_lock_file_wait(file, fl);
4523 break;
4524 default:
4525 BUG();
4526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 return res;
4528}
4529
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004530struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004531 struct nfs_locku_args arg;
4532 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004533 struct nfs4_lock_state *lsp;
4534 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004535 struct file_lock fl;
4536 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004537 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004538};
4539
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004540static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4541 struct nfs_open_context *ctx,
4542 struct nfs4_lock_state *lsp,
4543 struct nfs_seqid *seqid)
4544{
4545 struct nfs4_unlockdata *p;
4546 struct inode *inode = lsp->ls_state->inode;
4547
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004548 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004549 if (p == NULL)
4550 return NULL;
4551 p->arg.fh = NFS_FH(inode);
4552 p->arg.fl = &p->fl;
4553 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004554 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004555 p->arg.stateid = &lsp->ls_stateid;
4556 p->lsp = lsp;
4557 atomic_inc(&lsp->ls_count);
4558 /* Ensure we don't close file until we're done freeing locks! */
4559 p->ctx = get_nfs_open_context(ctx);
4560 memcpy(&p->fl, fl, sizeof(p->fl));
4561 p->server = NFS_SERVER(inode);
4562 return p;
4563}
4564
Trond Myklebust06f814a2006-01-03 09:55:07 +01004565static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004566{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004567 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004568 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004569 nfs4_put_lock_state(calldata->lsp);
4570 put_nfs_open_context(calldata->ctx);
4571 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004572}
4573
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004574static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004575{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004576 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004577
Trond Myklebust14516c32010-07-31 14:29:06 -04004578 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4579 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004580 switch (task->tk_status) {
4581 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004582 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4583 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004584 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004585 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004586 case -NFS4ERR_BAD_STATEID:
4587 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004588 case -NFS4ERR_STALE_STATEID:
4589 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004590 break;
4591 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004592 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004593 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004594 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004595 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004596}
4597
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004598static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004599{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004600 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004602 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004603 return;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004604 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004605 /* Note: exit _without_ running nfs4_locku_done */
4606 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004607 return;
4608 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004609 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004610 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004611 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004612 &calldata->res.seq_res,
4613 task) != 0)
4614 nfs_release_seqid(calldata->arg.seqid);
4615 else
4616 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617}
4618
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004619static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004620 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004621 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004622 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004623};
4624
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004625static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4626 struct nfs_open_context *ctx,
4627 struct nfs4_lock_state *lsp,
4628 struct nfs_seqid *seqid)
4629{
4630 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004631 struct rpc_message msg = {
4632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4633 .rpc_cred = ctx->cred,
4634 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004635 struct rpc_task_setup task_setup_data = {
4636 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004637 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004638 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004639 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004640 .flags = RPC_TASK_ASYNC,
4641 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004642
Frank Filz137d6ac2007-07-09 15:32:29 -07004643 /* Ensure this is an unlock - when canceling a lock, the
4644 * canceled lock is passed in, and it won't be an unlock.
4645 */
4646 fl->fl_type = F_UNLCK;
4647
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004648 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4649 if (data == NULL) {
4650 nfs_free_seqid(seqid);
4651 return ERR_PTR(-ENOMEM);
4652 }
4653
Trond Myklebust9d12b212012-01-17 22:04:25 -05004654 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004655 msg.rpc_argp = &data->arg;
4656 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004657 task_setup_data.callback_data = data;
4658 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004659}
4660
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4662{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004663 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004664 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004665 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004666 struct rpc_task *task;
4667 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004668 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Trond Myklebust9b073572006-06-29 16:38:34 -04004670 status = nfs4_set_lock_state(state, request);
4671 /* Unlock _before_ we do the RPC call */
4672 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004673 down_read(&nfsi->rwsem);
4674 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4675 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004676 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004677 }
4678 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004679 if (status != 0)
4680 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004681 /* Is this a delegated lock? */
4682 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004683 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004684 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004685 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004686 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004687 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004688 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004689 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004690 status = PTR_ERR(task);
4691 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004692 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004693 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004694 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004695out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004696 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004697 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698}
4699
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004700struct nfs4_lockdata {
4701 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004702 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004703 struct nfs4_lock_state *lsp;
4704 struct nfs_open_context *ctx;
4705 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004706 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004707 int rpc_status;
4708 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004709 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004710};
4711
4712static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004713 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4714 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004715{
4716 struct nfs4_lockdata *p;
4717 struct inode *inode = lsp->ls_state->inode;
4718 struct nfs_server *server = NFS_SERVER(inode);
4719
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004720 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004721 if (p == NULL)
4722 return NULL;
4723
4724 p->arg.fh = NFS_FH(inode);
4725 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004726 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004727 if (p->arg.open_seqid == NULL)
4728 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004729 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004730 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004731 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004732 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004733 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004734 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004735 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004736 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004737 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004738 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004739 atomic_inc(&lsp->ls_count);
4740 p->ctx = get_nfs_open_context(ctx);
4741 memcpy(&p->fl, fl, sizeof(p->fl));
4742 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004743out_free_seqid:
4744 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004745out_free:
4746 kfree(p);
4747 return NULL;
4748}
4749
4750static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4751{
4752 struct nfs4_lockdata *data = calldata;
4753 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Harvey Harrison3110ff82008-05-02 13:42:44 -07004755 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004756 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4757 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004758 /* Do we need to do an open_to_lock_owner? */
4759 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004760 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004761 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004762 data->arg.open_stateid = &state->stateid;
4763 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004764 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004765 } else
4766 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004767 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004768 if (nfs4_setup_sequence(data->server,
4769 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004770 &data->res.seq_res,
4771 task) == 0) {
4772 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004773 return;
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004774 }
4775 nfs_release_seqid(data->arg.open_seqid);
4776out_release_lock_seqid:
4777 nfs_release_seqid(data->arg.lock_seqid);
4778 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004779}
4780
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004781static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4782{
4783 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4784 nfs4_lock_prepare(task, calldata);
4785}
4786
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004787static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4788{
4789 struct nfs4_lockdata *data = calldata;
4790
Harvey Harrison3110ff82008-05-02 13:42:44 -07004791 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004792
Trond Myklebust14516c32010-07-31 14:29:06 -04004793 if (!nfs4_sequence_done(task, &data->res.seq_res))
4794 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004795
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004796 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004797 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004798 if (data->rpc_status == 0)
4799 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4800 else
4801 goto out;
4802 }
4803 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004804 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004805 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004806 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004807 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004808out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004809 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004810}
4811
4812static void nfs4_lock_release(void *calldata)
4813{
4814 struct nfs4_lockdata *data = calldata;
4815
Harvey Harrison3110ff82008-05-02 13:42:44 -07004816 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004817 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004818 if (data->cancelled != 0) {
4819 struct rpc_task *task;
4820 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4821 data->arg.lock_seqid);
4822 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004823 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004824 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004825 } else
4826 nfs_free_seqid(data->arg.lock_seqid);
4827 nfs4_put_lock_state(data->lsp);
4828 put_nfs_open_context(data->ctx);
4829 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004830 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004831}
4832
4833static const struct rpc_call_ops nfs4_lock_ops = {
4834 .rpc_call_prepare = nfs4_lock_prepare,
4835 .rpc_call_done = nfs4_lock_done,
4836 .rpc_release = nfs4_lock_release,
4837};
4838
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004839static const struct rpc_call_ops nfs4_recover_lock_ops = {
4840 .rpc_call_prepare = nfs4_recover_lock_prepare,
4841 .rpc_call_done = nfs4_lock_done,
4842 .rpc_release = nfs4_lock_release,
4843};
4844
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004845static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4846{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004847 switch (error) {
4848 case -NFS4ERR_ADMIN_REVOKED:
4849 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004850 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004851 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004852 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004853 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004854 break;
4855 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004856 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004857 case -NFS4ERR_EXPIRED:
4858 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004859 };
4860}
4861
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004862static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004863{
4864 struct nfs4_lockdata *data;
4865 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004866 struct rpc_message msg = {
4867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4868 .rpc_cred = state->owner->so_cred,
4869 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004870 struct rpc_task_setup task_setup_data = {
4871 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004872 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004873 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004874 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004875 .flags = RPC_TASK_ASYNC,
4876 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004877 int ret;
4878
Harvey Harrison3110ff82008-05-02 13:42:44 -07004879 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004880 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004881 fl->fl_u.nfs4_fl.owner,
4882 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004883 if (data == NULL)
4884 return -ENOMEM;
4885 if (IS_SETLKW(cmd))
4886 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004887 if (recovery_type > NFS_LOCK_NEW) {
4888 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004889 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004890 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4891 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004892 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004893 msg.rpc_argp = &data->arg;
4894 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004895 task_setup_data.callback_data = data;
4896 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004897 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004898 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004899 ret = nfs4_wait_for_completion_rpc_task(task);
4900 if (ret == 0) {
4901 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004902 if (ret)
4903 nfs4_handle_setlk_error(data->server, data->lsp,
4904 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004905 } else
4906 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004907 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004908 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004909 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910}
4911
4912static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4913{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004914 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004915 struct nfs4_exception exception = {
4916 .inode = state->inode,
4917 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004918 int err;
4919
4920 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004921 /* Cache the lock if possible... */
4922 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4923 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004924 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004925 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004926 break;
4927 nfs4_handle_exception(server, err, &exception);
4928 } while (exception.retry);
4929 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930}
4931
4932static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4933{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004934 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004935 struct nfs4_exception exception = {
4936 .inode = state->inode,
4937 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004938 int err;
4939
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004940 err = nfs4_set_lock_state(state, request);
4941 if (err != 0)
4942 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004943 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004944 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4945 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004946 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004947 switch (err) {
4948 default:
4949 goto out;
4950 case -NFS4ERR_GRACE:
4951 case -NFS4ERR_DELAY:
4952 nfs4_handle_exception(server, err, &exception);
4953 err = 0;
4954 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004955 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004956out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004957 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958}
4959
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004960#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004961/**
4962 * nfs41_check_expired_locks - possibly free a lock stateid
4963 *
4964 * @state: NFSv4 state for an inode
4965 *
4966 * Returns NFS_OK if recovery for this stateid is now finished.
4967 * Otherwise a negative NFS4ERR value is returned.
4968 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004969static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004970{
Chuck Levereb64cf92012-07-11 16:30:05 -04004971 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004972 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004973 struct nfs_server *server = NFS_SERVER(state->inode);
4974
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004975 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004976 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004977 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4978 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004979 /* Free the stateid unless the server
4980 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004981 if (status != -NFS4ERR_BAD_STATEID)
4982 nfs41_free_stateid(server,
4983 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004984 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004985 ret = status;
4986 }
4987 }
4988 };
4989
4990 return ret;
4991}
4992
4993static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4994{
4995 int status = NFS_OK;
4996
4997 if (test_bit(LK_STATE_IN_USE, &state->flags))
4998 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004999 if (status != NFS_OK)
5000 status = nfs4_lock_expired(state, request);
5001 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005002}
5003#endif
5004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5006{
Trond Myklebust19e03c52008-12-23 15:21:44 -05005007 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005008 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005009 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005011 if ((fl_flags & FL_POSIX) &&
5012 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5013 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005014 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005015 status = nfs4_set_lock_state(state, request);
5016 if (status != 0)
5017 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005018 request->fl_flags |= FL_ACCESS;
5019 status = do_vfs_lock(request->fl_file, request);
5020 if (status < 0)
5021 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005022 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005023 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005024 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005025 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005026 request->fl_flags = fl_flags & ~FL_SLEEP;
5027 status = do_vfs_lock(request->fl_file, request);
5028 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005029 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005030 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005031 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04005032 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005033 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005034 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005035 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005036 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5037 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005038out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005039 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005040out:
5041 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 return status;
5043}
5044
5045static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5046{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005047 struct nfs4_exception exception = {
5048 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005049 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005050 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 int err;
5052
5053 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005054 err = _nfs4_proc_setlk(state, cmd, request);
5055 if (err == -NFS4ERR_DENIED)
5056 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005058 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 } while (exception.retry);
5060 return err;
5061}
5062
5063static int
5064nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5065{
5066 struct nfs_open_context *ctx;
5067 struct nfs4_state *state;
5068 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5069 int status;
5070
5071 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005072 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 state = ctx->state;
5074
5075 if (request->fl_start < 0 || request->fl_end < 0)
5076 return -EINVAL;
5077
Trond Myklebustd9531262009-07-21 19:22:38 -04005078 if (IS_GETLK(cmd)) {
5079 if (state != NULL)
5080 return nfs4_proc_getlk(state, F_GETLK, request);
5081 return 0;
5082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
5084 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5085 return -EINVAL;
5086
Trond Myklebustd9531262009-07-21 19:22:38 -04005087 if (request->fl_type == F_UNLCK) {
5088 if (state != NULL)
5089 return nfs4_proc_unlck(state, cmd, request);
5090 return 0;
5091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
Trond Myklebustd9531262009-07-21 19:22:38 -04005093 if (state == NULL)
5094 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005095 /*
5096 * Don't rely on the VFS having checked the file open mode,
5097 * since it won't do this for flock() locks.
5098 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005099 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005100 case F_RDLCK:
5101 if (!(filp->f_mode & FMODE_READ))
5102 return -EBADF;
5103 break;
5104 case F_WRLCK:
5105 if (!(filp->f_mode & FMODE_WRITE))
5106 return -EBADF;
5107 }
5108
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 do {
5110 status = nfs4_proc_setlk(state, cmd, request);
5111 if ((status != -EAGAIN) || IS_SETLK(cmd))
5112 break;
5113 timeout = nfs4_set_lock_task_retry(timeout);
5114 status = -ERESTARTSYS;
5115 if (signalled())
5116 break;
5117 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 return status;
5119}
5120
Trond Myklebust888e6942005-11-04 15:38:11 -05005121int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5122{
5123 struct nfs_server *server = NFS_SERVER(state->inode);
5124 struct nfs4_exception exception = { };
5125 int err;
5126
5127 err = nfs4_set_lock_state(state, fl);
5128 if (err != 0)
5129 goto out;
5130 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005131 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005132 switch (err) {
5133 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005134 printk(KERN_ERR "NFS: %s: unhandled error "
5135 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005136 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005137 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005138 goto out;
5139 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005140 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07005141 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005142 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005143 nfs4_schedule_lease_recovery(server->nfs_client);
5144 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005145 case -NFS4ERR_BADSESSION:
5146 case -NFS4ERR_BADSLOT:
5147 case -NFS4ERR_BAD_HIGH_SLOT:
5148 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5149 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04005150 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005151 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005152 case -ERESTARTSYS:
5153 /*
5154 * The show must go on: exit, but mark the
5155 * stateid as needing recovery.
5156 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005157 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005158 case -NFS4ERR_ADMIN_REVOKED:
5159 case -NFS4ERR_BAD_STATEID:
5160 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005161 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005162 err = 0;
5163 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04005164 case -EKEYEXPIRED:
5165 /*
5166 * User RPCSEC_GSS context has expired.
5167 * We cannot recover this stateid now, so
5168 * skip it and allow recovery thread to
5169 * proceed.
5170 */
5171 err = 0;
5172 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07005173 case -ENOMEM:
5174 case -NFS4ERR_DENIED:
5175 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5176 err = 0;
5177 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005178 case -NFS4ERR_DELAY:
5179 break;
5180 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005181 err = nfs4_handle_exception(server, err, &exception);
5182 } while (exception.retry);
5183out:
5184 return err;
5185}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005186
Trond Myklebustcf470c32012-03-07 13:49:12 -05005187struct nfs_release_lockowner_data {
5188 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005189 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005190 struct nfs_release_lockowner_args args;
5191};
5192
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005193static void nfs4_release_lockowner_release(void *calldata)
5194{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005195 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005196 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005197 kfree(calldata);
5198}
5199
Trond Myklebust17280172012-03-11 13:11:00 -04005200static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005201 .rpc_release = nfs4_release_lockowner_release,
5202};
5203
Trond Myklebustcf470c32012-03-07 13:49:12 -05005204int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005205{
5206 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005207 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005208 struct rpc_message msg = {
5209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5210 };
5211
5212 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005213 return -EINVAL;
5214 data = kmalloc(sizeof(*data), GFP_NOFS);
5215 if (!data)
5216 return -ENOMEM;
5217 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005218 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005219 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5220 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5221 data->args.lock_owner.s_dev = server->s_dev;
5222 msg.rpc_argp = &data->args;
5223 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5224 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005225}
5226
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005227#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5228
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005229static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5230 const void *buf, size_t buflen,
5231 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005232{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005233 if (strcmp(key, "") != 0)
5234 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005235
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005236 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005237}
5238
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005239static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5240 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005241{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005242 if (strcmp(key, "") != 0)
5243 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005244
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005245 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005246}
5247
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005248static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5249 size_t list_len, const char *name,
5250 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005251{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005252 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005253
J. Bruce Fields096455a2006-03-20 23:23:42 -05005254 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5255 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005256
5257 if (list && len <= list_len)
5258 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005259 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005260}
5261
Andy Adamson533eb462011-06-13 18:25:56 -04005262/*
5263 * nfs_fhget will use either the mounted_on_fileid or the fileid
5264 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005265static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5266{
Andy Adamson533eb462011-06-13 18:25:56 -04005267 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5268 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5269 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005270 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005271 return;
5272
5273 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005274 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005275 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5276 fattr->nlink = 2;
5277}
5278
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005279static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5280 const struct qstr *name,
5281 struct nfs4_fs_locations *fs_locations,
5282 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005283{
5284 struct nfs_server *server = NFS_SERVER(dir);
5285 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005286 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005287 };
5288 struct nfs4_fs_locations_arg args = {
5289 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005290 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005291 .page = page,
5292 .bitmask = bitmask,
5293 };
Benny Halevy22958462009-04-01 09:22:02 -04005294 struct nfs4_fs_locations_res res = {
5295 .fs_locations = fs_locations,
5296 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005297 struct rpc_message msg = {
5298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5299 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005300 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005301 };
5302 int status;
5303
Harvey Harrison3110ff82008-05-02 13:42:44 -07005304 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005305
5306 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5307 * is not supported */
5308 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5309 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5310 else
5311 bitmask[0] |= FATTR4_WORD0_FILEID;
5312
Trond Myklebustc228fd32007-01-13 02:28:11 -05005313 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005314 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005315 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005316 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005317 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005318 return status;
5319}
5320
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005321int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5322 const struct qstr *name,
5323 struct nfs4_fs_locations *fs_locations,
5324 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005325{
5326 struct nfs4_exception exception = { };
5327 int err;
5328 do {
5329 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005330 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005331 &exception);
5332 } while (exception.retry);
5333 return err;
5334}
5335
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005336static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5337{
5338 int status;
5339 struct nfs4_secinfo_arg args = {
5340 .dir_fh = NFS_FH(dir),
5341 .name = name,
5342 };
5343 struct nfs4_secinfo_res res = {
5344 .flavors = flavors,
5345 };
5346 struct rpc_message msg = {
5347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5348 .rpc_argp = &args,
5349 .rpc_resp = &res,
5350 };
5351
5352 dprintk("NFS call secinfo %s\n", name->name);
5353 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5354 dprintk("NFS reply secinfo: %d\n", status);
5355 return status;
5356}
5357
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005358int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5359 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005360{
5361 struct nfs4_exception exception = { };
5362 int err;
5363 do {
5364 err = nfs4_handle_exception(NFS_SERVER(dir),
5365 _nfs4_proc_secinfo(dir, name, flavors),
5366 &exception);
5367 } while (exception.retry);
5368 return err;
5369}
5370
Andy Adamson557134a2009-04-01 09:21:53 -04005371#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005372/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005373 * Check the exchange flags returned by the server for invalid flags, having
5374 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5375 * DS flags set.
5376 */
5377static int nfs4_check_cl_exchange_flags(u32 flags)
5378{
5379 if (flags & ~EXCHGID4_FLAG_MASK_R)
5380 goto out_inval;
5381 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5382 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5383 goto out_inval;
5384 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5385 goto out_inval;
5386 return NFS_OK;
5387out_inval:
5388 return -NFS4ERR_INVAL;
5389}
5390
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005391static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005392nfs41_same_server_scope(struct nfs41_server_scope *a,
5393 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005394{
5395 if (a->server_scope_sz == b->server_scope_sz &&
5396 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5397 return true;
5398
5399 return false;
5400}
5401
Andy Adamson357f54d2010-12-14 10:11:57 -05005402/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005403 * nfs4_proc_bind_conn_to_session()
5404 *
5405 * The 4.1 client currently uses the same TCP connection for the
5406 * fore and backchannel.
5407 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005408int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005409{
5410 int status;
5411 struct nfs41_bind_conn_to_session_res res;
5412 struct rpc_message msg = {
5413 .rpc_proc =
5414 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5415 .rpc_argp = clp,
5416 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005417 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005418 };
5419
5420 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005421
5422 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5423 if (unlikely(res.session == NULL)) {
5424 status = -ENOMEM;
5425 goto out;
5426 }
5427
5428 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5429 if (status == 0) {
5430 if (memcmp(res.session->sess_id.data,
5431 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5432 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5433 status = -EIO;
5434 goto out_session;
5435 }
5436 if (res.dir != NFS4_CDFS4_BOTH) {
5437 dprintk("NFS: %s: Unexpected direction from server\n",
5438 __func__);
5439 status = -EIO;
5440 goto out_session;
5441 }
5442 if (res.use_conn_in_rdma_mode) {
5443 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5444 __func__);
5445 status = -EIO;
5446 goto out_session;
5447 }
5448 }
5449out_session:
5450 kfree(res.session);
5451out:
5452 dprintk("<-- %s status= %d\n", __func__, status);
5453 return status;
5454}
5455
5456/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005457 * nfs4_proc_exchange_id()
5458 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005459 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5460 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005461 * Since the clientid has expired, all compounds using sessions
5462 * associated with the stale clientid will be returning
5463 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5464 * be in some phase of session reset.
5465 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005466int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005467{
5468 nfs4_verifier verifier;
5469 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005470 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005471 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005472 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005473 };
5474 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005475 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005476 };
5477 int status;
5478 struct rpc_message msg = {
5479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5480 .rpc_argp = &args,
5481 .rpc_resp = &res,
5482 .rpc_cred = cred,
5483 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005484
Chuck Leverf0920752012-05-21 22:45:41 -04005485 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005486 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5487 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005488 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5489 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5490 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005491
Chuck Leveracdeb692012-05-21 22:46:16 -04005492 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005493 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005494 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005495 status = -ENOMEM;
5496 goto out;
5497 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005498
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005499 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005500 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005501 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005502 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005503 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005504 }
5505
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005506 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005507 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005508 status = -ENOMEM;
5509 goto out_server_scope;
5510 }
5511
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005512 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005513 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005514 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005515
Chuck Lever177313f2012-05-21 22:44:58 -04005516 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005517 clp->cl_clientid = res.clientid;
5518 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5519 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5520 clp->cl_seqid = res.seqid;
5521
Chuck Leveracdeb692012-05-21 22:46:16 -04005522 kfree(clp->cl_serverowner);
5523 clp->cl_serverowner = res.server_owner;
5524 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005525
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005526 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005527 kfree(clp->cl_implid);
5528 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005529
Chuck Lever177313f2012-05-21 22:44:58 -04005530 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005531 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005532 res.server_scope)) {
5533 dprintk("%s: server_scope mismatch detected\n",
5534 __func__);
5535 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005536 kfree(clp->cl_serverscope);
5537 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005538 }
5539
Chuck Lever177313f2012-05-21 22:44:58 -04005540 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005541 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005542 goto out;
5543 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005544 } else
5545 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005546
Chuck Leveracdeb692012-05-21 22:46:16 -04005547out_server_owner:
5548 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005549out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005550 kfree(res.server_scope);
5551out:
Chuck Lever177313f2012-05-21 22:44:58 -04005552 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005553 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005554 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005555 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005556 clp->cl_implid->date.seconds,
5557 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005558 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005559 return status;
5560}
5561
Trond Myklebust66245532012-05-25 17:18:09 -04005562static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5563 struct rpc_cred *cred)
5564{
5565 struct rpc_message msg = {
5566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5567 .rpc_argp = clp,
5568 .rpc_cred = cred,
5569 };
5570 int status;
5571
5572 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5573 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005574 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005575 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5576 return status;
5577}
5578
5579static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5580 struct rpc_cred *cred)
5581{
5582 unsigned int loop;
5583 int ret;
5584
5585 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5586 ret = _nfs4_proc_destroy_clientid(clp, cred);
5587 switch (ret) {
5588 case -NFS4ERR_DELAY:
5589 case -NFS4ERR_CLIENTID_BUSY:
5590 ssleep(1);
5591 break;
5592 default:
5593 return ret;
5594 }
5595 }
5596 return 0;
5597}
5598
5599int nfs4_destroy_clientid(struct nfs_client *clp)
5600{
5601 struct rpc_cred *cred;
5602 int ret = 0;
5603
5604 if (clp->cl_mvops->minor_version < 1)
5605 goto out;
5606 if (clp->cl_exchange_flags == 0)
5607 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005608 if (clp->cl_preserve_clid)
5609 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005610 cred = nfs4_get_exchange_id_cred(clp);
5611 ret = nfs4_proc_destroy_clientid(clp, cred);
5612 if (cred)
5613 put_rpccred(cred);
5614 switch (ret) {
5615 case 0:
5616 case -NFS4ERR_STALE_CLIENTID:
5617 clp->cl_exchange_flags = 0;
5618 }
5619out:
5620 return ret;
5621}
5622
Andy Adamson2050f0c2009-04-01 09:22:30 -04005623struct nfs4_get_lease_time_data {
5624 struct nfs4_get_lease_time_args *args;
5625 struct nfs4_get_lease_time_res *res;
5626 struct nfs_client *clp;
5627};
5628
5629static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5630 void *calldata)
5631{
5632 int ret;
5633 struct nfs4_get_lease_time_data *data =
5634 (struct nfs4_get_lease_time_data *)calldata;
5635
5636 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005637 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005638 /* just setup sequence, do not trigger session recovery
5639 since we're invoked within one */
5640 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005641 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005642 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005643
Trond Myklebustd3edcf92012-10-15 13:14:43 -04005644 if (ret != -EAGAIN)
5645 rpc_call_start(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005646 dprintk("<-- %s\n", __func__);
5647}
5648
5649/*
5650 * Called from nfs4_state_manager thread for session setup, so don't recover
5651 * from sequence operation or clientid errors.
5652 */
5653static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5654{
5655 struct nfs4_get_lease_time_data *data =
5656 (struct nfs4_get_lease_time_data *)calldata;
5657
5658 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005659 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5660 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005661 switch (task->tk_status) {
5662 case -NFS4ERR_DELAY:
5663 case -NFS4ERR_GRACE:
5664 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5665 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5666 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005667 /* fall through */
5668 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005669 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005670 return;
5671 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005672 dprintk("<-- %s\n", __func__);
5673}
5674
Trond Myklebust17280172012-03-11 13:11:00 -04005675static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005676 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5677 .rpc_call_done = nfs4_get_lease_time_done,
5678};
5679
5680int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5681{
5682 struct rpc_task *task;
5683 struct nfs4_get_lease_time_args args;
5684 struct nfs4_get_lease_time_res res = {
5685 .lr_fsinfo = fsinfo,
5686 };
5687 struct nfs4_get_lease_time_data data = {
5688 .args = &args,
5689 .res = &res,
5690 .clp = clp,
5691 };
5692 struct rpc_message msg = {
5693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5694 .rpc_argp = &args,
5695 .rpc_resp = &res,
5696 };
5697 struct rpc_task_setup task_setup = {
5698 .rpc_client = clp->cl_rpcclient,
5699 .rpc_message = &msg,
5700 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005701 .callback_data = &data,
5702 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005703 };
5704 int status;
5705
Trond Myklebust9d12b212012-01-17 22:04:25 -05005706 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005707 dprintk("--> %s\n", __func__);
5708 task = rpc_run_task(&task_setup);
5709
5710 if (IS_ERR(task))
5711 status = PTR_ERR(task);
5712 else {
5713 status = task->tk_status;
5714 rpc_put_task(task);
5715 }
5716 dprintk("<-- %s return %d\n", __func__, status);
5717
5718 return status;
5719}
5720
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005721struct nfs4_slot *nfs4_alloc_slots(struct nfs4_slot_table *table,
5722 u32 max_slots, gfp_t gfp_flags)
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005723{
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005724 struct nfs4_slot *tbl;
5725 u32 i;
5726
5727 tbl = kmalloc_array(max_slots, sizeof(*tbl), gfp_flags);
5728 if (tbl != NULL) {
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005729 for (i = 0; i < max_slots; i++) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005730 tbl[i].table = table;
Trond Myklebustdf2fabf2012-11-16 12:45:06 -05005731 tbl[i].slot_nr = i;
5732 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005733 }
5734 return tbl;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005735}
5736
5737static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5738 struct nfs4_slot *new,
5739 u32 max_slots,
5740 u32 ivalue)
5741{
5742 struct nfs4_slot *old = NULL;
5743 u32 i;
5744
5745 spin_lock(&tbl->slot_tbl_lock);
5746 if (new) {
5747 old = tbl->slots;
5748 tbl->slots = new;
5749 tbl->max_slots = max_slots;
5750 }
Trond Myklebustf9b1ef52012-10-29 16:48:40 -04005751 tbl->highest_used_slotid = NFS4_NO_SLOT;
Trond Myklebust464ee9f2012-11-20 12:49:27 -05005752 tbl->target_highest_slotid = max_slots - 1;
Trond Myklebustda0507b2012-11-20 18:10:30 -05005753 tbl->server_highest_slotid = max_slots - 1;
Trond Myklebust97e548a2012-11-20 14:45:48 -05005754 tbl->max_slotid = max_slots - 1;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005755 for (i = 0; i < tbl->max_slots; i++)
5756 tbl->slots[i].seq_nr = ivalue;
5757 spin_unlock(&tbl->slot_tbl_lock);
5758 kfree(old);
5759}
5760
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005761/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005762 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005763 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005764static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5765 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005766{
Andy Adamson104aeba2010-01-14 17:45:10 -05005767 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005768 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005769
Andy Adamson104aeba2010-01-14 17:45:10 -05005770 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5771 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005772
Andy Adamson104aeba2010-01-14 17:45:10 -05005773 /* Does the newly negotiated max_reqs match the existing slot table? */
5774 if (max_reqs != tbl->max_slots) {
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005775 new = nfs4_alloc_slots(tbl, max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005776 if (!new)
5777 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005778 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005779 ret = 0;
5780
5781 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005782 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5783 tbl, tbl->slots, tbl->max_slots);
5784out:
5785 dprintk("<-- %s: return %d\n", __func__, ret);
5786 return ret;
5787}
5788
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005789/* Destroy the slot table */
5790static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5791{
5792 if (session->fc_slot_table.slots != NULL) {
5793 kfree(session->fc_slot_table.slots);
5794 session->fc_slot_table.slots = NULL;
5795 }
5796 if (session->bc_slot_table.slots != NULL) {
5797 kfree(session->bc_slot_table.slots);
5798 session->bc_slot_table.slots = NULL;
5799 }
5800 return;
5801}
5802
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005803/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005804 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005805 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005806static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005807{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005808 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005809 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005810
Andy Adamsonaacd5532011-11-09 13:58:21 -05005811 dprintk("--> %s\n", __func__);
5812 /* Fore channel */
5813 tbl = &ses->fc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005814 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005815 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5816 if (status) /* -ENOMEM */
5817 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005818 /* Back channel */
5819 tbl = &ses->bc_slot_table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -05005820 tbl->session = ses;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005821 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5822 if (status && tbl->slots == NULL)
5823 /* Fore and back channel share a connection so get
5824 * both slot tables or neither */
5825 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005826 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005827}
5828
5829struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5830{
5831 struct nfs4_session *session;
5832 struct nfs4_slot_table *tbl;
5833
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005834 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005835 if (!session)
5836 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005837
Andy Adamson557134a2009-04-01 09:21:53 -04005838 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005839 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005840 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005841 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005842 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005843
5844 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005845 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005846 spin_lock_init(&tbl->slot_tbl_lock);
5847 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005848 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005849
Trond Myklebust1055d762010-06-16 09:52:27 -04005850 session->session_state = 1<<NFS4_SESSION_INITING;
5851
Andy Adamson557134a2009-04-01 09:21:53 -04005852 session->clp = clp;
5853 return session;
5854}
5855
5856void nfs4_destroy_session(struct nfs4_session *session)
5857{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005858 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005859 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005860
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005861 cred = nfs4_get_exchange_id_cred(session->clp);
5862 nfs4_proc_destroy_session(session, cred);
5863 if (cred)
5864 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005865
5866 rcu_read_lock();
5867 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5868 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005869 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005870 __func__, xprt);
5871 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005872 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005873 kfree(session);
5874}
5875
Andy Adamsonfc931582009-04-01 09:22:31 -04005876/*
5877 * Initialize the values to be used by the client in CREATE_SESSION
5878 * If nfs4_init_session set the fore channel request and response sizes,
5879 * use them.
5880 *
5881 * Set the back channel max_resp_sz_cached to zero to force the client to
5882 * always set csa_cachethis to FALSE because the current implementation
5883 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5884 */
5885static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5886{
5887 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005888 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5889 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005890
5891 if (mxrqst_sz == 0)
5892 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5893 if (mxresp_sz == 0)
5894 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5895 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005896 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5897 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005898 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005899 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005900
5901 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005902 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005903 __func__,
5904 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005905 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005906
5907 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005908 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5909 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5910 args->bc_attrs.max_resp_sz_cached = 0;
5911 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5912 args->bc_attrs.max_reqs = 1;
5913
5914 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5915 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5916 __func__,
5917 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5918 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5919 args->bc_attrs.max_reqs);
5920}
5921
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005922static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005923{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005924 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5925 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5926
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005927 if (rcvd->max_resp_sz > sent->max_resp_sz)
5928 return -EINVAL;
5929 /*
5930 * Our requested max_ops is the minimum we need; we're not
5931 * prepared to break up compounds into smaller pieces than that.
5932 * So, no point even trying to continue if the server won't
5933 * cooperate:
5934 */
5935 if (rcvd->max_ops < sent->max_ops)
5936 return -EINVAL;
5937 if (rcvd->max_reqs == 0)
5938 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005939 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5940 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005941 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005942}
5943
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005944static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5945{
5946 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5947 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005948
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005949 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5950 return -EINVAL;
5951 if (rcvd->max_resp_sz < sent->max_resp_sz)
5952 return -EINVAL;
5953 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5954 return -EINVAL;
5955 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005956 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005957 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005958 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005959 return -EINVAL;
5960 return 0;
5961}
Andy Adamson8d353012009-04-01 09:22:32 -04005962
Andy Adamson8d353012009-04-01 09:22:32 -04005963static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5964 struct nfs4_session *session)
5965{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005966 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005967
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005968 ret = nfs4_verify_fore_channel_attrs(args, session);
5969 if (ret)
5970 return ret;
5971 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005972}
5973
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005974static int _nfs4_proc_create_session(struct nfs_client *clp,
5975 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005976{
5977 struct nfs4_session *session = clp->cl_session;
5978 struct nfs41_create_session_args args = {
5979 .client = clp,
5980 .cb_program = NFS4_CALLBACK,
5981 };
5982 struct nfs41_create_session_res res = {
5983 .client = clp,
5984 };
5985 struct rpc_message msg = {
5986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5987 .rpc_argp = &args,
5988 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005989 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005990 };
5991 int status;
5992
5993 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005994 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005995
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005996 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005997
Trond Myklebust43095d32012-11-20 11:13:12 -05005998 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005999 /* Verify the session's negotiated channel_attrs values */
6000 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006001 /* Increment the clientid slot sequence id */
6002 clp->cl_seqid++;
6003 }
6004
6005 return status;
6006}
6007
6008/*
6009 * Issues a CREATE_SESSION operation to the server.
6010 * It is the responsibility of the caller to verify the session is
6011 * expired before calling this routine.
6012 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006013int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006014{
6015 int status;
6016 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006017 struct nfs4_session *session = clp->cl_session;
6018
6019 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6020
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006021 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006022 if (status)
6023 goto out;
6024
Andy Adamsonaacd5532011-11-09 13:58:21 -05006025 /* Init or reset the session slot tables */
6026 status = nfs4_setup_session_slot_tables(session);
6027 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006028 if (status)
6029 goto out;
6030
6031 ptr = (unsigned *)&session->sess_id.data[0];
6032 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6033 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006034out:
6035 dprintk("<-- %s\n", __func__);
6036 return status;
6037}
6038
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006039/*
6040 * Issue the over-the-wire RPC DESTROY_SESSION.
6041 * The caller must serialize access to this routine.
6042 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006043int nfs4_proc_destroy_session(struct nfs4_session *session,
6044 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006045{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006046 struct rpc_message msg = {
6047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6048 .rpc_argp = session,
6049 .rpc_cred = cred,
6050 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006051 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006052
6053 dprintk("--> nfs4_proc_destroy_session\n");
6054
6055 /* session is still being setup */
6056 if (session->clp->cl_cons_state != NFS_CS_READY)
6057 return status;
6058
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006059 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006060
6061 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006062 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006063 "Session has been destroyed regardless...\n", status);
6064
6065 dprintk("<-- nfs4_proc_destroy_session\n");
6066 return status;
6067}
6068
Trond Myklebust7b38c362012-05-23 13:23:31 -04006069/*
6070 * With sessions, the client is not marked ready until after a
6071 * successful EXCHANGE_ID and CREATE_SESSION.
6072 *
6073 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6074 * other versions of NFS can be tried.
6075 */
6076static int nfs41_check_session_ready(struct nfs_client *clp)
6077{
6078 int ret;
6079
6080 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6081 ret = nfs4_client_recover_expired_lease(clp);
6082 if (ret)
6083 return ret;
6084 }
6085 if (clp->cl_cons_state < NFS_CS_READY)
6086 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04006087 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04006088 return 0;
6089}
6090
Trond Myklebustfccba802009-07-21 16:48:07 -04006091int nfs4_init_session(struct nfs_server *server)
6092{
6093 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006094 struct nfs4_session *session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006095 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6096 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
Trond Myklebustfccba802009-07-21 16:48:07 -04006097
6098 if (!nfs4_has_session(clp))
6099 return 0;
6100
Trond Myklebustae72ae672012-11-20 11:02:55 -05006101 if (server->rsize != 0)
6102 target_max_resp_sz = server->rsize;
6103 target_max_resp_sz += nfs41_maxread_overhead;
6104
6105 if (server->wsize != 0)
6106 target_max_rqst_sz = server->wsize;
6107 target_max_rqst_sz += nfs41_maxwrite_overhead;
6108
Trond Myklebust1055d762010-06-16 09:52:27 -04006109 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04006110 spin_lock(&clp->cl_lock);
6111 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebustae72ae672012-11-20 11:02:55 -05006112 /* Initialise targets and channel attributes */
6113 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6114 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6115 session->fc_target_max_resp_sz = target_max_resp_sz;
6116 session->fc_attrs.max_resp_sz = target_max_resp_sz;
Trond Myklebust688a9022012-11-20 10:53:39 -05006117 } else {
6118 /* Just adjust the targets */
6119 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6120 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6121 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6122 }
6123 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6124 session->fc_target_max_resp_sz = target_max_resp_sz;
6125 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6126 }
Trond Myklebust7b38c362012-05-23 13:23:31 -04006127 }
6128 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05006129
Trond Myklebust688a9022012-11-20 10:53:39 -05006130 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6131 nfs4_schedule_lease_recovery(clp);
6132
Trond Myklebust7b38c362012-05-23 13:23:31 -04006133 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04006134}
6135
Trond Myklebust7b38c362012-05-23 13:23:31 -04006136int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00006137{
6138 struct nfs4_session *session = clp->cl_session;
6139 int ret;
6140
Trond Myklebust7b38c362012-05-23 13:23:31 -04006141 spin_lock(&clp->cl_lock);
6142 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6143 /*
6144 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6145 * DS lease to be equal to the MDS lease.
6146 */
6147 clp->cl_lease_time = lease_time;
6148 clp->cl_last_renewal = jiffies;
6149 }
6150 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00006151
Trond Myklebust7b38c362012-05-23 13:23:31 -04006152 ret = nfs41_check_session_ready(clp);
6153 if (ret)
6154 return ret;
6155 /* Test for the DS role */
6156 if (!is_ds_client(clp))
6157 return -ENODEV;
6158 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00006159}
6160EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6161
6162
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006163/*
6164 * Renew the cl_session lease.
6165 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006166struct nfs4_sequence_data {
6167 struct nfs_client *clp;
6168 struct nfs4_sequence_args args;
6169 struct nfs4_sequence_res res;
6170};
6171
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006172static void nfs41_sequence_release(void *data)
6173{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006174 struct nfs4_sequence_data *calldata = data;
6175 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006176
Alexandros Batsakis71358402010-02-05 03:45:05 -08006177 if (atomic_read(&clp->cl_count) > 1)
6178 nfs4_schedule_state_renewal(clp);
6179 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006180 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006181}
6182
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006183static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6184{
6185 switch(task->tk_status) {
6186 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006187 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6188 return -EAGAIN;
6189 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006190 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006191 }
6192 return 0;
6193}
6194
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006195static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006196{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006197 struct nfs4_sequence_data *calldata = data;
6198 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006199
Trond Myklebust14516c32010-07-31 14:29:06 -04006200 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6201 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006202
6203 if (task->tk_status < 0) {
6204 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006205 if (atomic_read(&clp->cl_count) == 1)
6206 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006207
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006208 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6209 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006210 return;
6211 }
6212 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006213 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006214out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006215 dprintk("<-- %s\n", __func__);
6216}
6217
6218static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6219{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006220 struct nfs4_sequence_data *calldata = data;
6221 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006222 struct nfs4_sequence_args *args;
6223 struct nfs4_sequence_res *res;
6224
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006225 args = task->tk_msg.rpc_argp;
6226 res = task->tk_msg.rpc_resp;
6227
Trond Myklebust9d12b212012-01-17 22:04:25 -05006228 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006229 return;
6230 rpc_call_start(task);
6231}
6232
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006233static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6234{
6235 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6236 nfs41_sequence_prepare(task, data);
6237}
6238
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006239static const struct rpc_call_ops nfs41_sequence_ops = {
6240 .rpc_call_done = nfs41_sequence_call_done,
6241 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006242 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006243};
6244
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006245static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6246 .rpc_call_done = nfs41_sequence_call_done,
6247 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6248 .rpc_release = nfs41_sequence_release,
6249};
6250
6251static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6252 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006253{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006254 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006255 struct rpc_message msg = {
6256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6257 .rpc_cred = cred,
6258 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006259 struct rpc_task_setup task_setup_data = {
6260 .rpc_client = clp->cl_rpcclient,
6261 .rpc_message = &msg,
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006262 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006263 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6264 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006265
Alexandros Batsakis71358402010-02-05 03:45:05 -08006266 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006267 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006268 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006269 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006270 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006271 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006272 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006273 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006274 msg.rpc_argp = &calldata->args;
6275 msg.rpc_resp = &calldata->res;
6276 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006277 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006278
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006279 return rpc_run_task(&task_setup_data);
6280}
6281
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006282static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006283{
6284 struct rpc_task *task;
6285 int ret = 0;
6286
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006287 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6288 return 0;
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006289 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006290 if (IS_ERR(task))
6291 ret = PTR_ERR(task);
6292 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006293 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006294 dprintk("<-- %s status=%d\n", __func__, ret);
6295 return ret;
6296}
6297
6298static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6299{
6300 struct rpc_task *task;
6301 int ret;
6302
Bryan Schumaker6bdb5f22012-11-12 16:55:38 -05006303 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006304 if (IS_ERR(task)) {
6305 ret = PTR_ERR(task);
6306 goto out;
6307 }
6308 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006309 if (!ret) {
6310 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6311
6312 if (task->tk_status == 0)
6313 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006314 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006315 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006316 rpc_put_task(task);
6317out:
6318 dprintk("<-- %s status=%d\n", __func__, ret);
6319 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006320}
6321
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006322struct nfs4_reclaim_complete_data {
6323 struct nfs_client *clp;
6324 struct nfs41_reclaim_complete_args arg;
6325 struct nfs41_reclaim_complete_res res;
6326};
6327
6328static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6329{
6330 struct nfs4_reclaim_complete_data *calldata = data;
6331
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006332 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04006333 if (nfs41_setup_sequence(calldata->clp->cl_session,
6334 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006335 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006336 return;
6337
6338 rpc_call_start(task);
6339}
6340
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006341static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6342{
6343 switch(task->tk_status) {
6344 case 0:
6345 case -NFS4ERR_COMPLETE_ALREADY:
6346 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6347 break;
6348 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006349 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006350 /* fall through */
6351 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006352 return -EAGAIN;
6353 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006354 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006355 }
6356 return 0;
6357}
6358
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006359static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6360{
6361 struct nfs4_reclaim_complete_data *calldata = data;
6362 struct nfs_client *clp = calldata->clp;
6363 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6364
6365 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006366 if (!nfs41_sequence_done(task, res))
6367 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006368
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006369 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6370 rpc_restart_call_prepare(task);
6371 return;
6372 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006373 dprintk("<-- %s\n", __func__);
6374}
6375
6376static void nfs4_free_reclaim_complete_data(void *data)
6377{
6378 struct nfs4_reclaim_complete_data *calldata = data;
6379
6380 kfree(calldata);
6381}
6382
6383static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6384 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6385 .rpc_call_done = nfs4_reclaim_complete_done,
6386 .rpc_release = nfs4_free_reclaim_complete_data,
6387};
6388
6389/*
6390 * Issue a global reclaim complete.
6391 */
6392static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6393{
6394 struct nfs4_reclaim_complete_data *calldata;
6395 struct rpc_task *task;
6396 struct rpc_message msg = {
6397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6398 };
6399 struct rpc_task_setup task_setup_data = {
6400 .rpc_client = clp->cl_rpcclient,
6401 .rpc_message = &msg,
6402 .callback_ops = &nfs4_reclaim_complete_call_ops,
6403 .flags = RPC_TASK_ASYNC,
6404 };
6405 int status = -ENOMEM;
6406
6407 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006408 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006409 if (calldata == NULL)
6410 goto out;
6411 calldata->clp = clp;
6412 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006413
Trond Myklebust9d12b212012-01-17 22:04:25 -05006414 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006415 msg.rpc_argp = &calldata->arg;
6416 msg.rpc_resp = &calldata->res;
6417 task_setup_data.callback_data = calldata;
6418 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006419 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006420 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006421 goto out;
6422 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006423 status = nfs4_wait_for_completion_rpc_task(task);
6424 if (status == 0)
6425 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006426 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006427 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006428out:
6429 dprintk("<-- %s status=%d\n", __func__, status);
6430 return status;
6431}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006432
6433static void
6434nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6435{
6436 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006437 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006438
6439 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006440 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6441 * right now covering the LAYOUTGET we are about to send.
6442 * However, that is not so catastrophic, and there seems
6443 * to be no way to prevent it completely.
6444 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006445 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006446 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006447 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006448 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6449 NFS_I(lgp->args.inode)->layout,
6450 lgp->args.ctx->state)) {
6451 rpc_exit(task, NFS4_OK);
6452 return;
6453 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006454 rpc_call_start(task);
6455}
6456
6457static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6458{
6459 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006460 struct inode *inode = lgp->args.inode;
6461 struct nfs_server *server = NFS_SERVER(inode);
6462 struct pnfs_layout_hdr *lo;
6463 struct nfs4_state *state = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006464
6465 dprintk("--> %s\n", __func__);
6466
6467 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006468 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006469
6470 switch (task->tk_status) {
6471 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006472 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006473 case -NFS4ERR_LAYOUTTRYLATER:
6474 case -NFS4ERR_RECALLCONFLICT:
6475 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006476 break;
6477 case -NFS4ERR_EXPIRED:
6478 case -NFS4ERR_BAD_STATEID:
6479 spin_lock(&inode->i_lock);
6480 lo = NFS_I(inode)->layout;
6481 if (!lo || list_empty(&lo->plh_segs)) {
6482 spin_unlock(&inode->i_lock);
6483 /* If the open stateid was bad, then recover it. */
6484 state = lgp->args.ctx->state;
6485 } else {
6486 LIST_HEAD(head);
6487
6488 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6489 spin_unlock(&inode->i_lock);
6490 /* Mark the bad layout state as invalid, then
6491 * retry using the open stateid. */
6492 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006493 }
6494 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006495 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6496 rpc_restart_call_prepare(task);
6497out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006498 dprintk("<-- %s\n", __func__);
6499}
6500
Idan Kedar85541162012-08-02 11:47:10 +03006501static size_t max_response_pages(struct nfs_server *server)
6502{
6503 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6504 return nfs_page_array_len(0, max_resp_sz);
6505}
6506
6507static void nfs4_free_pages(struct page **pages, size_t size)
6508{
6509 int i;
6510
6511 if (!pages)
6512 return;
6513
6514 for (i = 0; i < size; i++) {
6515 if (!pages[i])
6516 break;
6517 __free_page(pages[i]);
6518 }
6519 kfree(pages);
6520}
6521
6522static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6523{
6524 struct page **pages;
6525 int i;
6526
6527 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6528 if (!pages) {
6529 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6530 return NULL;
6531 }
6532
6533 for (i = 0; i < size; i++) {
6534 pages[i] = alloc_page(gfp_flags);
6535 if (!pages[i]) {
6536 dprintk("%s: failed to allocate page\n", __func__);
6537 nfs4_free_pages(pages, size);
6538 return NULL;
6539 }
6540 }
6541
6542 return pages;
6543}
6544
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006545static void nfs4_layoutget_release(void *calldata)
6546{
6547 struct nfs4_layoutget *lgp = calldata;
Idan Kedar85541162012-08-02 11:47:10 +03006548 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6549 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006550
6551 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006552 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006553 put_nfs_open_context(lgp->args.ctx);
6554 kfree(calldata);
6555 dprintk("<-- %s\n", __func__);
6556}
6557
6558static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6559 .rpc_call_prepare = nfs4_layoutget_prepare,
6560 .rpc_call_done = nfs4_layoutget_done,
6561 .rpc_release = nfs4_layoutget_release,
6562};
6563
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006564struct pnfs_layout_segment *
6565nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006566{
6567 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar85541162012-08-02 11:47:10 +03006568 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006569 struct rpc_task *task;
6570 struct rpc_message msg = {
6571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6572 .rpc_argp = &lgp->args,
6573 .rpc_resp = &lgp->res,
6574 };
6575 struct rpc_task_setup task_setup_data = {
6576 .rpc_client = server->client,
6577 .rpc_message = &msg,
6578 .callback_ops = &nfs4_layoutget_call_ops,
6579 .callback_data = lgp,
6580 .flags = RPC_TASK_ASYNC,
6581 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006582 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006583 int status = 0;
6584
6585 dprintk("--> %s\n", __func__);
6586
Idan Kedar85541162012-08-02 11:47:10 +03006587 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6588 if (!lgp->args.layout.pages) {
6589 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006590 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006591 }
6592 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6593
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006594 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006595 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006596 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006597 task = rpc_run_task(&task_setup_data);
6598 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006599 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006600 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006601 if (status == 0)
6602 status = task->tk_status;
6603 if (status == 0)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006604 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006605 rpc_put_task(task);
6606 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006607 if (status)
6608 return ERR_PTR(status);
6609 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006610}
6611
Benny Halevycbe82602011-05-22 19:52:37 +03006612static void
6613nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6614{
6615 struct nfs4_layoutreturn *lrp = calldata;
6616
6617 dprintk("--> %s\n", __func__);
6618 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006619 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006620 return;
6621 rpc_call_start(task);
6622}
6623
6624static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6625{
6626 struct nfs4_layoutreturn *lrp = calldata;
6627 struct nfs_server *server;
6628
6629 dprintk("--> %s\n", __func__);
6630
6631 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6632 return;
6633
6634 server = NFS_SERVER(lrp->args.inode);
6635 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006636 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006637 return;
6638 }
Benny Halevycbe82602011-05-22 19:52:37 +03006639 dprintk("<-- %s\n", __func__);
6640}
6641
6642static void nfs4_layoutreturn_release(void *calldata)
6643{
6644 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006645 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006646
6647 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006648 spin_lock(&lo->plh_inode->i_lock);
6649 if (lrp->res.lrs_present)
6650 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6651 lo->plh_block_lgets--;
6652 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006653 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006654 kfree(calldata);
6655 dprintk("<-- %s\n", __func__);
6656}
6657
6658static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6659 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6660 .rpc_call_done = nfs4_layoutreturn_done,
6661 .rpc_release = nfs4_layoutreturn_release,
6662};
6663
6664int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6665{
6666 struct rpc_task *task;
6667 struct rpc_message msg = {
6668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6669 .rpc_argp = &lrp->args,
6670 .rpc_resp = &lrp->res,
6671 };
6672 struct rpc_task_setup task_setup_data = {
6673 .rpc_client = lrp->clp->cl_rpcclient,
6674 .rpc_message = &msg,
6675 .callback_ops = &nfs4_layoutreturn_call_ops,
6676 .callback_data = lrp,
6677 };
6678 int status;
6679
6680 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006681 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006682 task = rpc_run_task(&task_setup_data);
6683 if (IS_ERR(task))
6684 return PTR_ERR(task);
6685 status = task->tk_status;
6686 dprintk("<-- %s status=%d\n", __func__, status);
6687 rpc_put_task(task);
6688 return status;
6689}
6690
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006691/*
6692 * Retrieve the list of Data Server devices from the MDS.
6693 */
6694static int _nfs4_getdevicelist(struct nfs_server *server,
6695 const struct nfs_fh *fh,
6696 struct pnfs_devicelist *devlist)
6697{
6698 struct nfs4_getdevicelist_args args = {
6699 .fh = fh,
6700 .layoutclass = server->pnfs_curr_ld->id,
6701 };
6702 struct nfs4_getdevicelist_res res = {
6703 .devlist = devlist,
6704 };
6705 struct rpc_message msg = {
6706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6707 .rpc_argp = &args,
6708 .rpc_resp = &res,
6709 };
6710 int status;
6711
6712 dprintk("--> %s\n", __func__);
6713 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6714 &res.seq_res, 0);
6715 dprintk("<-- %s status=%d\n", __func__, status);
6716 return status;
6717}
6718
6719int nfs4_proc_getdevicelist(struct nfs_server *server,
6720 const struct nfs_fh *fh,
6721 struct pnfs_devicelist *devlist)
6722{
6723 struct nfs4_exception exception = { };
6724 int err;
6725
6726 do {
6727 err = nfs4_handle_exception(server,
6728 _nfs4_getdevicelist(server, fh, devlist),
6729 &exception);
6730 } while (exception.retry);
6731
6732 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6733 err, devlist->num_devs);
6734
6735 return err;
6736}
6737EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6738
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006739static int
6740_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6741{
6742 struct nfs4_getdeviceinfo_args args = {
6743 .pdev = pdev,
6744 };
6745 struct nfs4_getdeviceinfo_res res = {
6746 .pdev = pdev,
6747 };
6748 struct rpc_message msg = {
6749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6750 .rpc_argp = &args,
6751 .rpc_resp = &res,
6752 };
6753 int status;
6754
6755 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006756 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006757 dprintk("<-- %s status=%d\n", __func__, status);
6758
6759 return status;
6760}
6761
6762int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6763{
6764 struct nfs4_exception exception = { };
6765 int err;
6766
6767 do {
6768 err = nfs4_handle_exception(server,
6769 _nfs4_proc_getdeviceinfo(server, pdev),
6770 &exception);
6771 } while (exception.retry);
6772 return err;
6773}
6774EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6775
Andy Adamson863a3c62011-03-23 13:27:54 +00006776static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6777{
6778 struct nfs4_layoutcommit_data *data = calldata;
6779 struct nfs_server *server = NFS_SERVER(data->args.inode);
6780
6781 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006782 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006783 return;
6784 rpc_call_start(task);
6785}
6786
6787static void
6788nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6789{
6790 struct nfs4_layoutcommit_data *data = calldata;
6791 struct nfs_server *server = NFS_SERVER(data->args.inode);
6792
6793 if (!nfs4_sequence_done(task, &data->res.seq_res))
6794 return;
6795
6796 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006797 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6798 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6799 case -NFS4ERR_BADLAYOUT: /* no layout */
6800 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006801 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006802 break;
6803 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006804 nfs_post_op_update_inode_force_wcc(data->args.inode,
6805 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006806 break;
6807 default:
6808 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6809 rpc_restart_call_prepare(task);
6810 return;
6811 }
6812 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006813}
6814
6815static void nfs4_layoutcommit_release(void *calldata)
6816{
6817 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006818 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006819 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006820
Andy Adamsondb29c082011-07-30 20:52:38 -04006821 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006822 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006823 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6824 list_del_init(&lseg->pls_lc_list);
6825 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6826 &lseg->pls_flags))
Trond Myklebust9369a432012-09-18 20:57:08 -04006827 pnfs_put_lseg(lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04006828 }
Peng Tao92407e72011-10-23 20:21:17 -07006829
6830 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6831 smp_mb__after_clear_bit();
6832 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6833
Andy Adamson863a3c62011-03-23 13:27:54 +00006834 put_rpccred(data->cred);
6835 kfree(data);
6836}
6837
6838static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6839 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6840 .rpc_call_done = nfs4_layoutcommit_done,
6841 .rpc_release = nfs4_layoutcommit_release,
6842};
6843
6844int
Andy Adamsonef311532011-03-12 02:58:10 -05006845nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006846{
6847 struct rpc_message msg = {
6848 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6849 .rpc_argp = &data->args,
6850 .rpc_resp = &data->res,
6851 .rpc_cred = data->cred,
6852 };
6853 struct rpc_task_setup task_setup_data = {
6854 .task = &data->task,
6855 .rpc_client = NFS_CLIENT(data->args.inode),
6856 .rpc_message = &msg,
6857 .callback_ops = &nfs4_layoutcommit_ops,
6858 .callback_data = data,
6859 .flags = RPC_TASK_ASYNC,
6860 };
6861 struct rpc_task *task;
6862 int status = 0;
6863
6864 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6865 "lbw: %llu inode %lu\n",
6866 data->task.tk_pid, sync,
6867 data->args.lastbytewritten,
6868 data->args.inode->i_ino);
6869
Trond Myklebust9d12b212012-01-17 22:04:25 -05006870 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006871 task = rpc_run_task(&task_setup_data);
6872 if (IS_ERR(task))
6873 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006874 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006875 goto out;
6876 status = nfs4_wait_for_completion_rpc_task(task);
6877 if (status != 0)
6878 goto out;
6879 status = task->tk_status;
6880out:
6881 dprintk("%s: status %d\n", __func__, status);
6882 rpc_put_task(task);
6883 return status;
6884}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006885
6886static int
6887_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6888 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6889{
6890 struct nfs41_secinfo_no_name_args args = {
6891 .style = SECINFO_STYLE_CURRENT_FH,
6892 };
6893 struct nfs4_secinfo_res res = {
6894 .flavors = flavors,
6895 };
6896 struct rpc_message msg = {
6897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6898 .rpc_argp = &args,
6899 .rpc_resp = &res,
6900 };
6901 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6902}
6903
6904static int
6905nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6906 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6907{
6908 struct nfs4_exception exception = { };
6909 int err;
6910 do {
6911 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6912 switch (err) {
6913 case 0:
6914 case -NFS4ERR_WRONGSEC:
6915 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006916 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006917 default:
6918 err = nfs4_handle_exception(server, err, &exception);
6919 }
6920 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006921out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006922 return err;
6923}
6924
6925static int
6926nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6927 struct nfs_fsinfo *info)
6928{
6929 int err;
6930 struct page *page;
6931 rpc_authflavor_t flavor;
6932 struct nfs4_secinfo_flavors *flavors;
6933
6934 page = alloc_page(GFP_KERNEL);
6935 if (!page) {
6936 err = -ENOMEM;
6937 goto out;
6938 }
6939
6940 flavors = page_address(page);
6941 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6942
6943 /*
6944 * Fall back on "guess and check" method if
6945 * the server doesn't support SECINFO_NO_NAME
6946 */
6947 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6948 err = nfs4_find_root_sec(server, fhandle, info);
6949 goto out_freepage;
6950 }
6951 if (err)
6952 goto out_freepage;
6953
6954 flavor = nfs_find_best_sec(flavors);
6955 if (err == 0)
6956 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6957
6958out_freepage:
6959 put_page(page);
6960 if (err == -EACCES)
6961 return -EPERM;
6962out:
6963 return err;
6964}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006965
6966static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006967{
6968 int status;
6969 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006970 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006971 };
6972 struct nfs41_test_stateid_res res;
6973 struct rpc_message msg = {
6974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6975 .rpc_argp = &args,
6976 .rpc_resp = &res,
6977 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006978
Chuck Lever38527b12012-07-11 16:30:23 -04006979 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006980 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006981 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04006982 if (status != NFS_OK) {
6983 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006984 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006985 }
6986 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006987 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006988}
6989
Chuck Lever38527b12012-07-11 16:30:23 -04006990/**
6991 * nfs41_test_stateid - perform a TEST_STATEID operation
6992 *
6993 * @server: server / transport on which to perform the operation
6994 * @stateid: state ID to test
6995 *
6996 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6997 * Otherwise a negative NFS4ERR value is returned if the operation
6998 * failed or the state ID is not currently valid.
6999 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007000static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007001{
7002 struct nfs4_exception exception = { };
7003 int err;
7004 do {
Chuck Lever377e5072012-07-11 16:29:45 -04007005 err = _nfs41_test_stateid(server, stateid);
7006 if (err != -NFS4ERR_DELAY)
7007 break;
7008 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007009 } while (exception.retry);
7010 return err;
7011}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007012
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007013static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007014{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007015 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007016 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007017 };
7018 struct nfs41_free_stateid_res res;
7019 struct rpc_message msg = {
7020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7021 .rpc_argp = &args,
7022 .rpc_resp = &res,
7023 };
Chuck Lever38527b12012-07-11 16:30:23 -04007024 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007025
Chuck Lever38527b12012-07-11 16:30:23 -04007026 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007027 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Chuck Lever38527b12012-07-11 16:30:23 -04007028 status = nfs4_call_sync_sequence(server->client, server, &msg,
Chuck Lever377e5072012-07-11 16:29:45 -04007029 &args.seq_args, &res.seq_res, 1);
Chuck Lever38527b12012-07-11 16:30:23 -04007030 dprintk("NFS reply free_stateid: %d\n", status);
7031 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007032}
7033
Chuck Lever38527b12012-07-11 16:30:23 -04007034/**
7035 * nfs41_free_stateid - perform a FREE_STATEID operation
7036 *
7037 * @server: server / transport on which to perform the operation
7038 * @stateid: state ID to release
7039 *
7040 * Returns NFS_OK if the server freed "stateid". Otherwise a
7041 * negative NFS4ERR value is returned.
7042 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007043static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007044{
7045 struct nfs4_exception exception = { };
7046 int err;
7047 do {
Chuck Lever377e5072012-07-11 16:29:45 -04007048 err = _nfs4_free_stateid(server, stateid);
7049 if (err != -NFS4ERR_DELAY)
7050 break;
7051 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007052 } while (exception.retry);
7053 return err;
7054}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007055
7056static bool nfs41_match_stateid(const nfs4_stateid *s1,
7057 const nfs4_stateid *s2)
7058{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007059 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007060 return false;
7061
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007062 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007063 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007064 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007065 return true;
7066
7067 return false;
7068}
7069
Andy Adamson557134a2009-04-01 09:21:53 -04007070#endif /* CONFIG_NFS_V4_1 */
7071
Trond Myklebust36281ca2012-03-04 18:13:56 -05007072static bool nfs4_match_stateid(const nfs4_stateid *s1,
7073 const nfs4_stateid *s2)
7074{
Trond Myklebustf597c532012-03-04 18:13:56 -05007075 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007076}
7077
7078
Trond Myklebust17280172012-03-11 13:11:00 -04007079static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007080 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007081 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 .recover_open = nfs4_open_reclaim,
7083 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007084 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007085 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007086 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087};
7088
Andy Adamson591d71c2009-04-01 09:22:47 -04007089#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007090static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007091 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7092 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7093 .recover_open = nfs4_open_reclaim,
7094 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007095 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007096 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007097 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007098 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007099};
7100#endif /* CONFIG_NFS_V4_1 */
7101
Trond Myklebust17280172012-03-11 13:11:00 -04007102static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007103 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007104 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 .recover_open = nfs4_open_expired,
7106 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007107 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007108 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109};
7110
Andy Adamson591d71c2009-04-01 09:22:47 -04007111#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007112static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007113 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7114 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007115 .recover_open = nfs41_open_expired,
7116 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007117 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007118 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007119};
7120#endif /* CONFIG_NFS_V4_1 */
7121
Trond Myklebust17280172012-03-11 13:11:00 -04007122static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007123 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007124 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007125 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007126};
7127
7128#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007129static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007130 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007131 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007132 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007133};
7134#endif
7135
Trond Myklebust97dc1352010-06-16 09:52:26 -04007136static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7137 .minor_version = 0,
7138 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007139 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007140 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007141 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7142 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7143 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007144};
7145
7146#if defined(CONFIG_NFS_V4_1)
7147static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7148 .minor_version = 1,
7149 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007150 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007151 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007152 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7153 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7154 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007155};
7156#endif
7157
Trond Myklebust97dc1352010-06-16 09:52:26 -04007158const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7159 [0] = &nfs_v4_0_minor_ops,
7160#if defined(CONFIG_NFS_V4_1)
7161 [1] = &nfs_v4_1_minor_ops,
7162#endif
7163};
7164
Bryan Schumaker73a79702012-07-16 16:39:12 -04007165const struct inode_operations nfs4_dir_inode_operations = {
7166 .create = nfs_create,
7167 .lookup = nfs_lookup,
7168 .atomic_open = nfs_atomic_open,
7169 .link = nfs_link,
7170 .unlink = nfs_unlink,
7171 .symlink = nfs_symlink,
7172 .mkdir = nfs_mkdir,
7173 .rmdir = nfs_rmdir,
7174 .mknod = nfs_mknod,
7175 .rename = nfs_rename,
7176 .permission = nfs_permission,
7177 .getattr = nfs_getattr,
7178 .setattr = nfs_setattr,
7179 .getxattr = generic_getxattr,
7180 .setxattr = generic_setxattr,
7181 .listxattr = generic_listxattr,
7182 .removexattr = generic_removexattr,
7183};
7184
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007185static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007186 .permission = nfs_permission,
7187 .getattr = nfs_getattr,
7188 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007189 .getxattr = generic_getxattr,
7190 .setxattr = generic_setxattr,
7191 .listxattr = generic_listxattr,
7192 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007193};
7194
David Howells509de812006-08-22 20:06:11 -04007195const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 .version = 4, /* protocol version */
7197 .dentry_ops = &nfs4_dentry_operations,
7198 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007199 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007200 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007202 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007203 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204 .getattr = nfs4_proc_getattr,
7205 .setattr = nfs4_proc_setattr,
7206 .lookup = nfs4_proc_lookup,
7207 .access = nfs4_proc_access,
7208 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 .create = nfs4_proc_create,
7210 .remove = nfs4_proc_remove,
7211 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007212 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 .unlink_done = nfs4_proc_unlink_done,
7214 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007215 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007216 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007217 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 .link = nfs4_proc_link,
7219 .symlink = nfs4_proc_symlink,
7220 .mkdir = nfs4_proc_mkdir,
7221 .rmdir = nfs4_proc_remove,
7222 .readdir = nfs4_proc_readdir,
7223 .mknod = nfs4_proc_mknod,
7224 .statfs = nfs4_proc_statfs,
7225 .fsinfo = nfs4_proc_fsinfo,
7226 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007227 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 .decode_dirent = nfs4_decode_dirent,
7229 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007230 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007231 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007232 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007234 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007235 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007236 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007238 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007239 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007241 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007242 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007243 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007244 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007245 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007246 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007247 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007248 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007249 .create_server = nfs4_create_server,
7250 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251};
7252
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007253static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7254 .prefix = XATTR_NAME_NFSV4_ACL,
7255 .list = nfs4_xattr_list_nfs4_acl,
7256 .get = nfs4_xattr_get_nfs4_acl,
7257 .set = nfs4_xattr_set_nfs4_acl,
7258};
7259
7260const struct xattr_handler *nfs4_xattr_handlers[] = {
7261 &nfs4_xattr_nfs4_acl_handler,
7262 NULL
7263};
7264
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265/*
7266 * Local variables:
7267 * c-basic-offset: 8
7268 * End:
7269 */